"use strict";
/**
 * Postprocessing Pipeline — Runs the full postprocessing chain on findings.
 *
 * Dedup → context adjustments → contradiction resolution → suppression.
 * Records audit decisions for each sub-step via FilterPipeline.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.MAX_VALIDATION_CANDIDATES_PER_FILE = exports.capValidationCandidatesPerFile = exports.resolveContradictions = exports.aggregateNoisyFindings = exports.deduplicateVulnerabilities = void 0;
exports.postprocessFindings = postprocessFindings;
const dedup_1 = require("./dedup");
const context_adjustments_1 = require("./filtering/context-adjustments");
const contradictions_1 = require("./contradictions");
const suppression_1 = require("./suppression");
const fid = (v) => `${v.filePath}:${v.lineNumber}:${v.category}`;
/**
 * Run the full postprocessing chain on findings.
 *
 * Pipeline: dedup → context adjustments → contradiction resolution → suppression
 */
function postprocessFindings(input) {
    const { findings, files, middlewareConfig, projectPath, filterPipeline, } = input;
    // 1. Deduplicate vulnerabilities
    const uniqueVulnerabilities = (0, dedup_1.deduplicateVulnerabilities)(findings);
    if (filterPipeline.isEnabled) {
        const uniqueIds = new Set(uniqueVulnerabilities.map(fid));
        for (const v of findings) {
            if (!uniqueIds.has(fid(v))) {
                filterPipeline.record(fid(v), { stage: 'deduplication', action: 'deduplicated', reason: 'Duplicate finding removed' });
            }
        }
    }
    // 2. Apply file context-based severity adjustments (Phase 2b)
    const contextAdjustedVulnerabilities = (0, context_adjustments_1.applyContextAdjustments)(uniqueVulnerabilities);
    if (filterPipeline.isEnabled) {
        for (let i = 0; i < uniqueVulnerabilities.length; i++) {
            const before = uniqueVulnerabilities[i];
            const after = contextAdjustedVulnerabilities[i];
            if (before.severity !== after.severity) {
                filterPipeline.record(fid(after), { stage: 'global_context', action: 'downgraded', reason: after.validationNotes || 'Context-based severity downgrade', originalSeverity: before.severity, newSeverity: after.severity });
            }
        }
    }
    // 3. Resolve contradictions
    const resolvedVulnerabilities = (0, contradictions_1.resolveContradictions)(contextAdjustedVulnerabilities, middlewareConfig);
    if (filterPipeline.isEnabled) {
        const resolvedIds = new Set(resolvedVulnerabilities.map(fid));
        for (const v of contextAdjustedVulnerabilities) {
            if (!resolvedIds.has(fid(v))) {
                filterPipeline.record(fid(v), { stage: 'contradiction_resolution', action: 'dismissed', reason: 'Removed by contradiction resolution' });
            }
        }
        for (let i = 0; i < contextAdjustedVulnerabilities.length; i++) {
            const before = contextAdjustedVulnerabilities[i];
            const after = resolvedVulnerabilities.find(v => fid(v) === fid(before));
            if (after && before.severity !== after.severity) {
                filterPipeline.record(fid(after), { stage: 'contradiction_resolution', action: 'downgraded', reason: 'Downgraded by contradiction resolution', originalSeverity: before.severity, newSeverity: after.severity });
            }
        }
    }
    // 4. Apply suppressions (inline comments + config file)
    const resolvedProjectPath = projectPath || process.cwd();
    const suppressionManager = new suppression_1.SuppressionManager({ projectPath: resolvedProjectPath });
    const suppressionResult = suppressionManager.applySuppressions(resolvedVulnerabilities, files);
    if (filterPipeline.isEnabled) {
        for (const s of suppressionResult.suppressed) {
            filterPipeline.record(fid(s.vulnerability), { stage: 'user_suppression', action: 'suppressed', reason: s.suppression.reason || `Suppressed by ${s.suppression.type}` });
        }
    }
    return {
        findings: suppressionResult.findings,
        suppressionResult,
    };
}
// Re-export sub-modules for direct access
var dedup_2 = require("./dedup");
Object.defineProperty(exports, "deduplicateVulnerabilities", { enumerable: true, get: function () { return dedup_2.deduplicateVulnerabilities; } });
var aggregation_1 = require("./aggregation");
Object.defineProperty(exports, "aggregateNoisyFindings", { enumerable: true, get: function () { return aggregation_1.aggregateNoisyFindings; } });
var contradictions_2 = require("./contradictions");
Object.defineProperty(exports, "resolveContradictions", { enumerable: true, get: function () { return contradictions_2.resolveContradictions; } });
var validation_cap_1 = require("./validation-cap");
Object.defineProperty(exports, "capValidationCandidatesPerFile", { enumerable: true, get: function () { return validation_cap_1.capValidationCandidatesPerFile; } });
Object.defineProperty(exports, "MAX_VALIDATION_CANDIDATES_PER_FILE", { enumerable: true, get: function () { return validation_cap_1.MAX_VALIDATION_CANDIDATES_PER_FILE; } });
//# sourceMappingURL=index.js.map