"use strict";
/**
 * Report Builder — Composes the final ScanResult from processed findings.
 *
 * Encapsulates sorting, counting, suppressed-summary building, and result
 * object construction.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildScanResult = buildScanResult;
const summary_1 = require("./summary");
/**
 * Build the final ScanResult from postprocessed findings.
 */
function buildScanResult(input) {
    const { repoInfo, files, findings, suppressionResult, startTime, validationStats, showSuppressed, filterPipeline, cancelled, cancelReason, } = input;
    // Use the filtered findings (after suppression)
    const afterSuppression = suppressionResult.findings;
    // Sort by severity
    const sortedVulnerabilities = (0, summary_1.sortBySeverity)(afterSuppression);
    // Compute issue-mix counts (based on unsuppressed findings)
    const severityCounts = (0, summary_1.computeSeverityCounts)(sortedVulnerabilities);
    const categoryCounts = (0, summary_1.computeCategoryCounts)(sortedVulnerabilities);
    const hasBlockingIssues = severityCounts.critical > 0 || severityCounts.high > 0;
    // Build suppressed vulnerabilities summary (for --show-suppressed)
    const suppressedVulnerabilities = showSuppressed
        ? suppressionResult.suppressed.map(s => ({
            hash: s.suppression.hash,
            filePath: s.vulnerability.filePath,
            lineNumber: s.vulnerability.lineNumber,
            category: s.vulnerability.category,
            severity: s.vulnerability.severity,
            title: s.vulnerability.title,
            suppressionType: s.suppression.type,
            suppressionReason: s.suppression.reason,
            expires: s.suppression.expires,
        }))
        : undefined;
    return {
        repoName: repoInfo.name,
        repoUrl: repoInfo.url,
        branch: repoInfo.branch,
        filesScanned: files.length,
        filesSkipped: 0, // TODO: track skipped files
        vulnerabilities: sortedVulnerabilities,
        severityCounts,
        categoryCounts,
        hasBlockingIssues: cancelled ? false : hasBlockingIssues,
        scanDuration: Date.now() - startTime,
        timestamp: new Date().toISOString(),
        validationStats,
        suppressionStats: suppressionResult.suppressed.length > 0 || suppressionResult.expiredSuppressions > 0
            ? suppressionResult.stats
            : undefined,
        suppressedVulnerabilities,
        filterAuditTrail: filterPipeline?.isEnabled ? filterPipeline.getAuditTrail() : undefined,
        ...(cancelled ? { cancelled: true, cancelReason } : {}),
    };
}
//# sourceMappingURL=build-result.js.map