import{join}from"node:path";import{existsSync}from"node:fs";import{resolvePackageSourceFilePath}from"#internal/application/package.js";import{mkdir,writeFile}from"node:fs/promises";import{createCompiledModuleMapSource}from"#compiler/module-map.js";import{stringifyEsmImportSpecifier}from"#internal/application/import-specifier.js";async function writeCompiledArtifactsFiles(t){let a=join(t.outDir,`compiled-artifacts-bootstrap.mjs`),o=join(t.outDir,`compiled-artifacts-instrumentation.mjs`),s=resolveInstrumentationModule(t.compileResult.manifest.agentRoot);await mkdir(t.outDir,{recursive:!0}),await writeFile(a,await createCompiledArtifactsBootstrapSource({compileResult:t.compileResult,installModulePath:resolvePackageSourceFilePath(`src/runtime/loaders/bundled-artifacts.ts`),moduleMapPath:a,metadata:t.compileResult.metadata})),s!==void 0&&await writeFile(o,createInstrumentationPluginSource({agentName:t.compileResult.manifest.config.name,instrumentationPath:s,registerConfigPath:resolvePackageSourceFilePath(`src/harness/instrumentation-config.ts`)}));let c={bootstrapPath:a};return s!==void 0&&(c.instrumentationPluginPath=o,c.instrumentationSourcePath=s),c}const INSTRUMENTATION_EXTENSIONS=[`.ts`,`.mts`,`.js`,`.mjs`];function resolveInstrumentationModule(n){for(let r of INSTRUMENTATION_EXTENSIONS){let i=join(n,`instrumentation${r}`);if(existsSync(i))return i}}function stripCompiledModuleMapExports(e){return e.replace(/^export const moduleMap = /m,`const moduleMap = `).replace(/\nexport default moduleMap;\n?$/,`
`)}async function createCompiledArtifactsBootstrapSource(e){let t=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)};`,``,t,``,`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.`,`}`,``,`export async function __eveInstallCompiledArtifactsStep() {`,`  "use step";`,`  return null;`,`}`,``].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{writeCompiledArtifactsFiles};