"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runCoverageAcrossPacks = runCoverageAcrossPacks;
const languages_1 = require("../languages");
/**
 * Iterate active packs and invoke each one's
 * `capabilities.coverage.runTests`, returning per-pack outcome rows.
 * Packs without a `runTests` implementation surface as `'skipped'` so
 * the caller can still render the full active-pack matrix.
 *
 * Fail-fast semantics match the legacy `coverage` command: a
 * `failed` outcome stops the iteration (caller handles exit code).
 * `unavailable` and `skipped` keep the loop running because they mean
 * "no artifact this run" rather than "the test run blew up."
 */
async function runCoverageAcrossPacks(cwd, opts = {}) {
    const failFast = opts.failFast ?? true;
    const active = (0, languages_1.detectActiveLanguages)(cwd);
    const candidates = active.filter((p) => !opts.langFilter || p.id === opts.langFilter);
    const rows = [];
    for (const pack of candidates) {
        const provider = pack.capabilities?.coverage;
        if (!provider?.runTests) {
            const row = {
                pack: pack.id,
                status: 'skipped',
                durationMs: 0,
                artifact: null,
                reason: 'no runTests() implementation yet (pack coverage capability is read-only)',
            };
            rows.push(row);
            opts.onPackEnd?.(row);
            continue;
        }
        opts.onPackStart?.(pack.id);
        const outcome = await provider.runTests(cwd);
        let row;
        if (outcome.kind === 'success') {
            row = {
                pack: pack.id,
                status: 'success',
                durationMs: outcome.durationMs,
                artifact: outcome.artifact,
                reason: null,
            };
        }
        else if (outcome.kind === 'unavailable') {
            row = {
                pack: pack.id,
                status: 'unavailable',
                durationMs: 0,
                artifact: null,
                reason: outcome.reason,
            };
        }
        else {
            row = {
                pack: pack.id,
                status: 'failed',
                durationMs: outcome.durationMs,
                artifact: null,
                reason: outcome.reason,
            };
        }
        rows.push(row);
        opts.onPackEnd?.(row);
        if (row.status === 'failed' && failFast)
            break;
    }
    return { rows, activePacks: active };
}
//# sourceMappingURL=coverage-runner.js.map