"use client";
import { apiDefaultParams } from "@/constants/Api_Params";
import {
  useCompanyCategoriesQuery,
  useDeleteCompanyCategoryQuery,
  useToggleCompanyCategoryPublished,
} from "@/hooks/queries";
import { TCompanyCategory } from "@/lib/api";
import { usePathname, useRouter } from "next/navigation";
import { useState } from "react";

const useCompanyCategoriesPresenter = () => {
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [currentCategory, setCurrentCategory] = useState<TCompanyCategory>();
  const [params, setParams] = useState<Record<string, any>>({
    ...apiDefaultParams,
  });
  const router = useRouter();
  const pathName = usePathname();
  const { data, isLoading, isError } = useCompanyCategoriesQuery(params);
  const { mutate: deleteCategory } = useDeleteCompanyCategoryQuery(() =>
    setIsModalOpen(false),
  );
  const {mutate:togglePublished,isPending:isToggling} = useToggleCompanyCategoryPublished()
  const onConfirmDelete = () => {
    if (!currentCategory) return;
    deleteCategory(currentCategory?.id);
  };
  return {
    categories: data?.data.categories,
    isModalOpen,
    setIsModalOpen,
    isLoading,
    isError,
    pathName,
    router,
    params,
    setParams,
    currentCategory,
    togglePublished,
    isToggling,
    setCurrentCategory,
    onConfirmDelete,
  };
};

export default useCompanyCategoriesPresenter;
