import { existsSync, readdirSync, rmSync, statSync } from 'node:fs';
import { join, resolve } from 'node:path';
export function cleanupEvalResults(args) {
    const resultsDir = resolve(args.resultsDir);
    const candidates = listEvalResultDirs(resultsDir)
        .filter((candidate) => !args.before || candidate.modifiedAtMs < args.before.getTime())
        .sort((left, right) => left.modifiedAtMs - right.modifiedAtMs);
    const dryRun = args.dryRun ?? true;
    const removed = [];
    if (!dryRun) {
        for (const candidate of candidates) {
            rmSync(candidate.path, { recursive: true, force: true });
            removed.push(candidate);
        }
    }
    return {
        resultsDir,
        before: args.before?.toISOString(),
        dryRun,
        candidates,
        removed,
    };
}
export function listEvalResultDirs(resultsDir) {
    const resolved = resolve(resultsDir);
    if (!existsSync(resolved)) {
        return [];
    }
    return readdirSync(resolved, { withFileTypes: true })
        .filter((entry) => entry.isDirectory())
        .map((entry) => {
        const path = join(resolved, entry.name);
        const stat = statSync(path);
        return {
            path,
            name: entry.name,
            modifiedAt: stat.mtime.toISOString(),
            modifiedAtMs: stat.mtimeMs,
        };
    });
}
//# sourceMappingURL=cleanup.js.map