"use client";
import { ConfirmationModalProps } from "@/components/ui/ConfirmationModal";
import { apiDefaultParams } from "@/constants/Api_Params";
import { useTendersQuery } from "@/hooks/queries";
import useDebounce from "@/hooks/useDebounce";
import { TCustomer } from "@/lib/api";
import { usePathname, useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { z } from "zod";
const useTenderListPresenter = () => {
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [currentTender, setCurrentTender] = useState<TCustomer | null>(null);
  const [search, setSearch] = useState("");
  const [modalConfig, setModalConfig] = useState<
    Partial<ConfirmationModalProps>
  >({});
  const [params, setParams] = useState<Record<string, any>>({
    ...apiDefaultParams,
  });
  const router = useRouter();
  const debouncedSearch = useDebounce(search, 1000);
  const pathName = usePathname();
  const { data, error, isPending } = useTendersQuery(params);

  useEffect(() => {
    const result = z.string().safeParse(debouncedSearch);
    if (result.success) {
      setParams((prevParams) => ({
        ...prevParams,
      }));
    }
  }, [debouncedSearch]);

  const handleFilterChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setSearch(e.target.value);
  };
  const columns = [
    "row",
    "title",
    "country code",
    "submission url",
    "status",
    "actions",
  ];
  return {
    currentTender,
    columns,
    setCurrentTender,
    search,
    handleFilterChange,
    router,
    allTenders: data,
    error,
    isPending,
    setParams,
    data,
    pathName,
    isModalOpen,
    setIsModalOpen,
    modalConfig,
    setModalConfig,
  };
};

export default useTenderListPresenter;
