"use strict";
/**
 * Deduplication — Remove duplicate vulnerabilities.
 *
 * Handles standard dedup (same file, line, category) and cross-layer URL dedup
 * (e.g., Layer 1 sensitive_url + Layer 2 ai_pattern on same line).
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.deduplicateVulnerabilities = deduplicateVulnerabilities;
const parsed_file_1 = require("../shared/parsed-file");
/**
 * Remove duplicate vulnerabilities (same file, line, category)
 * Also handles cross-layer URL duplicates (sensitive_url + ai_pattern)
 */
function deduplicateVulnerabilities(vulnerabilities) {
    const seen = new Map();
    const urlDedupMap = new Map();
    for (const vuln of vulnerabilities) {
        // Special handling for URL duplicates across layers
        // (e.g., Layer 1 detects as sensitive_url, Layer 2 detects as ai_pattern on same line)
        // Route based on category, not lineContent regex — a sensitive_url finding is a URL
        // finding regardless of what scheme appears in the line content.
        if (vuln.category === 'sensitive_url' || vuln.category === 'ai_pattern') {
            // Create compound key that ignores category differences for URLs
            const urlKey = `${vuln.filePath}:${vuln.lineNumber}:url_finding`;
            const existing = urlDedupMap.get(urlKey);
            if (!existing) {
                urlDedupMap.set(urlKey, vuln);
            }
            else {
                // Keep Layer 1 (more specific) over Layer 2 AI pattern
                // Or keep higher severity
                if (vuln.layer < existing.layer ||
                    (0, parsed_file_1.severityRank)(vuln.severity) > (0, parsed_file_1.severityRank)(existing.severity)) {
                    urlDedupMap.set(urlKey, vuln);
                }
            }
            continue;
        }
        // Standard deduplication for non-URL findings
        const key = `${vuln.filePath}:${vuln.lineNumber}:${vuln.category}`;
        const existing = seen.get(key);
        // Keep the higher severity or higher confidence finding
        if (!existing) {
            seen.set(key, vuln);
        }
        else if ((0, parsed_file_1.severityRank)(vuln.severity) > (0, parsed_file_1.severityRank)(existing.severity)) {
            seen.set(key, vuln);
        }
        else if ((0, parsed_file_1.severityRank)(vuln.severity) === (0, parsed_file_1.severityRank)(existing.severity) &&
            (0, parsed_file_1.confidenceRank)(vuln.confidence) > (0, parsed_file_1.confidenceRank)(existing.confidence)) {
            seen.set(key, vuln);
        }
    }
    // Combine URL and non-URL findings
    return [...Array.from(seen.values()), ...Array.from(urlDedupMap.values())];
}
//# sourceMappingURL=dedup.js.map