import { useInfiniteQuery } from '@tanstack/react-query'
import { getClientOperations, OPERATIONS_LIMIT } from '../api/paymentApi'

export function useOperations() {
  return useInfiniteQuery({
    queryKey: ['payment', 'operations'],
    queryFn: ({ pageParam }) => getClientOperations(pageParam as number),
    initialPageParam: 0,
    getNextPageParam: (lastPage, allPages) => {
      if (lastPage.operations.length < OPERATIONS_LIMIT) return undefined
      return allPages.length * OPERATIONS_LIMIT
    },
    staleTime: 30_000,
  })
}
