import{context as a}from"esbuild";import{mkdirSync as j,writeFileSync as b,existsSync as M}from"node:fs";import*as m from"node:path";import{RUNTIME_RESOURCES_DIR as Y,TELEMETRY_ENABLED as V}from"../constants/common.js";import{fromCurrentDir as t}from"../utils/paths.js";import{NodeBrowserPlugin as S}from"./plugins/node-browser.js";import{PortalImport as l}from"./plugins/portal-import.js";import{ThemesResolver as u}from"./plugins/themes-resolver.js";import{AssetsResolver as E}from"./plugins/assets-resolver.js";import{EsbuildErrorCollector as _}from"./plugins/esbuild-compile-resolver.js";import{OpenapiDocsModuleReplacer as f}from"./plugins/openapi-docs-module-replacer/index.js";import{ServerPropsResolver as R}from"./plugins/server-props-resolver.js";import{StyledComponentsSSR as v}from"./plugins/styled-components-ssr.js";import{DependencyResolver as d}from"./plugins/dependency-resolver.js";import{YamlLoader as L}from"./plugins/yaml-loader.js";import{ApiRequestHandlersResolver as A}from"./plugins/api-request-handlers-resolver.js";import{MiddlewareResolver as N}from"./plugins/middleware-resolver.js";import{OnRebuild as D}from"./plugins/on-rebuild.js";import{getPublicEnvVariables as $}from"../utils/envs/get-public-env-variables.js";import{PORTAL_VERSION as T}from"../version.js";import{AsyncApiPatch as O}from"./plugins/async-api-patch.js";import{telemetryTraceStep as g}from"../../cli/telemetry/helpers/trace-step.js";const C={bundle:!0,format:"esm",chunkNames:"chunks/[name]-[hash]"};function y(e){const o=m.join(e,"tsconfig.json");return M(o)?o:void 0}const p=new Set;async function ie(){await Promise.all([...p].map(e=>e.dispose())),p.clear()}async function ce(e,o,r="production",n){return await g("build.create_client_compiler",async()=>{const c=$(),i={};Object.entries(c).map(([h,w])=>i[`process.env.${h}`]=JSON.stringify(w));const s=[...n?[D(n)]:[],S({path:"path-browserify",fs:"{}",tty:"tty-browserify",os:"os-browserify",http:"stream-http",https:"stream-http",readline:"{}",crypto:"{}",stream:"{}",zlib:"{}","https-proxy-agent":"{}"}),u(e,e.contentDir),E(e,e.contentDir),_(e,e.contentDir),R(e),d(),l(),L(),O(),v(),f()],I=[t(import.meta.url,"../../client/browser-entry.js"),t(import.meta.url,"../../client/user-tags-entry.js")],P=await a({...C,entryPoints:I,outdir:m.join(o,Y),sourcemap:r!=="production"&&process.env.ENABLE_SOURCE_MAPS==="true",plugins:s,tsconfig:y(e.contentDir),mainFields:["browser","module","main"],metafile:r!=="production",minify:r==="production",splitting:!0,external:["constants","zlib","stream","https","vm","module","worker_threads","child_process","@swc/core"],inject:[process.env.INSPECT_MODE==="true"?t(import.meta.url,"../../client/inspect-mode-hooks.js"):"",t(import.meta.url,"./web-shim.js")].filter(Boolean),define:{...i,"process.env.NODE_ENV":`"${r}"`,"process.env.REDOCLY_LOCAL_DEV":`"${process.env.REDOCLY_LOCAL_DEV}"`,"process.env.SERVER_EDITOR_APP_URL":`"${process.env.SERVER_EDITOR_APP_URL}"`,"process.env.ENABLE_COMMENTS":`"${process.env.ENABLE_COMMENTS}"`,"process.env.REDOCLY_TELEMETRY":V?'""':'"off"',"process.env.REDOCLY_PORTAL_VERSION":`"${T}"`,"process.env":`{"NODE_ENV": "${r}"}`,"process.platform":'"browser"',"process.browser":"true","module.hot":"false",global:"{}",...process.env.REDOCLY_PREFIX_PATHS&&{"process.env.REDOCLY_PREFIX_PATHS":`"${process.env.REDOCLY_PREFIX_PATHS}"`},...process.env.INSPECT_MODE==="true"&&{"process.env.INSPECT_MODE":`${process.env.INSPECT_MODE}`},...process.env.MAIN_API_URL&&{"process.env.MAIN_API_URL":`"${process.env.MAIN_API_URL}"`}},logLevel:"silent"});return p.add(P),P})}async function pe(e,o,r="development",n){j(o,{recursive:!0}),b(m.join(o,"package.json"),JSON.stringify({name:"@redocly/portal/server-cache",type:"module"}));const c=[...n?[D(n)]:[],u(e,e.contentDir),E(e,e.contentDir),_(e,e.contentDir),R(e),A(e),N(e),d(),l(),L(),O(),v(),f()],i=[t(import.meta.url,"../../client/server-entry.js"),t(import.meta.url,"../../client/user-tags-entry.js"),t(import.meta.url,"../../client/server-props-entry.js"),t(import.meta.url,"../../client/api-request-handlers-entry.js"),t(import.meta.url,"../../client/middleware-entry.js")],s=await a({...C,entryPoints:i,outdir:o,plugins:c,sourcemap:r!=="production"&&process.env.ENABLE_SOURCE_MAPS==="true",minify:r==="production",tsconfig:y(e.contentDir),mainFields:["module","main"],splitting:!0,external:["react","react-router-dom","@dr.pogodin/react-helmet","@swc/core","pnpapi","canvas"],banner:{js:`import { createRequire as topLevelCreateRequire } from 'module';
 const require = topLevelCreateRequire(import.meta.url);`},platform:"node",define:{"process.env.NODE_ENV":`"${r}"`,"process.env.REDOCLY_LOCAL_DEV":`"${process.env.REDOCLY_LOCAL_DEV}"`,"module.hot":"false"},logLevel:"silent"});return p.add(s),s}async function ae(e,o,r="production",n){return await g("build.create_server_compiler",async()=>{const c=[...n?[D(n)]:[],S({"node-fetch":"{}",webpack:"{}",swagger2openapi:"{}"}),u(e,e.contentDir),E(e,e.contentDir),_(e,e.contentDir),R(e),A(e),N(e),d(),l(),L(),O(),v(),f()],i=[{in:t(import.meta.url,"../node-bundle-entry.js"),out:"index"},{in:t(import.meta.url,"../../client/user-tags-entry.js"),out:"user-tags-entry"},{in:t(import.meta.url,"../workers/api-routes-worker.js"),out:"api-routes-worker"}],s=await a({...C,entryPoints:i,outExtension:{".js":".mjs"},outdir:o,platform:"node",plugins:c,tsconfig:y(e.contentDir),mainFields:["module","main"],metafile:r!=="production",minify:r==="production",splitting:!1,define:{"process.env.NODE_ENV":`"${r}"`,"process.env.REDOCLY_LOCAL_DEV":`"${process.env.REDOCLY_LOCAL_DEV}"`,"process.env.REDOCLY_STATIC_BUNDLE":"true","process.env.REDOCLY_PORTAL_VERSION":`"${T}"`,"module.hot":"false",...process.env.REDOCLY_PREFIX_PATHS&&{"process.env.REDOCLY_PREFIX_PATHS":`"${process.env.REDOCLY_PREFIX_PATHS}"`}},banner:{js:`import { createRequire as topLevelCreateRequire } from 'module';
 const require = topLevelCreateRequire(import.meta.url);`},logLevel:"silent"});return p.add(s),s})}export{ce as createClientCompiler,ae as createNodeBundleCompiler,pe as createServerCompiler,ie as stopAllCompilers};
