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