{"version":3,"file":"bootstrapPrismicPreview.cjs","sources":["../../../src/lib/bootstrapPrismicPreview.ts"],"sourcesContent":["import { CustomTypeModel, SharedSliceModel } from \"@prismicio/client\";\nimport { withAssetPrefix } from \"gatsby\";\n\nimport { usePrismicPreviewStore } from \"../usePrismicPreviewStore\";\n\nimport { fetchLinkedDocuments } from \"./fetchLinkedDocuments\";\nimport { fetchNewDocuments } from \"./fetchNewDocuments\";\nimport { fetchPublishedDocumentIDs } from \"./fetchPublishedDocumentIDs\";\nimport { fmtLog } from \"./fmtLog\";\nimport { getClient } from \"./getClient\";\nimport { getPluginOptions } from \"./getPluginOptions\";\nimport { getRepositoryConfig } from \"./getRepositoryConfig\";\nimport { normalizeDocument } from \"./normalizeDocument\";\n\nconst bootstrapPrismicPreview = async (\n\trepositoryName: string,\n\tabortController: AbortController,\n): Promise<void> => {\n\tconst state = usePrismicPreviewStore.getState();\n\n\t// Only bootstrap once per session\n\tif (state.isBootstrapped) {\n\t\treturn;\n\t}\n\n\tconst pluginOptions = getPluginOptions(repositoryName);\n\tif (!pluginOptions) {\n\t\tconsole.error(\n\t\t\tfmtLog(\n\t\t\t\trepositoryName,\n\t\t\t\t'Plugin options could not be found. Did you add \"gatsby-plugin-prismic-previews\" for this repository to your app\\'s \"gatsby-config.js\" file?',\n\t\t\t),\n\t\t);\n\n\t\treturn;\n\t}\n\n\tconst repositoryConfig = getRepositoryConfig(repositoryName);\n\tif (!repositoryConfig) {\n\t\tconsole.error(\n\t\t\tfmtLog(\n\t\t\t\trepositoryName,\n\t\t\t\t'Repository configuration could not be found. Did you add <PrismicPreviewProvider> to your \"gatsby-browser.js\" and \"gatsby-ssr.js\"? It must contain a repository configuration object for this repository.',\n\t\t\t),\n\t\t);\n\n\t\treturn;\n\t}\n\n\tconst client = getClient(pluginOptions);\n\n\tconst signal = abortController.signal;\n\n\t// @ts-expect-error - `getCachedRepository()` is a private internal\n\t// client method. We use it here to manually prime the cached\n\t// repository cache. This saves us from fetching the repository\n\t// multiple times when we run queries in parallel in the next block.\n\tawait client.getCachedRepository();\n\n\tconst [localPublishedDocumentIDs, newDocuments] = await Promise.all([\n\t\tfetchPublishedDocumentIDs({\n\t\t\tclient,\n\t\t\tabortController,\n\t\t\tpluginOptions,\n\t\t}),\n\t\tfetchNewDocuments({ client, abortController }),\n\t]);\n\n\tif (localPublishedDocumentIDs.length) {\n\t\tstate.setPublishedDocumentIDs(localPublishedDocumentIDs);\n\t}\n\n\tconst modelsRaw = await fetch(withAssetPrefix(__PUBLIC_MODELS_PATH__), {\n\t\tsignal,\n\t});\n\tconst models: Record<\n\t\tstring,\n\t\t{\n\t\t\tcustomTypeModels: CustomTypeModel[];\n\t\t\tsharedSliceModels: SharedSliceModel[];\n\t\t}\n\t> = await modelsRaw.json();\n\n\tconst modelsForRepository = models[pluginOptions.repositoryName];\n\n\tawait Promise.all([\n\t\tfetchLinkedDocuments(\n\t\t\tnewDocuments,\n\t\t\tclient,\n\t\t\tpluginOptions,\n\t\t\trepositoryConfig,\n\t\t\tmodelsForRepository.customTypeModels,\n\t\t\tmodelsForRepository.sharedSliceModels,\n\t\t\tabortController,\n\t\t),\n\t\tPromise.all(\n\t\t\tnewDocuments.map(async (doc) => {\n\t\t\t\tconst model = modelsForRepository.customTypeModels.find(\n\t\t\t\t\t(model) => model.id === doc.type,\n\t\t\t\t);\n\n\t\t\t\tif (model) {\n\t\t\t\t\tconst normalizedDocument = await normalizeDocument(\n\t\t\t\t\t\tdoc,\n\t\t\t\t\t\tmodel,\n\t\t\t\t\t\tmodelsForRepository.sharedSliceModels,\n\t\t\t\t\t\trepositoryConfig,\n\t\t\t\t\t\tpluginOptions,\n\t\t\t\t\t);\n\n\t\t\t\t\tstate.addDocument(normalizedDocument);\n\t\t\t\t}\n\t\t\t}),\n\t\t),\n\t]);\n\n\tstate.setIsBootstrapped(true);\n};\n\nexport default bootstrapPrismicPreview;\n"],"names":["usePrismicPreviewStore","getPluginOptions","fmtLog","getRepositoryConfig","getClient","fetchPubl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