import type * as StackbitTypes from '@stackbit/types'; import type * as ContentStoreTypes from '../types'; import { mapCSIDocumentsToStoreDocuments } from './csi-to-store-docs-converter'; import { mapDocumentsToLocalizedApiObjects } from './store-to-api-docs-converter'; import { mapDocumentsToApiDocuments } from './store-to-api-v2-docs-converter'; import { ContentSourceData } from '../types'; type MapDocumentToApiProps = { contentSourceData: ContentStoreTypes.ContentSourceData; locale?: string; assetSources: StackbitTypes.AssetSource[]; createConfigDelegate: () => StackbitTypes.ConfigDelegate; contentSourceDataById: Record; modelMap: Record; }; export function mapDocumentVersionsToApiDocumentVersions({ versions, contentSourceData, locale, assetSources, createConfigDelegate, contentSourceDataById, modelMap }: MapDocumentToApiProps & { versions: StackbitTypes.DocumentVersion[] }): ContentStoreTypes.APIDocumentVersion[] { return versions.map((version) => { if (!version.document) { return version; } const delegate = createConfigDelegate(); const contentStoreDocuments = mapCSIDocumentsToStoreDocuments({ csiDocuments: [version.document], contentSourceInstance: contentSourceData.instance, defaultLocaleCode: contentSourceData.defaultLocaleCode, modelMap, createConfigDelegate, assetSources, logger: delegate.getLogger() }); const [object] = mapDocumentsToLocalizedApiObjects({ documents: contentStoreDocuments, locale, delegate }); const [apiDocument] = mapDocumentsToApiDocuments({ documents: contentStoreDocuments, contentSourceDataById, delegate }); return { ...version, object, apiDocument }; }); }