// engine/data-projects-gc/gc-runner.ts — SPEC-581
// Orchestrates scan + optional deletion of data/projects/ garbage.
import { rm } from 'node:fs/promises';
import { scanProjectDirs } from './scanner.js';
/**
 * Run the GC pipeline:
 * 1. Scan data/projects/ for test-named, inactive, and orphaned directories.
 * 2. If apply=true, delete each candidate.
 * 3. Return a result summary.
 *
 * @param options - Run configuration (dataProjectsDir, apply, inactiveDays, …)
 */
export async function runGC(options) {
    const { dataProjectsDir, apply = false, inactiveDays = 90, registeredHashes, includeOrphans = false, } = options;
    const scanResult = await scanProjectDirs(dataProjectsDir, {
        inactiveDays,
        registeredHashes,
        includeOrphans,
    });
    const candidates = [
        ...scanResult.testNamed,
        ...scanResult.inactive,
        ...scanResult.orphaned,
    ];
    const deleted = [];
    const errors = [];
    if (apply) {
        for (const entry of candidates) {
            try {
                await rm(entry.fullPath, { recursive: true, force: true });
                deleted.push(entry.name);
            }
            catch (err) {
                /* v8 ignore next 4 -- deletion error: typically OS-level permission or race condition */
                errors.push(`${entry.name}: ${err instanceof Error ? err.message : String(err)}`);
            }
        }
    }
    return {
        dryRun: !apply,
        scannedTotal: scanResult.total,
        testNamed: scanResult.testNamed,
        inactive: scanResult.inactive,
        orphaned: scanResult.orphaned,
        candidateCount: candidates.length,
        deleted,
        errors,
        inactiveDays,
    };
}
//# sourceMappingURL=gc-runner.js.map