import{mkdirSync as l,writeFileSync as u,existsSync as d}from"fs";import{dirname as i,join as p}from"path";import{slash as C}from"../../utils/path/slash.js";import{ensureDir as a}from"../utils/fs.js";import{fromCurrentDir as m}from"../utils/paths.js";function h(n){return Array.from(n.entries()).sort(([e],[o])=>o.localeCompare(e))}let c=null;function S(n){const e=m(import.meta.url,"../../client/runtime/generated/templates.js"),o=`export const templates = {
`+h(n.templates).map(([t,s])=>`  "${t}": () => import("${C(s)}"),
`).join(`
`)+"}";l(i(e),{recursive:!0});const r=p(i(e),"package.json");d(r)||u(p(i(e),"package.json"),JSON.stringify({name:"@redocly/portal/ssr-entry"})),c!==o&&(u(a(e),o,"utf-8"),c=o)}let g=null;function b(n){const e=Array.from(n.browserPlugins),o=m(import.meta.url,"../../client/runtime/generated/browser-plugins.js"),r=`
    ${e.map((t,s)=>`import * as plugin${s} from '${t.replace(/\\/g,"/")}'`).join(`
`)}

    export const onRouteChange = (context, themeConfig) => {
      ${e.map((t,s)=>`plugin${s}.onRouteChange?.(context, themeConfig);`).join(`
`)}

      // Hooks registered during browser runtime
      for (const hookCallback of __onRouteChangeHooks) {
        typeof hookCallback === 'function' && hookCallback(context, themeConfig)
      }
    }
  `;l(i(o),{recursive:!0}),g!==r&&(u(a(o),r,"utf-8"),g=r)}let f=null;function R(n){const e=m(import.meta.url,"../../client/runtime/generated/routes.js"),r=`
  export const clientRoutes = [
    ${Array.from(n.routesBySlug.values()).filter(t=>t.hasClientRoutes).map(t=>t.baseSlug||t.slug).map(t=>`  ${JSON.stringify(t)},`).join(`
`)}
  ]`;l(i(e),{recursive:!0}),f!==r&&(u(a(e),r,"utf-8"),f=r)}export{b as generateBrowserPluginsModule,R as generateClientRoutes,S as generateTemplatesModule};
