import { useInfiniteQuery, } from '@tanstack/react-query';
import { useSeamClient } from '../../lib/seam/use-seam-client.js';
export function useSeamInfiniteQuery(endpointPath, parameters, options = {}) {
    const { endpointClient: client, queryKeyPrefixes } = useSeamClient();
    return useInfiniteQuery({
        enabled: client != null,
        ...options,
        queryKey: [
            ...queryKeyPrefixes,
            ...endpointPath.split('/').filter((v) => v !== ''),
            parameters,
        ],
        initialPageParam: null,
        getNextPageParam: (lastPage) => lastPage.nextPageCursor,
        queryFn: async ({ pageParam }) => {
            if (client == null)
                return {
                    data: [],
                    nextPageCursor: null,
                };
            // Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.
            // Type assertion is needed here for performance reasons. The types are correct at runtime.
            const endpoint = client[endpointPath];
            const request = endpoint(parameters, options);
            const pages = client.createPaginator(request);
            if (pageParam == null) {
                const [data, { nextPageCursor }] = await pages.firstPage();
                return {
                    data: data,
                    nextPageCursor,
                };
            }
            // Type assertion is needed for pageParam since the Seam API expects a branded PageCursor type.
            const [data, { nextPageCursor }] = await pages.nextPage(pageParam);
            return {
                data: data,
                nextPageCursor,
            };
        },
    });
}
//# sourceMappingURL=use-seam-infinite-query.js.map