import{join}from"node:path";import{toErrorMessage}from"#shared/errors.js";import{createHash}from"node:crypto";import{readFile}from"node:fs/promises";import{loadModuleBackedDefinition}from"#compiler/normalize-helpers.js";import{normalizeSandboxDefinition}from"#internal/authored-definition/sandbox.js";async function compileSandboxDefinition(e,t){let n=`Expected the sandbox export "${t.exportName??`default`}" from "${t.logicalPath}" to match the public Eve shape.`,r=normalizeSandboxDefinition(await loadModuleBackedDefinition({agentRoot:e,kind:`sandbox`,source:t}),n),o=r.revalidationKey===void 0?void 0:await resolveSandboxRevalidationKey({message:n,revalidationKey:r.revalidationKey,source:t});return{description:r.description,exportName:t.exportName,logicalPath:t.logicalPath,revalidationKey:o,sourceHash:await resolveSandboxSourceHash(e,t),sourceId:t.sourceId,sourceKind:`module`}}async function resolveSandboxRevalidationKey(e){let n;try{n=await e.revalidationKey()}catch(n){throw Error(`${e.message} Failed to execute the "revalidationKey" function from "${e.source.logicalPath}": ${toErrorMessage(n)}`)}if(typeof n!=`string`)throw Error(`${e.message} The "revalidationKey" function must return a string.`);if(n.trim().length===0)throw Error(`${e.message} The "revalidationKey" function must return a non-empty string.`);return n}async function resolveSandboxSourceHash(t,i){let a=await readFile(join(t,i.logicalPath));return createHash(`sha256`).update(a).digest(`hex`)}export{compileSandboxDefinition};