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