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