import identity from 'lodash/identity';
import { useWixPatternsContainer } from '../providers';
import { useCreateCollection } from './useCollection';
import { useState } from 'react';
async function fetchData({ config, wixPatternsQuery, httpClient, errorHandler, }) {
    const { ambassador } = config;
    const transformPlatformizedQuery = config.transformPlatformizedQuery || identity;
    const platformizedQuery = defaultPlatformizedQuery(wixPatternsQuery);
    const query = await transformPlatformizedQuery(platformizedQuery, wixPatternsQuery);
    return errorHandler?.withErrorHandler(() => httpClient.request(ambassador.queryEntities({ query })), {});
}
function getOffsetConfig({ config, httpClient, errorHandler, }) {
    return {
        queryName: config.ambassador.fqdn,
        ...config,
        paginationMode: 'offset',
        fetchData: async (query) => {
            const response = await fetchData({
                config,
                wixPatternsQuery: query,
                httpClient,
                errorHandler,
            });
            return {
                items: response?.data.entities ?? [],
                total: response?.data.metadata.total ?? 0,
            };
        },
    };
}
function getCursorConfig({ config, httpClient, errorHandler, }) {
    const { ambassador } = config;
    return {
        queryName: ambassador.fqdn,
        ...config,
        paginationMode: 'cursor',
        fetchData: async (query) => {
            const response = await fetchData({
                config,
                wixPatternsQuery: query,
                httpClient,
                errorHandler,
            });
            return {
                items: response?.data.entities ?? [],
                cursor: response?.data.metadata.cursors.next,
                total: response?.data.metadata.total ?? 0,
            };
        },
    };
}
export function useCreateAmbassadorCollection() {
    const createCollection = useCreateCollection();
    const { httpClient, errorHandler } = useWixPatternsContainer();
    return (config) => {
        const collectionConfig = config.ambassador.queryEntities.paginationMode === 'offset'
            ? getOffsetConfig({ config, httpClient, errorHandler })
            : getCursorConfig({ config, httpClient, errorHandler });
        return createCollection(collectionConfig);
    };
}
export function useAmbassadorCollection(config) {
    const createAmbassadorCollection = useCreateAmbassadorCollection();
    const [collection] = useState(() => createAmbassadorCollection(config));
    return collection;
}
function defaultPlatformizedQuery(wixPatternsQuery) {
    return {
        filter: {},
        sort: wixPatternsQuery.sort?.map((sort) => ({
            fieldName: sort.fieldName,
            order: sort.order.toUpperCase(),
        })),
        cursorPaging: {
            cursor: wixPatternsQuery.cursor,
            limit: wixPatternsQuery.limit,
        },
    };
}
//# sourceMappingURL=useAmbassadorCollection.js.map