// engine/project-scanner/quality-baseline.ts — Capture quality baseline metrics
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { glob } from 'glob';
async function countTestFiles(projectPath) {
    try {
        const files = await glob('**/*.{test,spec}.{ts,js,tsx,jsx,py,go,rs}', {
            cwd: projectPath,
            ignore: ['**/node_modules/**', '**/dist/**', '**/.git/**'],
        });
        return files.length;
    }
    catch {
        return 0;
    }
}
async function readLintWarnings(projectPath) {
    const reportPaths = [
        'eslint-report.json',
        '.eslint-output.json',
        'lint-results.json',
        'reports/lint.json',
    ];
    for (const rel of reportPaths) {
        try {
            const raw = await readFile(join(projectPath, rel), 'utf-8');
            const parsed = JSON.parse(raw);
            if (Array.isArray(parsed)) {
                return parsed.reduce((sum, file) => {
                    if (typeof file === 'object' && file !== null && 'warningCount' in file) {
                        return sum + (Number(file.warningCount) || 0);
                    }
                    return sum;
                }, 0);
            }
        }
        catch {
            // try next
        }
    }
    return null;
}
async function readCoveragePercent(projectPath) {
    const coveragePaths = [
        'coverage/coverage-summary.json',
        'coverage-summary.json',
        '.nyc_output/coverage-summary.json',
    ];
    for (const rel of coveragePaths) {
        try {
            const raw = await readFile(join(projectPath, rel), 'utf-8');
            const parsed = JSON.parse(raw);
            const pct = parsed.total?.statements?.pct ?? parsed.total?.lines?.pct ?? null;
            if (pct !== null && typeof pct === 'number') {
                return pct;
            }
        }
        catch {
            // try next
        }
    }
    return null;
}
export async function captureBaseline(projectPath) {
    const [testCount, lintWarnings, coveragePercent] = await Promise.all([
        countTestFiles(projectPath),
        readLintWarnings(projectPath),
        readCoveragePercent(projectPath),
    ]);
    return { testCount, lintWarnings, coveragePercent };
}
//# sourceMappingURL=quality-baseline.js.map