import{join,relative}from"node:path";import{readdir}from"node:fs/promises";import{loadAuthoredModuleNamespace}from"#internal/authored-module-loader.js";const EVAL_FILE_SUFFIX=`.eval.ts`;async function discoverEvalFiles(n){let r=join(n,`evals`),i=[];try{await collectEvalFiles(r,i)}catch(e){if(isNoEntryError(e))return[];throw e}return i.sort((e,n)=>relative(r,e).localeCompare(relative(r,n))),i}function deriveEvalId(n,r){let a=relative(join(n,`evals`),r).split(/[\\/]/u).join(`/`);return a.endsWith(EVAL_FILE_SUFFIX)?a.slice(0,-8):a}async function importEvalFile(e,t){let n=(await loadAuthoredModuleNamespace(t)).default;if(!isEveEvalDefinition(n))throw Error(`Eval file "${t}" does not export a valid EveEval as its default export. Use defineEval() to create the eval.`);return{...n,id:deriveEvalId(e,t)}}async function discoverAndImportEvals(e,t){let n=await discoverEvalFiles(e);if(n.length===0)return[];let r=[];for(let i of n){let n=await importEvalFile(e,i);(t===void 0||t.length===0||t.includes(n.id))&&r.push(n)}return r}function isEveEvalDefinition(e){return typeof e==`object`&&!!e&&`_tag`in e&&e._tag===`EveEval`}async function collectEvalFiles(t,r){let a=await readdir(t,{withFileTypes:!0});for(let n of a){let a=join(t,n.name);n.isDirectory()?await collectEvalFiles(a,r):n.isFile()&&n.name.endsWith(EVAL_FILE_SUFFIX)&&r.push(a)}}function isNoEntryError(e){return typeof e==`object`&&!!e&&`code`in e&&e.code===`ENOENT`}export{discoverAndImportEvals,discoverEvalFiles,importEvalFile};