import*as I from"react";import{withPathPrefix as v}from"@redocly/theme/core/utils";import{appendSearchParams as j}from"../utils";import{mapObject as z}from"../../utils/object/map-object.js";import{mapObjectAsync as A}from"../../utils/object/map-object-async.js";import{isFunction as G}from"../../utils/guards/is-function.js";import{isBrowser as N}from"../../utils/env/is-browser.js";import{GLOBAL_DATA_URL as $}from"../../constants/common.js";import{getClientPageDataUrl as V}from"../../utils/url/get-client-page-data-url.js";import{getSharedDataUrl as B}from"../../utils/url/get-shared-data-url.js";import{normalizeRouteSlug as l}from"../../utils/path/normalize-route-slug.js";import{templates as g}from"./generated/templates.js";import{AsyncCache as S}from"./cache.js";import{fetch as y}from"./fetch.js";import{updateTranslations as K}from"../app/l10n";import{PageMissingDefaultExportError as b}from"./errors/page-missing-default-export-error.js";const d=new S,i=new S;let D=null,m,W;const u=new Set;async function q(){const o=await(await y(v($))).json();return m=o,o}async function C(o){const t=v(B(o)),e=process.env.NODE_ENV==="development"?t:encodeURI(t);return await(await y(e)).json()}async function F(o){return i.has(o)||i.set(o,C(o)),i.get(o)}function lt(){const[,o]=I.useReducer(t=>t+1,0);typeof window>"u"||process.env.NODE_ENV!=="production"&&(window.__redoclyRefresh=o)}function _(o,t){const e=t?new URLSearchParams(t).get("revision"):null,a=t?new URLSearchParams(t).get("version"):null;return e||a?`${o}?${e?`revision=${encodeURIComponent(e)}`:""}${a?`&version=${encodeURIComponent(a)}`:""}`.trim():o}class p{static markdocComponents={};static async updateTemplate(t,e,a){const n=l(t);g[a]=()=>e;const r=l(window.location.pathname);n===r&&(await this.load(t,!0),window.__redoclyRefresh())}static async update(t){const e=l(t),a=l(window.location.pathname);if(e!==a){d.delete(e);return}await this.load(t,!0),window.__redoclyRefresh()}static async updateSharedData(t){await i.set(t,C(t)),window.__redoclyRefresh()}static addGlobalDataListener(t){u.add(t)}static removeGlobalDataListener(t){u.delete(t)}static updateGlobalData(t){u.forEach(e=>e(t)),m=t,K(m.l10n)}static async tryLoad(t,e,a){try{return await this.load(t,e,a)}catch{return null}}static async loadDynamicMarkdocComponents(t){const e=this.markdocComponents?.[t];e&&G(e)&&Object.assign(this.markdocComponents,await e())}static async load(t,e,a){const n=l(t),r=_(n,a),s=await d.get(r);if(!m&&!D&&(D=q()),s&&!e)return s;{const[f]=await Promise.all([d.set(r,c(a)),D]);return f}async function c(f){const U=V(n),k=j(U,f),E=await y(k),{templateId:R,sharedDataIds:w,props:P,userData:T,versions:L,redirectTo:M,isPublic:O}=await E.json(),[x]=await Promise.all([(async()=>p.loadTemplate(R))(),Promise.all((P.dynamicMarkdocComponents||[]).map(h=>p.loadDynamicMarkdocComponents(h))),w?A(w,h=>F(h)):Promise.resolve({})]);return{templateId:R,slug:n,sharedDataIds:w,redirectTo:M,props:P,Template:x,userData:T,versions:L,isPublic:O}}}static loadSync(t){const e=l(t),a=_(e,typeof window<"u"?window.location.search:void 0),n=d.tryGetSync(a);if(!n)return;const r=z(n.sharedDataIds,s=>i.getSync(s,"Data dependency not cached ahead of time: "+s));return{...n,sharedData:r}}static async prepare(t,e,a,n){const r=t.templateId;if(await Promise.all((t.props.dynamicMarkdocComponents||[]).map(c=>this.loadDynamicMarkdocComponents(c))),e)for(const c of Object.keys(e))i.setSync(c,e[c]);const s=await p.loadTemplate(r);s&&d.setSync(t.slug,{templateId:r,slug:t.slug,props:t.props,sharedDataIds:t.sharedDataIds||{},Template:s,userData:t.userInfo,versions:t.versions,isPublic:t.isPublic}),m=a,W=n}static async loadTemplate(t){try{const e=await g[t]();if(t.endsWith("page.tsx")&&!e.default)throw new b(t);const a=e.default;return typeof a=="function"?a:a.default}catch(e){if(!(process.env.NODE_ENV==="development")||!N())throw e;return e instanceof b?()=>(window.location.reload(),null):(window.location.reload(),null)}}static clear(){u.clear(),i.clear(),d.clear()}}(typeof window<"u"?window:global).__LOADER=p;export{p as Loader,F as getSharedData,m as globalData,W as initLocale,lt as useRefresh};
