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

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