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