import { parseAsString, parseAsStringEnum, useQueryStates } from 'nuqs';

import { SessionsTabs, type SessionsTabsType } from '../types';

export const sessionFiltersState = {
  search: parseAsString.withDefault(''),
  tab: parseAsStringEnum<SessionsTabsType>(
    Object.values(SessionsTabs),
  ).withDefault(SessionsTabs.PUBLISHED),
};

export const useSessionsFilters = () => {
  const [queryState, setQueryStates] = useQueryStates(sessionFiltersState);

  const clearFilters = () => {
    setQueryStates({
      tab: null,
      search: null,
    });
  };

  const handleTabChange = (value: string) => {
    setQueryStates({ tab: value as SessionsTabsType });
  };

  const handleSearchChange = (value: string) => {
    setQueryStates({ search: value });
  };

  return { ...queryState, clearFilters, handleTabChange, handleSearchChange };
};
