import { useInfiniteQuery } from 'react-query';

import { profileKeys } from 'src/pages/dashboard/profile/queries';
import { getUserList } from 'src/services/users';

const useInfiniteUsersAndAgents = (
  params: {
    name: string;
    limit: number;
    isAgent?: boolean;
  },
  options: any,
) => {
  return useInfiniteQuery(
    profileKeys.userList(params.name),
    ({ pageParam = 1 }) =>
      getUserList(
        params.name,
        pageParam,
        params.limit,
        false,
        [],
        undefined,
        undefined,
        undefined,
        params.isAgent,
      ),
    {
      getNextPageParam: lastPage =>
        lastPage.data.page < lastPage.data.totalPages
          ? lastPage.data.page + 1
          : undefined,
      keepPreviousData: true,
      refetchOnMount: false,
      refetchOnWindowFocus: false,
      ...options,
    },
  );
};

export default useInfiniteUsersAndAgents;
