"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.runClaimVerification = runClaimVerification;
const evidenceContractBuilder_1 = require("../evidence/evidenceContractBuilder");
const deterministicClaimVerifier_1 = require("./deterministicClaimVerifier");
function isIdentityResolution(value) {
    return Boolean(value &&
        typeof value === 'object' &&
        !Array.isArray(value) &&
        value.version === 'identity_contract@1' &&
        typeof value.identityRefId === 'string');
}
function collectIdentityResolutions(envelopes = []) {
    const byId = new Map();
    for (const envelope of envelopes) {
        const meta = envelope.meta;
        if (!meta)
            continue;
        const candidate = meta.identityResolution;
        if (isIdentityResolution(candidate)) {
            byId.set(candidate.identityRefId, candidate);
            continue;
        }
        if (typeof meta.identityRefId === 'string' && typeof meta.identityStatus === 'string') {
            byId.set(meta.identityRefId, {
                version: 'identity_contract@1',
                identityRefId: meta.identityRefId,
                target: {
                    traceId: typeof meta.traceId === 'string' ? meta.traceId : 'unknown',
                    traceSide: meta.traceSide === 'current' || meta.traceSide === 'reference'
                        ? meta.traceSide
                        : 'unknown',
                    source: 'derived',
                },
                status: meta.identityStatus,
                processes: [],
                threads: [],
                warnings: Array.isArray(meta.identityWarnings) ? meta.identityWarnings.map(String) : [],
            });
        }
    }
    return Array.from(byId.values());
}
function supportLevelFromVerifierStatus(status, fallback) {
    switch (status) {
        case 'verified':
            return 'verified';
        case 'inference':
            return 'inference';
        case 'unsupported':
            return 'unsupported';
        case 'partial':
        case 'not_checked':
            return 'partial';
        default:
            return fallback;
    }
}
function applyVerifierSupportLevels(claimSupport, verification) {
    const byClaimId = new Map(verification.claimResults.map(result => [result.claimId, result.status]));
    return claimSupport.map(claim => {
        const supportLevel = supportLevelFromVerifierStatus(byClaimId.get(claim.claimId), claim.supportLevel);
        return supportLevel === claim.supportLevel ? claim : { ...claim, supportLevel };
    });
}
function runClaimVerification(input) {
    const evidenceContract = (0, evidenceContractBuilder_1.buildEvidenceContract)({
        conclusionContract: input.conclusionContract,
        dataEnvelopes: input.dataEnvelopes,
        comparisonReportSection: input.comparisonReportSection,
    });
    const claimVerificationResult = (0, deterministicClaimVerifier_1.runDeterministicClaimVerifier)({
        claimSupport: evidenceContract.claimSupport,
        policy: input.policy || 'record_only',
    });
    const claimSupport = applyVerifierSupportLevels(evidenceContract.claimSupport, claimVerificationResult);
    evidenceContract.claimSupport = claimSupport;
    return {
        evidenceContract,
        claimSupport,
        claimVerificationResult,
        identityResolutions: collectIdentityResolutions(input.dataEnvelopes),
    };
}
//# sourceMappingURL=claimVerificationRunner.js.map