{"version":3,"file":"resolveUnpublishedPrismicPreview.cjs","sources":["../../../src/lib/resolveUnpublishedPrismicPreview.ts"],"sourcesContent":["import type { NormalizedDocument, PagePropsLike } from \"../types\";\n\nimport { usePrismicPreviewStore } from \"../usePrismicPreviewStore\";\n\nimport { fmtLog } from \"./fmtLog\";\nimport { getRepositoryConfig } from \"./getRepositoryConfig\";\n\nconst resolveUnpublishedPrismicPreview = <TProps extends PagePropsLike>(\n\trepositoryName: string,\n\tabortController: AbortController,\n\tsetUnpublishedData: React.Dispatch<\n\t\tReact.SetStateAction<{\n\t\t\tdata?: { [key: string]: NormalizedDocument };\n\t\t\tcomponent?: React.ComponentType<TProps>;\n\t\t}>\n\t>,\n\tpathname?: string,\n): void => {\n\tif (pathname) {\n\t\tconst repositoryConfig = getRepositoryConfig(repositoryName);\n\t\tif (!repositoryConfig) {\n\t\t\tconsole.error(\n\t\t\t\tfmtLog(\n\t\t\t\t\trepositoryName,\n\t\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\t),\n\t\t\t);\n\n\t\t\treturn;\n\t\t}\n\n\t\tif (repositoryConfig.componentResolver) {\n\t\t\tconst state = usePrismicPreviewStore.getState();\n\n\t\t\tconst documentsForPage = Object.values(state.documents).filter(\n\t\t\t\t(doc) => doc.url === pathname,\n\t\t\t);\n\n\t\t\tif (documentsForPage.length > 0) {\n\t\t\t\tconst firstDocument = documentsForPage[0];\n\n\t\t\t\tconst typename = firstDocument.__typename;\n\t\t\t\tconst dataKey = typename.charAt(0).toLowerCase() + typename.slice(1);\n\n\t\t\t\tconst componentResolver = repositoryConfig.componentResolver;\n\n\t\t\t\tconst ResolvedComponent =\n\t\t\t\t\ttypeof componentResolver === \"function\"\n\t\t\t\t\t\t? componentResolver(documentsForPage)\n\t\t\t\t\t\t: componentResolver[firstDocument.type];\n\n\t\t\t\tif (ResolvedComponent) {\n\t\t\t\t\tif (!abortController.signal.aborted) {\n\t\t\t\t\t\tsetUnpublishedData({\n\t\t\t\t\t\t\tdata: { [dataKey]: firstDocument },\n\t\t\t\t\t\t\tcomponent: ResolvedComponent,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\tfmtLog(\n\t\t\t\t\t\t\trepositoryName,\n\t\t\t\t\t\t\t`A component for this unpublished document preview was not found. The app's default 404 page will be displayed instead.\\n\\nIf this was unintentional, you can fix the issue by ensuring the componentResolver option for this repository in <PrismicPreviewProvider> returns a value for \"${firstDocument.type}\" documents.`,\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tconsole.warn(\n\t\t\t\tfmtLog(\n\t\t\t\t\trepositoryName,\n\t\t\t\t\t\"A componentResolver object or function for this repository was not provided to <PrismicPreviewProvider>. The app's default 404 page will be displayed instead of the previewed document.\\n\\nYou can fix this warning by adding a componentResolver value to this repository's configuration object in <PrismicPreviewProvider>.\",\n\t\t\t\t),\n\t\t\t);\n\t\t}\n\t}\n};\n\nexport default resolveUnpublishedPrismicPreview;\n"],"names":["getRepositoryConfig","fmtLog","usePrismicPreviewStore"],"mappings":";;;;AAOA,MAAM,mCAAmC,CACxC,gBACA,iBACA,oBAMA,aACS;AACT,MAAI,UAAU;AACP,UAAA,mBAAmBA,wCAAoB,cAAc;AAC3D,QAAI,CAAC,kBAAkB;AACtB,cAAQ,MACPC,OAAAA,OACC,gBACA,2MAA2M,CAC3M;AAGF;AAAA,IACA;AAED,QAAI,iBAAiB,mBAAmB;AACjC,YAAA,QAAQC,8CAAuB;AAE/B,YAAA,mBAAmB,OAAO,OAAO,MAAM,SAAS,EAAE,OACvD,CAAC,QAAQ,IAAI,QAAQ,QAAQ;AAG1B,UAAA,iBAAiB,SAAS,GAAG;AAC1B,cAAA,gBAAgB,iBAAiB,CAAC;AAExC,cAAM,WAAW,cAAc;AACzB,cAAA,UAAU,SAAS,OAAO,CAAC,EAAE,YAAW,IAAK,SAAS,MAAM,CAAC;AAEnE,cAAM,oBAAoB,iBAAiB;AAErC,cAAA,oBACL,OAAO,sBAAsB,aAC1B,kBAAkB,gBAAgB,IAClC,kBAAkB,cAAc,IAAI;AAExC,YAAI,mBAAmB;AAClB,cAAA,CAAC,gBAAgB,OAAO,SAAS;AACjB,+BAAA;AAAA,cAClB,MAAM,EAAE,CAAC,OAAO,GAAG,cAAe;AAAA,cAClC,WAAW;AAAA,YAAA,CACX;AAAA,UACD;AAAA,QAAA,OACK;AACE,kBAAA,KACPD,cACC,gBACA;AAAA;AAAA,iKAA4R,cAAc,kBAAkB,CAC5T;AAAA,QAEF;AAAA,MACD;AAAA,IAAA,OACK;AACN,cAAQ,KACPA,OAAAA,OACC,gBACA,iUAAiU,CACjU;AAAA,IAEF;AAAA,EACD;AACF;;"}