"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareDenormToLegacyRow = compareDenormToLegacyRow;
exports.summarizeParity = summarizeParity;
const denormalize_1 = require("./denormalize");
const metricIdentity_1 = require("./metricIdentity");
function compareDenormToLegacyRow(row) {
    const { analytics, components } = (0, denormalize_1.denormalizeEvalResult)(row, 1);
    const mismatches = [];
    const grading = row.gradingResult ?? row.grading_result;
    const expectedPass = grading?.pass ? 1 : 0;
    if (analytics.gradingPass !== expectedPass) {
        mismatches.push({
            evalResultId: row.id,
            field: 'grading_pass',
            expected: expectedPass,
            actual: analytics.gradingPass,
        });
    }
    const isRedteam = Number(row.isRedteam ?? row.is_redteam ?? 0);
    if (isRedteam === 0) {
        const legacyKeys = new Set();
        for (const cr of grading?.componentResults ?? []) {
            const key = (0, metricIdentity_1.assertionRuntimeExtras)(cr.assertion)?.displayLabel?.trim() ||
                cr.assertion?.type?.trim() ||
                '';
            if (key) {
                legacyKeys.add(key);
            }
        }
        const denormKeys = new Set(analytics.metricKeys);
        if (legacyKeys.size !== denormKeys.size) {
            mismatches.push({
                evalResultId: row.id,
                field: 'metric_keys.length',
                expected: legacyKeys.size,
                actual: denormKeys.size,
            });
        }
    }
    const legacyComponentCount = grading?.componentResults?.length ?? 0;
    const failureReason = Number(row.failureReason ?? row.failure_reason ?? 0);
    const expectedComponents = failureReason === 2 ? components.length : legacyComponentCount > 0 ? legacyComponentCount : grading ? 1 : 0;
    if (components.length !== expectedComponents && failureReason !== 2) {
        mismatches.push({
            evalResultId: row.id,
            field: 'components.length',
            expected: expectedComponents,
            actual: components.length,
        });
    }
    return { ok: mismatches.length === 0, mismatches };
}
function summarizeParity(results) {
    const total = results.length;
    const failed = results.filter((r) => !r.ok).length;
    return {
        total,
        failed,
        mismatchRate: total === 0 ? 0 : failed / total,
    };
}
//# sourceMappingURL=parity.js.map