"use client";
import { apiDefaultParams } from "@/constants/Api_Params";
import { useGetNotificationHistoryQuery } from "@/hooks/queries/useNotificationQueries";
import { deleteEmptyKeys } from "@/utils/shared";
import { useIsMutating } from "@tanstack/react-query";
import { usePathname, useRouter } from "next/navigation";
import { useState } from "react";
import { useForm } from "react-hook-form";

const useNotificationHistoryTablePresenter = () => {
  const { register, handleSubmit, watch, setValue } = useForm();
  const [isFilterModalOpen, setIsFilterModalOpen] = useState(false);
  const [params, setParams] = useState<Record<string, any>>({
    ...apiDefaultParams,
  });
  const { data, isLoading } = useGetNotificationHistoryQuery(params);
  const isMutating = useIsMutating();
  const columns: string[] = [
    "row",
    "title",
    "channel",
    "created at",
    "receiver",
    "message",
    "priority",
    "type",
    "seen",
    "status",
    "actions",
  ];
  const pathName = usePathname();
  const router = useRouter();
  const search = (data: any) => {
    const newParams = { ...params, ...data, offset: 0 };
    const cleanedParams = deleteEmptyKeys(newParams);
    setParams(cleanedParams);
    const queryString = new URLSearchParams(cleanedParams).toString();
    router.push(`${pathName}?${queryString}`);
    setIsFilterModalOpen(false);
  };
  return {
    notificationHistory: data?.data,
    metadata: data?.meta,
    isLoading,
    columns,
    router,
    pathName,
    setParams,
    register,
    handleSubmit,
    watch,
    setValue,
    isFilterModalOpen,
    setIsFilterModalOpen,
    isMutating,
    search,
  };
};

export default useNotificationHistoryTablePresenter;
