import { useQueryStates } from 'nuqs';

import { employeeLifecycleFilterState, PAGE_SIZE } from '../constants';
import {
  JourneyProgressStatusType,
  JourneyTaskExpirationStatusType,
} from '../types';

type ReturnType = {
  search: string | undefined;
  progressStatus: JourneyProgressStatusType | undefined;
  expirationStatus: JourneyTaskExpirationStatusType | undefined;
  collaborators: string | undefined;
  limit: number | undefined;
};

export const useFilterParams = () => {
  const [queryState, setQueryStates] = useQueryStates(
    employeeLifecycleFilterState,
  );
  const { search, progressStatus, expirationStatus, collaborators, limit } =
    queryState;

  const clearFilters = () => {
    setQueryStates({
      search: null,
      progressStatus: null,
      expirationStatus: null,
      collaborators: null,
      limit: PAGE_SIZE,
    });
  };

  const filterParams: ReturnType = {
    progressStatus: progressStatus ?? undefined,
    expirationStatus: expirationStatus ?? undefined,
    search: search ?? undefined,
    collaborators: collaborators ?? undefined,
    limit: limit ?? undefined,
  };

  return { filterParams, clearFilters, setQueryStates };
};
