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

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

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