import{pathToFileURL as I}from"url";import{HelmetProvider as z}from"@dr.pogodin/react-helmet";import{renderToString as m}from"react-dom/server";import{StaticRouterProvider as G,createStaticHandler as J,createStaticRouter as K}from"react-router-dom/server.js";import r from"react";import{readFile as Q}from"fs/promises";import{join as V}from"path";import{withPathPrefix as l,getLocaleFromPathname as W,withoutPathPrefix as X,removeTrailingSlash as Y}from"@redocly/theme/core/utils";import{RUNTIME_RESOURCES_DIR as T}from"../constants/common.js";import{slash as Z}from"../../utils/path/slash.js";import{reporter as tt}from"../tools/notifiers/reporter.js";import{envConfig as p}from"../config/env-config.js";import{slug as et}from"../../utils/slugger.js";import{shouldForwardProp as rt}from"../../utils/should-forward-prop.js";import{htmlTemplate as ot}from"./template.js";import{detectColorSchemaScript as st,generatePreloadPageData as nt}from"./utils.js";import"./hijack-console.js";import{getSitemapLinkTag as at}from"./get-sitemap-link-tag.js";let g="";async function it(t){return p.isRuntimeMode?await import("../../client/server-entry.js"):await import(I(`${Z(t)}/server-entry.js`)+"?"+new Date)}async function Mt(t){if(!p.isRuntimeMode){const e=tt.getCompilationProblems();if(e.length>0){const{renderCompilationError:n}=await import("./render-compilation-error.js");return n(e)}}const{Loader:u,App:R,routes:w,ServerStyleSheet:E,StyleSheetManager:C,ErrorDetails:P,initL10n:y}=await it(t.serverOutDir),{page:h,store:{globalData:S,hasSitemap:D},sharedData:v,ssrHref:b}=t,f=S?.l10n,i=W(X(h.slug),f?.defaultLocale,f?.locales),d=t.page.request?.url||"http://localhost/";await u.prepare({...h,slug:Y(new URL(d).pathname)},v,S,i),await y(i);const c=new E,{query:M,dataRoutes:x}=J([{Component:R,path:l("*"),children:w}]),s=await M(new Request(d));if(s instanceof Response)throw s;const B=K(x,s);try{globalThis.SSR_HOSTNAME=b,globalThis.SSR_USER_AGENT=t.userAgent,globalThis.SSR_OMIT_SUSPENSE=t.omitSuspense;const e={},n=m(r.createElement(z,{context:e},r.createElement(C,{sheet:c.instance,shouldForwardProp:rt},r.createElement(G,{router:B,context:s})))),L=D?at():"",a=e.helmet,k=a?.title.toString()||"",_=t.store.globalData?.logo?.favicon,o=t.store.config,j=`<link rel="preload" as="fetch" crossorigin="anonymous" href="${l("/app-data.json")}" />`,F=nt(t.page.slug),H=st(o),U=t.page.product?.name?`product-${et(t.page.product.name)}`:"",A=t.store.ssr.headTags.join(""),$=t.store.ssr.postBodyTags.join(""),N=t.store.ssr.preBodyTags.join(""),O=`<link rel="stylesheet" href="${l(T)}/browser-entry.css" />`;!p.isDevelopMode&&!g&&(g=`<style>${await Q(V(t.outdir,T,"browser-entry.css"),"utf-8")}</style>`);const q={bodyHtml:n,linkTags:o.linkTags+c.getStyleTags()+(a?.link.toString()||""),title:k,favicon:_,headScriptTags:L+H+o.headScriptTags+A+(a?.script.toString()||"")+(a?.meta.toString()||""),preload:[j,F],postBodyScriptTags:o.postBodyScriptTags+$,preBodyScriptTags:o.preBodyScriptTags||""+N,lang:i,productClass:U,runtimeCss:g||O};return{html:ot(q),statusCode:200}}catch(e){return t.telemetry.sendSsrErrorCaughtMessage({message:e.message}),{html:m(r.createElement(P,{error:e})),statusCode:500,error:e}}finally{c.seal(),u.clear()}}function xt(t){return m(r.createElement(r.Fragment,null,...t))}export{Mt as render,xt as renderComponents};
