import { useState, useMemo } from "react";
import type { FilterValues } from "@/components/Filters";

const DEFAULT_PAGE = 1;
const DEFAULT_LIMIT = 10;

export const useNotificationFilters = () => {
  const [filters, setFilters] = useState<FilterValues>({});
  const [currentPage, setCurrentPage] = useState(DEFAULT_PAGE);
  const [limit] = useState(DEFAULT_LIMIT);

  const apiParams = useMemo(() => {
    const params: Record<string, string | number> = {
      page: currentPage,
      limit: limit,
    };

    if (filters.search) {
      params.search = filters.search as string;
    }

    if (filters.notificationType) {
      params.notificationType = filters.notificationType as string;
    }

    return params;
  }, [filters, currentPage, limit]);

  const handleFiltersChange = (newFilters: FilterValues) => {
    setFilters(newFilters);
    setCurrentPage(DEFAULT_PAGE);
  };

  const handlePageChange = (page: number) => {
    setCurrentPage(page);
  };

  return {
    filters,
    currentPage,
    apiParams,
    handleFiltersChange,
    handlePageChange,
    limit,
  };
};
