import { sourceFetchData } from '@wix/bex-core'; import type { FiltersMap, OffsetQuery, OffsetQueryResult, SchemaSource, } from '@wix/bex-core'; import type { CollectionConfigWithConditionals } from '@wix/bex-core/react'; type FetchData = (query: OffsetQuery) => Promise>; /** * The collection options a schema-entry hook accepts — everything the source * doesn't supply. Source-owned keys (`queryName`, `paginationMode`, * `fetchData`, `fetchTotal`, `itemKey`, `itemName`) don't exist here. */ export type SchemaCollectionOptions = Omit< CollectionConfigWithConditionals, | 'queryName' | 'paginationMode' | 'fetchData' | 'fetchTotal' | 'itemKey' | 'itemName' > & { /** Wraps the source's fetch (filter/enrich results) without replacing it. */ wrapFetchData?: (next: FetchData) => FetchData; }; /** Builds the collection config from the source: the query fetches through the * source's backend, identity and labels come from the source, and the * consumer's options fill the rest. * `options` is typed loosely — the hooks' option types already enforce the * consumer surface, and TS can't infer `F` back through the conditional * config type; callers pass the generics explicitly. */ export function schemaCollectionConfig( source: SchemaSource, options?: object, ): CollectionConfigWithConditionals { const { wrapFetchData, ...rest } = (options ?? {}) as SchemaCollectionOptions< T, F >; const fetchData = sourceFetchData(source); return { ...rest, queryName: source.collectionId, paginationMode: source.paginationMode, fetchData: wrapFetchData ? wrapFetchData(fetchData) : fetchData, itemKey: source.itemKey, itemName: source.itemName, } as unknown as CollectionConfigWithConditionals; }