import { useQuery } from 'react-query';

import { getHydratedTasks } from 'src/pages/dashboard/serviceManagement/services';
import { CoordinatorFilterParams } from 'src/pages/dashboard/serviceManagement/types';

import { serviceManagementKeys } from '../queries';

const allowedSortDirections = ['ASC', 'DESC'];

const useCoordinatorTasks = (params: CoordinatorFilterParams) => {
  const tasksQuery = useQuery(
    serviceManagementKeys.tasks.hydratedList(params),
    () =>
      getHydratedTasks({
        ...params,
        sortDirection: allowedSortDirections.includes(
          params.sortDirection || '',
        )
          ? params.sortDirection
          : 'DESC',
      }),
    {
      enabled: !!params?.helpDeskId,
      keepPreviousData: true,
      refetchOnMount: false,
      select: response => response.data,
    },
  );

  return tasksQuery;
};

export default useCoordinatorTasks;
