import{EVE_DEV_DISPATCH_SCHEDULE_ROUTE_PATTERN,EVE_DEV_RUNTIME_ARTIFACTS_ROUTE_PATH,EVE_HEALTH_ROUTE_PATH,EVE_INFO_ROUTE_PATH}from"#protocol/routes.js";import{dirname,join,relative}from"node:path";import{resolvePackageRoot,resolvePackageSourceFilePath,resolveWorkflowModulePath}from"#internal/application/package.js";import{mkdir,writeFile}from"node:fs/promises";import{normalizeEsmImportSpecifier,stringifyEsmImportSpecifier}from"#internal/application/import-specifier.js";import{EVE_WORKFLOW_QUEUE_PREFIX}from"#internal/workflow/queue-namespace.js";import{WorkflowBundleBuilder}from"#internal/workflow-bundle/builder.js";import{createNitroArtifactsConfig}from"#internal/nitro/host/artifacts-config.js";import{computeChannelRouteRegistrations,registerChannelVirtualHandlers}from"#internal/nitro/host/channel-routes.js";function includesApplicationRoutes(e){return e===`all`||e===`app`}function includesWorkflowBundles(e){return includesWorkflowRoute(e)}function includesWorkflowRoute(e){return e===`all`||e===`flow`}function registerHandler(e,t){let n=`#eve-route-handler/${t.method??`ALL`} ${t.route}`,r=stringifyEsmImportSpecifier(t.handlerPath);e.options.handlers.push({handler:n,method:t.method,route:t.route}),e.options.virtual[n]=[`import handler from ${r};`,`export default handler;`].join(`
`)}function resolveNitroWorkflowBuildDirectory(e){return join(e.options.buildDir,`workflow`)}function createRelativeImportSpecifier(e,t){let n=relative(e,t).replaceAll(`\\`,`/`);return n.startsWith(`.`)?n:`./${n}`}async function addWorkflowFileHandler(e,t){let n=join(resolveNitroWorkflowBuildDirectory(e),`${t.bundleName}-handler.mjs`),r=dirname(n),a=createRelativeImportSpecifier(r,t.bundlePath),o=(t.directHandlers??[]).map(e=>{let t=createRelativeImportSpecifier(r,e.bundlePath);return{importSpecifier:t,isOwnBundle:t===a,queuePrefix:e.queuePrefix}});await mkdir(r,{recursive:!0}),await writeFile(n,buildWorkflowFileHandlerSource({bundlePath:a,directHandlers:o,runtimeImportSpecifier:t.runtimeImportSpecifier})),e.options.handlers.push({handler:n,route:t.route})}function buildWorkflowFileHandlerSource(e){let t=[`// Generated by Eve. Do not edit by hand.`,`import { POST } from ${JSON.stringify(e.bundlePath)};`];if(e.directHandlers.length>0&&e.runtimeImportSpecifier!==void 0){let n=0,r=e.directHandlers.map(e=>{if(e.isOwnBundle)return{...e,binding:`POST`};let t=`__eveWorkflowDirectHandler${n}`;return n+=1,{...e,binding:t}});for(let e of r)e.isOwnBundle||t.push(`import { POST as ${e.binding} } from ${JSON.stringify(e.importSpecifier)};`);t.push(`import { getWorld as __eveGetWorkflowWorld } from ${JSON.stringify(e.runtimeImportSpecifier)};`,``,`try {`,`  const __eveWorkflowWorld = await __eveGetWorkflowWorld();`,`  if (typeof __eveWorkflowWorld?.registerHandler === "function") {`);for(let e of r)t.push(`    __eveWorkflowWorld.registerHandler(${JSON.stringify(e.queuePrefix)}, ${e.binding});`);t.push(`  }`,`} catch (err) {`,`  console.warn("[eve] Failed to register direct workflow queue handlers:", err);`,`}`)}return t.push(``,`export default async ({ req }) => {`,`  return await POST(req);`,`};`,``),t.join(`
`)}function addFrameworkVirtualHandler(e,t){let n=`#eve-route${t.route}`,r=stringifyEsmImportSpecifier(t.modulePath);e.options.handlers.push({handler:n,method:t.method,route:t.route}),e.options.virtual[n]=[`import { ${t.handlerExport} } from ${r};`,`export default async (event) => ${t.handlerExport}(${t.args}, event.req);`].join(`
`)}async function configureNitroRoutes(i,a,s){if(includesWorkflowBundles(s.surface)){let e=resolvePackageRoot(),t=new WorkflowBundleBuilder({appRoot:a.appRoot,compiledArtifactsBootstrapPath:a.compiledArtifacts.bootstrapPath,outDir:a.workflowBuildDir,rootDir:e,watch:i.options.dev}),n=Promise.resolve(),buildWorkflowArtifacts=async()=>{await t.build({nitroStepOutfile:includesWorkflowRoute(s.surface)?join(resolveNitroWorkflowBuildDirectory(i),`steps.mjs`):void 0,nitroWorkflowOutfile:i.options.dev&&includesWorkflowRoute(s.surface)?join(resolveNitroWorkflowBuildDirectory(i),`workflows.mjs`):void 0})},syncWorkflowArtifacts=async()=>{let e=n.then(buildWorkflowArtifacts);n=e.catch(()=>{}),await e},r=!0;await syncWorkflowArtifacts(),i.hooks.hook(`build:before`,async()=>{if(r){r=!1;return}await syncWorkflowArtifacts()}),i.options.dev&&i.hooks.hook(`dev:reload`,async()=>{await syncWorkflowArtifacts()})}let c=createNitroArtifactsConfig({appRoot:a.appRoot,dev:i.options.dev});includesApplicationRoutes(s.surface)&&(registerHandler(i,{handlerPath:resolvePackageSourceFilePath(`src/internal/nitro/routes/index.ts`),method:`GET`,route:`/`}),registerHandler(i,{handlerPath:resolvePackageSourceFilePath(`src/internal/nitro/routes/health.ts`),method:`GET`,route:EVE_HEALTH_ROUTE_PATH}),addFrameworkVirtualHandler(i,{args:JSON.stringify({...c,mode:i.options.dev?`development`:`production`}),handlerExport:`handleAgentInfoRequest`,method:`GET`,modulePath:resolvePackageSourceFilePath(`src/internal/nitro/routes/info.ts`),route:EVE_INFO_ROUTE_PATH}),registerChannelVirtualHandlers(i,{artifactsConfig:c,registrations:computeChannelRouteRegistrations(a)}),i.options.dev&&(addFrameworkVirtualHandler(i,{args:JSON.stringify({appRoot:c.appRoot}),handlerExport:`handleDevRuntimeArtifactsRequest`,method:`GET`,modulePath:resolvePackageSourceFilePath(`src/internal/nitro/routes/dev-runtime-artifacts.ts`),route:EVE_DEV_RUNTIME_ARTIFACTS_ROUTE_PATH}),addFrameworkVirtualHandler(i,{args:JSON.stringify({appRoot:c.appRoot}),handlerExport:`handleDevScheduleDispatchRequest`,method:`POST`,modulePath:resolvePackageSourceFilePath(`src/internal/nitro/routes/dev-schedule-dispatch.ts`),route:EVE_DEV_DISPATCH_SCHEDULE_ROUTE_PATTERN})));let l=resolveNitroWorkflowBuildDirectory(i),u=includesWorkflowRoute(s.surface)?i.options.dev?join(l,`workflows.mjs`):join(a.workflowBuildDir,`workflows.mjs`):void 0,d=i.options.dev&&u!==void 0?[{bundlePath:u,queuePrefix:EVE_WORKFLOW_QUEUE_PREFIX}]:[],f=d.length>0?normalizeEsmImportSpecifier(resolveWorkflowModulePath(`workflow/runtime`)):void 0;u&&await addWorkflowFileHandler(i,{bundleName:`workflows`,bundlePath:u,directHandlers:d,route:`/.well-known/workflow/v1/flow`,runtimeImportSpecifier:f}),i.routing.sync()}export{configureNitroRoutes};