import { useInfiniteQuery } from 'react-query';

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

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

const DEFAULT_LIMIT = 10;

const useInfiniteAgentGroups = (
  params: {
    name: string;
    limit: number;
    helpDeskId?: string;
  },
  options: any,
) => {
  return useInfiniteQuery(
    serviceManagementKeys.agentGroups.list(params),
    ({ pageParam = 1 }) =>
      getAgentGroups({
        page: pageParam,
        limit: params.limit ?? DEFAULT_LIMIT,
        name: params.name,
        helpDeskId: params.helpDeskId,
      }),
    {
      getNextPageParam: lastPage =>
        lastPage.data.page === lastPage.data.totalPages
          ? undefined
          : lastPage.data.page + 1,
      ...options,
    },
  );
};

export default useInfiniteAgentGroups;
