import { useInfiniteQuery } from "@tanstack/react-query";
import * as api from "../service/Service";
import { BrandResponse } from "../types/Types";

interface UseInfiniteBrandProductsDataParams {
  page?: number;
  limit?: number;
  category?: string;
  minPrice?: number;
  maxPrice?: number;
  sort?: number;
}

export const useInfiniteBrandProductsData = (
  brandId: string,
  params?: UseInfiniteBrandProductsDataParams
) => {
  return useInfiniteQuery<BrandResponse>({
    queryKey: ["infinite-brand-products", brandId, params],
    queryFn: ({ pageParam = 1 }) =>
      api.getBrandProducts({
        brandId,
        ...params,
        page: pageParam as number,
      }),
    getNextPageParam: (lastPage) => {
      const pager = lastPage.results.pager;
      return pager.nextPage ? pager.page + 1 : undefined;
    },
    initialPageParam: 1,
    enabled: !!brandId,
  });
};
