/**
 * Assemble one gate's row from its cells.
 *
 * Separated from the report's own module because the two do different work:
 * that one reads the world, this one shapes what was read. Everything here is
 * pure — the same inputs give the same row, which is what makes the byte-
 * identical property testable without touching a filesystem.
 * @module cli/gate-report-assemble
 */
import { buildCell } from "./gate-report-cells.js";
/**
 * A gate-level `run` override from the settings file.
 * @param gates - The gates block
 * @param id - Gate id
 * @returns The task, or null
 */
function projectLevelTask(gates, id) {
    const gate = gates[id];
    if (typeof gate !== "object" || gate === null)
        return null;
    const run = Reflect.get(gate, "run");
    return typeof run === "string" ? run : null;
}
/**
 * Invert the static job table so a gate can name its CI job.
 * @param registry - The shipped registry
 * @returns Gate id -> job id
 */
export function invertJobTable(registry) {
    // A gate may have several provers and exactly one job whose name IS its
    // label — the job a ruleset matches — and that is the one this map must
    // name. Secondary provers are excluded by the shipped list rather than by
    // declaration order: order produced the right answer and was a trap, since
    // reordering the table would silently change which job a gate reports as its
    // own with nothing to notice.
    const secondary = new Set(registry.SECONDARY_PROVER_JOBS ?? []);
    // The reverse survives for the case the list does not cover: two PRIMARY
    // jobs naming one gate is a defect the façade suite fails on, and until it
    // is fixed the first declaration still wins rather than the last.
    return new Map(Object.entries(registry.QUALITY_JOB_GATES)
        .filter(([job]) => !secondary.has(job))
        .map(([job, gate]) => [gate, job])
        .reverse());
}
/**
 * Build one gate's row.
 * @param context - Report-wide cell inputs
 * @param moments - Per-moment resolution results, in axis order
 * @param id - Gate id
 * @param jobForGate - Gate id -> `quality.yml` job
 * @returns The row
 */
export function buildRow(context, moments, id, jobForGate) {
    const definition = context.registry.REGISTRY[id];
    const mergeIndex = moments.findIndex(entry => entry.moment === context.mergeMoment);
    const qualityJob = jobForGate.get(id) ?? null;
    const cells = moments.map(moment => buildCell(context, moment, id, qualityJob));
    return {
        id,
        label: definition?.label ?? id,
        summary: definition?.summary ?? "",
        legalMoments: [...(definition?.moments ?? [])].sort((left, right) => left.localeCompare(right)),
        defaultTask: definition?.task ?? null,
        taskAt: { ...definition?.taskAt },
        projectTask: projectLevelTask(context.gates, id),
        mayRewrite: definition?.mayRewrite === true,
        costly: definition?.costly === true,
        interceptor: context.registry.INTERCEPTORS[id] ?? null,
        qualityJob,
        moments: cells,
        merge: context.merge({
            expectedContext: cells[mergeIndex]?.expectedContext ?? null,
            task: cells[mergeIndex]?.task ?? null,
            label: definition?.label ?? id,
        }),
    };
}
//# sourceMappingURL=gate-report-assemble.js.map