"use strict";
/**
 * Finding Grouping Logic
 * Groups and sorts vulnerabilities for workflow-friendly output
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.THEME_CONFIG = void 0;
exports.getRiskTheme = getRiskTheme;
exports.groupByTheme = groupByTheme;
exports.limitPerGroup = limitPerGroup;
exports.sortBySeverity = sortBySeverity;
exports.getBlockingIssues = getBlockingIssues;
exports.getActionableIssues = getActionableIssues;
/**
 * Map categories to risk themes
 */
function getRiskTheme(category) {
    switch (category) {
        case 'hardcoded_secret':
        case 'high_entropy_string':
        case 'sensitive_url':
            return 'secrets';
        case 'sql_injection':
        case 'xss':
        case 'command_injection':
        case 'dangerous_function':
            return 'injection';
        case 'missing_auth':
        case 'security_bypass':
            return 'auth';
        case 'ai_pattern':
        case 'ai_prompt_injection':
        case 'ai_unsafe_execution':
        case 'ai_overpermissive_tool':
            return 'ai';
        case 'insecure_config':
        case 'cors_misconfiguration':
        case 'root_container':
        case 'dangerous_file':
        case 'weak_crypto':
            return 'config';
        case 'data_exposure':
        case 'sensitive_variable':
            return 'data';
        default:
            return 'other';
    }
}
/**
 * Theme display names and icons
 */
exports.THEME_CONFIG = {
    secrets: { name: 'Secrets & Credentials', icon: '🔑', priority: 1 },
    injection: { name: 'Injection Vulnerabilities', icon: '💉', priority: 2 },
    auth: { name: 'Authentication Issues', icon: '🔒', priority: 3 },
    ai: { name: 'AI Security', icon: '🤖', priority: 4 },
    config: { name: 'Configuration Issues', icon: '⚙️', priority: 5 },
    data: { name: 'Data Exposure', icon: '📊', priority: 6 },
    other: { name: 'Other Issues', icon: '⚠️', priority: 7 },
};
/**
 * Severity sort order (higher = more severe)
 */
const SEVERITY_ORDER = {
    critical: 5,
    high: 4,
    medium: 3,
    low: 2,
    info: 1,
};
/**
 * Group vulnerabilities by risk theme
 */
function groupByTheme(vulnerabilities) {
    // Group by theme
    const groups = new Map();
    for (const vuln of vulnerabilities) {
        const theme = getRiskTheme(vuln.category);
        if (!groups.has(theme)) {
            groups.set(theme, []);
        }
        groups.get(theme).push(vuln);
    }
    // Convert to array and sort
    const result = [];
    for (const [theme, findings] of groups) {
        // Sort findings within group: severity desc, then confidence desc
        findings.sort((a, b) => {
            const severityDiff = SEVERITY_ORDER[b.severity] - SEVERITY_ORDER[a.severity];
            if (severityDiff !== 0)
                return severityDiff;
            const confidenceOrder = { high: 3, medium: 2, low: 1 };
            return confidenceOrder[b.confidence] - confidenceOrder[a.confidence];
        });
        // Count severities
        const severityCounts = {
            critical: 0,
            high: 0,
            medium: 0,
            low: 0,
            info: 0,
        };
        for (const f of findings) {
            severityCounts[f.severity]++;
        }
        const config = exports.THEME_CONFIG[theme];
        result.push({
            theme,
            themeName: config.name,
            themeIcon: config.icon,
            findings,
            severityCounts,
        });
    }
    // Sort groups by theme priority
    result.sort((a, b) => exports.THEME_CONFIG[a.theme].priority - exports.THEME_CONFIG[b.theme].priority);
    return result;
}
/**
 * Limit findings per group to avoid overwhelming output
 */
function limitPerGroup(groups, maxPerGroup = 10) {
    return groups.map(group => ({
        ...group,
        findings: group.findings.slice(0, maxPerGroup),
    }));
}
/**
 * Get findings sorted by severity across all groups
 */
function sortBySeverity(vulnerabilities) {
    return [...vulnerabilities].sort((a, b) => {
        const severityDiff = SEVERITY_ORDER[b.severity] - SEVERITY_ORDER[a.severity];
        if (severityDiff !== 0)
            return severityDiff;
        const confidenceOrder = { high: 3, medium: 2, low: 1 };
        return confidenceOrder[b.confidence] - confidenceOrder[a.confidence];
    });
}
/**
 * Filter to only blocking issues (critical/high)
 */
function getBlockingIssues(vulnerabilities) {
    return vulnerabilities.filter(v => v.severity === 'critical' || v.severity === 'high');
}
/**
 * Filter to actionable issues (medium and above)
 */
function getActionableIssues(vulnerabilities) {
    return vulnerabilities.filter(v => v.severity === 'critical' || v.severity === 'high' || v.severity === 'medium');
}
//# sourceMappingURL=grouping.js.map