import { useInfiniteQuery } from 'react-query';

import { getSegmentedServiceItems } from 'src/pages/dashboard/serviceManagement/services';

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

const useInfiniteServiceItems = (
  params: {
    name: string;
    limit: number;
    helpDeskId?: string;
    applyCandidateAgentRule?: boolean;
  },
  options: any,
) => {
  return useInfiniteQuery(
    serviceManagementKeys.serviceItems.list(params),
    ({ pageParam = 1 }) =>
      getSegmentedServiceItems({
        page: pageParam,
        query: params.name,
        limit: params.limit ?? 10,
        includeAssigned: false,
        applyCandidateAgentRule: params.applyCandidateAgentRule ?? false,
        applyVisibilityRule: false,
        helpDeskId: params.helpDeskId,
      }),
    {
      getNextPageParam: lastPage =>
        lastPage.data.page === lastPage.data.totalPages
          ? undefined
          : lastPage.data.page + 1,
      refetchOnMount: false,
      refetchOnWindowFocus: false,
      ...options,
    },
  );
};

export default useInfiniteServiceItems;
