import{getWorldImport}from"../../node_modules/.pnpm/@workflow_utils@5.0.0-beta.6/node_modules/@workflow/utils/dist/world-target.js";import"../../node_modules/.pnpm/@workflow_utils@5.0.0-beta.6/node_modules/@workflow/utils/dist/index.js";import{resolvePackageCompiledFilePath,resolvePackageSourceFilePath}from"#internal/application/package.js";import{existsSync}from"node:fs";import{join}from"node:path";import{copyFile,mkdir,writeFile}from"node:fs/promises";import{createCompiledModuleMapSource}from"#compiler/module-map.js";import{stringifyEsmImportSpecifier}from"#internal/application/import-specifier.js";import{buildPackageUserAgent}from"#internal/user-agent.js";import{readMaterializedAuthoredModuleIndex}from"#internal/materialized-authored-modules.js";import{usesParentDevelopmentWorkflowWorld}from"#internal/workflow/development-world-protocol.js";async function writeCompiledArtifactsFiles(e){let t=join(e.outDir,`compiled-artifacts-bootstrap.mjs`),r=join(e.outDir,`compiled-artifacts-instrumentation.mjs`),a=join(e.outDir,`compiled-artifacts-workflow-world.mjs`),s=resolveInstrumentationModule(e.compileResult.manifest.agentRoot);await mkdir(e.outDir,{recursive:!0}),await writeFile(t,await createCompiledArtifactsBootstrapSource({compileResult:e.compileResult,installModulePath:resolvePackageSourceFilePath(`src/runtime/loaders/bundled-artifacts.ts`),moduleMapPath:t,metadata:e.compileResult.metadata})),await writeFile(a,createWorkflowWorldPluginSource({compiledArtifactsBootstrapPath:t,configuredWorld:e.compileResult.manifest.config.experimental?.workflow?.world,defaultWorld:e.defaultWorkflowWorld})),s!==void 0&&await writeFile(r,createInstrumentationPluginSource({agentName:e.compileResult.manifest.config.name,instrumentationPath:s,registerConfigPath:resolvePackageSourceFilePath(`src/harness/instrumentation-config.ts`)}));let c={bootstrapPath:t,workflowWorldPluginPath:a};return s!==void 0&&(c.instrumentationPluginPath=r,c.instrumentationSourcePath=s),c}async function writeDevelopmentCompiledArtifactsFiles(e){let t=join(e.outDir,`compiled-artifacts-bootstrap.mjs`),r=join(e.outDir,`compiled-artifacts-instrumentation.mjs`),s=join(e.outDir,`compiled-artifacts-instrumentation-source.mjs`),c=join(e.outDir,`compiled-artifacts-workflow-world.mjs`),l=await readMaterializedAuthoredModuleIndex(e.runtimeAppRoot);if(l===void 0)throw Error(`Development generation at "${e.runtimeAppRoot}" is not materialized.`);await mkdir(e.outDir,{recursive:!0}),await writeFile(t,createDevelopmentCompiledArtifactsBootstrapSource(e.compileResult.manifest.config.name)),await writeFile(c,createDevelopmentWorkflowWorldPluginSource({compiledArtifactsBootstrapPath:t,configuredWorld:e.compileResult.manifest.config.experimental?.workflow?.world}));let u={bootstrapPath:t,workflowWorldPluginPath:c};return l.instrumentation!==void 0&&(await copyFile(join(e.runtimeAppRoot,`.eve`,`compile`,l.instrumentation),s),await writeFile(r,createInstrumentationPluginSource({agentName:e.compileResult.manifest.config.name,instrumentationPath:s,registerConfigPath:resolvePackageSourceFilePath(`src/harness/instrumentation-config.ts`)})),u.instrumentationPluginPath=r,u.instrumentationSourcePath=s),u}function createDevelopmentCompiledArtifactsBootstrapSource(e){return[`// Generated by eve. Do not edit by hand.`,`import { installEveWorkflowQueueNamespace } from ${stringifyEsmImportSpecifier(resolvePackageSourceFilePath(`src/internal/workflow/queue-namespace.ts`))};`,``,`installEveWorkflowQueueNamespace(${JSON.stringify(e)});`,``,`export default function installDevelopmentCompiledArtifactsPlugin() {}`,``].join(`
`)}const INSTRUMENTATION_EXTENSIONS=[`.ts`,`.mts`,`.js`,`.mjs`];function resolveInstrumentationModule(e){for(let t of INSTRUMENTATION_EXTENSIONS){let n=join(e,`instrumentation${t}`);if(existsSync(n))return n}}function stripCompiledModuleMapExports(e){return e.replace(/^export const moduleMap = /m,`const moduleMap = `).replace(/\nexport default moduleMap;\n?$/,`
`)}async function createCompiledArtifactsBootstrapSource(e){let t=e.compileResult.manifest.config.name,r=stripCompiledModuleMapExports(createCompiledModuleMapSource({importSpecifierStyle:`absolute`,manifest:e.compileResult.manifest,moduleMapPath:e.moduleMapPath})).trim();return[`// Generated by eve. Do not edit by hand.`,`import { installBundledCompiledArtifacts } from ${stringifyEsmImportSpecifier(e.installModulePath)};`,`import { installEveWorkflowQueueNamespace } from ${stringifyEsmImportSpecifier(resolvePackageSourceFilePath(`src/internal/workflow/queue-namespace.ts`))};`,``,`installEveWorkflowQueueNamespace(${JSON.stringify(t)});`,``,r,``,`const metadata = ${JSON.stringify(e.metadata,null,2)};`,``,`const manifest = ${JSON.stringify(e.compileResult.manifest,null,2)};`,``,`export function installCompiledArtifactsBootstrap() {`,`  installBundledCompiledArtifacts({`,`    manifest,`,`    metadata,`,`    moduleMap,`,`  });`,`}`,``,`installCompiledArtifactsBootstrap();`,``,`// Default export satisfies the Nitro plugin contract so this file`,`// can be used directly as a Nitro plugin without a separate wrapper.`,`export default function installCompiledArtifactsPlugin() {`,`  // Already installed on import above.`,`}`,``].join(`
`)}function resolveWorkflowWorldWiring(e){if(e===`@workflow/world-local`){let r=stringifyEsmImportSpecifier(resolvePackageSourceFilePath(`src/internal/workflow/local-world-data-directory.ts`));return{moduleImportSpecifier:resolvePackageCompiledFilePath(`src/compiled/${e}/index.js`),extraImportLines:`
import { resolveLocalWorkflowWorldDataDirectory } from ${r};`.trimStart().split(`
`),runtimeImports:`getWorld, setWorld`,createWorldSource:`const workflowWorld = await workflowWorldModule.createWorld({
  dataDir: resolveLocalWorkflowWorldDataDirectory(process.cwd()),
});`}}return e===`@workflow/world-vercel`?{moduleImportSpecifier:resolvePackageCompiledFilePath(`src/compiled/${e}/index.js`),extraImportLines:[],runtimeImports:`getWorld, setWorld`,createWorldSource:`
const workflowWorld = await workflowWorldModule.createWorld({
  headers: { "User-Agent": ${JSON.stringify(buildPackageUserAgent())} },
});`.trimStart()}:{moduleImportSpecifier:e,extraImportLines:[],runtimeImports:`createWorldFromModule, getWorld, setWorld`,createWorldSource:`const workflowWorld = await createWorldFromModule(workflowWorldModule);`}}function createWorkflowWorldPluginSource(r){let i=resolveWorkflowWorldWiring(getWorldImport({WORKFLOW_TARGET_WORLD:r.configuredWorld??r.defaultWorld})),a=resolvePackageCompiledFilePath(`src/compiled/@workflow/core/runtime.js`),o=resolvePackageSourceFilePath(`src/internal/workflow/validate-world.ts`);return[`// Generated by eve. Do not edit by hand.`,`import ${stringifyEsmImportSpecifier(r.compiledArtifactsBootstrapPath)};`,`import * as workflowWorldModule from ${stringifyEsmImportSpecifier(i.moduleImportSpecifier)};`,...i.extraImportLines,`import { ${i.runtimeImports} } from ${stringifyEsmImportSpecifier(a)};`,`import { validateWorkflowWorld } from ${stringifyEsmImportSpecifier(o)};`,``,i.createWorldSource,`validateWorkflowWorld({ packageName: ${JSON.stringify(r.configuredWorld)}, world: workflowWorld });`,`setWorld(workflowWorld);`,`await getWorld();`,`await workflowWorld.start?.();`,``,`export default function installWorkflowWorldPlugin() {}`,``].join(`
`)}function createDevelopmentWorkflowWorldPluginSource(e){if(!usesParentDevelopmentWorkflowWorld(e.configuredWorld))return createWorkflowWorldPluginSource({...e,defaultWorld:`local`});let r=resolvePackageCompiledFilePath(`src/compiled/@workflow/core/runtime.js`),i=resolvePackageSourceFilePath(`src/internal/workflow/development-world-client.ts`);return[`// Generated by eve. Do not edit by hand.`,`import ${stringifyEsmImportSpecifier(e.compiledArtifactsBootstrapPath)};`,`import { getWorld, setWorld } from ${stringifyEsmImportSpecifier(r)};`,`import { createDevelopmentWorkflowWorld } from ${stringifyEsmImportSpecifier(i)};`,``,`setWorld(createDevelopmentWorkflowWorld());`,`await getWorld();`,``,`export default function installDevelopmentWorkflowWorldPlugin() {}`,``].join(`
`)}function createInstrumentationPluginSource(e){return[`// Generated by eve. Do not edit by hand.`,`import * as instrumentationModule from ${stringifyEsmImportSpecifier(e.instrumentationPath)};`,`import { registerInstrumentationConfig } from ${stringifyEsmImportSpecifier(e.registerConfigPath)};`,``,`if (instrumentationModule.default != null) {`,`  registerInstrumentationConfig(instrumentationModule.default, { agentName: ${JSON.stringify(e.agentName)} });`,`}`,``,`// Default export satisfies the Nitro plugin contract so this file`,`// can be used directly as a Nitro plugin without a separate wrapper.`,`export default function installInstrumentationPlugin() {}`,``].join(`
`)}export{createCompiledArtifactsBootstrapSource,createDevelopmentWorkflowWorldPluginSource,createWorkflowWorldPluginSource,writeCompiledArtifactsFiles,writeDevelopmentCompiledArtifactsFiles};