import { useInfiniteQuery } from '@tanstack/react-query';
import { getAllProducts } from '../requests/products/productRequest';

export const useInfiniteProducts = (filter) => {
  return useInfiniteQuery(
    ['products'],
    async ({ pageParam = 1 }) => await getAllProducts(pageParam),
    {
      getNextPageParam: (lastPage, pages) => {
        const maxPages = Math.ceil(lastPage?.productCount / 9);
        const nextPage = pages.length + 1;
        if (nextPage <= maxPages) {
          return nextPage;
        }
      },
      enabled: filter === 'All' || filter === '',
      staleTime: 0,
      cacheTime: 0,
    }
  );
};
