import { queryItems } from '@wix/bex-utils/@wix/ambassador-items-selection-spi-host-v1-provider/http';
import { useWixPatternsContainer } from '@wix/bex-core/react';
import { useFilterCollection } from '../useFilterCollection';
function buildRequest(params) {
    const { query, providerKey } = params;
    const resolvedQuery = query.cursor != null
        ? {
            cursorPaging: { cursor: query.cursor, limit: query.limit },
        }
        : query.offset != null && query.offset > 0
            ? {
                paging: {
                    offset: query.offset,
                    limit: query.limit,
                },
            }
            : {
                paging: { limit: query.limit, offset: 0 },
                cursorPaging: { limit: query.limit },
            };
    if (!query.cursor) {
        if (query.filters && Object.keys(query.filters).length > 0) {
            resolvedQuery.filter = { ...query.filters };
        }
        // According to the guidelines, we should not pass search query if cursor is present
        if (query.search) {
            resolvedQuery.search = query.search;
        }
    }
    const request = {
        providerKey,
        query: resolvedQuery,
    };
    return request;
}
// When we publish items-selection-code, we need to use this code from there and no to duplicate it here
const useQueryItems = () => {
    const { httpClient } = useWixPatternsContainer();
    const fetchItems = async ({ providerKey, query }) => {
        const request = buildRequest({
            query,
            providerKey,
        });
        const res = await httpClient.request(queryItems(request));
        const { items, pagingMetadata } = res.data;
        return {
            items: items || [],
            total: pagingMetadata?.total || 0,
            hasNext: pagingMetadata?.hasNext,
            cursor: pagingMetadata?.cursors?.next,
        };
    };
    return { fetchItems };
};
export const useItemsSelectionFilter = ({ providerKey, filter, queryFilters, }) => {
    const { httpClient } = useWixPatternsContainer();
    const { fetchItems } = useQueryItems();
    const renderItem = (item) => ({
        title: item?.name ?? '',
        subtitle: item?.description ?? '',
    });
    const collection = useFilterCollection(filter, {
        queryName: providerKey,
        fetchData: async (query) => {
            const { items, ...res } = await fetchItems({
                providerKey,
                query: {
                    ...query,
                    filters: {
                        ...(queryFilters || {}),
                        ...query.filters,
                    },
                },
                httpClient,
            });
            // initial pagination unknown, set to cursor if next cursor exists
            collection._paginationModeRef.current = res.cursor ? 'cursor' : 'offset';
            return {
                ...res,
                items,
            };
        },
        limit: 50,
    });
    return { collection, renderItem };
};
//# sourceMappingURL=useItemsSelectionFilter.js.map