"use strict";
/**
 * VS Code Diagnostic Formatter
 * Formats scan results as LSP diagnostic format for VS Code integration
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiagnosticTag = exports.DiagnosticSeverity = void 0;
exports.formatDiagnostic = formatDiagnostic;
exports.formatDiagnosticsByFile = formatDiagnosticsByFile;
exports.generateCodeAction = generateCodeAction;
exports.formatForProblemsPanel = formatForProblemsPanel;
/**
 * LSP Diagnostic Severity
 * https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#diagnosticSeverity
 */
var DiagnosticSeverity;
(function (DiagnosticSeverity) {
    DiagnosticSeverity[DiagnosticSeverity["Error"] = 1] = "Error";
    DiagnosticSeverity[DiagnosticSeverity["Warning"] = 2] = "Warning";
    DiagnosticSeverity[DiagnosticSeverity["Information"] = 3] = "Information";
    DiagnosticSeverity[DiagnosticSeverity["Hint"] = 4] = "Hint";
})(DiagnosticSeverity || (exports.DiagnosticSeverity = DiagnosticSeverity = {}));
/**
 * LSP Diagnostic Tag
 */
var DiagnosticTag;
(function (DiagnosticTag) {
    DiagnosticTag[DiagnosticTag["Unnecessary"] = 1] = "Unnecessary";
    DiagnosticTag[DiagnosticTag["Deprecated"] = 2] = "Deprecated";
})(DiagnosticTag || (exports.DiagnosticTag = DiagnosticTag = {}));
/**
 * Map vulnerability severity to LSP diagnostic severity
 */
function mapSeverity(severity) {
    switch (severity) {
        case 'critical':
        case 'high':
            return DiagnosticSeverity.Error;
        case 'medium':
            return DiagnosticSeverity.Warning;
        case 'low':
            return DiagnosticSeverity.Information;
        case 'info':
            return DiagnosticSeverity.Hint;
        default:
            return DiagnosticSeverity.Information;
    }
}
/**
 * Create range for a finding (line-based, full line)
 */
function createRange(lineNumber, lineContent) {
    const line = lineNumber - 1; // LSP is 0-indexed
    const endChar = lineContent ? lineContent.length : 1000;
    return {
        start: { line, character: 0 },
        end: { line, character: endChar },
    };
}
/**
 * Format vulnerability as LSP diagnostic
 */
function formatDiagnostic(finding, fileUri) {
    const severity = mapSeverity(finding.severity);
    const severityLabel = finding.severity.toUpperCase();
    // Build message with fix suggestion
    let message = `[${severityLabel}] ${finding.title}\n\n${finding.description}`;
    if (finding.suggestedFix) {
        message += `\n\n💡 Fix: ${finding.suggestedFix}`;
    }
    const diagnostic = {
        range: createRange(finding.lineNumber, finding.lineContent),
        severity,
        code: finding.category,
        source: 'oculum',
        message,
    };
    // Add validation notes if available
    if (finding.validationNotes) {
        diagnostic.relatedInformation = [{
                location: {
                    uri: fileUri || `file://${finding.filePath}`,
                    range: createRange(finding.lineNumber),
                },
                message: `AI Validation: ${finding.validationNotes}`,
            }];
    }
    return diagnostic;
}
/**
 * Format vulnerabilities as diagnostics grouped by file
 */
function formatDiagnosticsByFile(vulnerabilities, baseUri) {
    const byFile = new Map();
    for (const finding of vulnerabilities) {
        const uri = baseUri
            ? `${baseUri}/${finding.filePath}`
            : `file://${finding.filePath}`;
        if (!byFile.has(uri)) {
            byFile.set(uri, []);
        }
        byFile.get(uri).push(formatDiagnostic(finding, uri));
    }
    // Sort diagnostics within each file by line number
    const result = [];
    for (const [uri, diagnostics] of byFile) {
        diagnostics.sort((a, b) => a.range.start.line - b.range.start.line);
        result.push({ uri, diagnostics });
    }
    // Sort files alphabetically
    result.sort((a, b) => a.uri.localeCompare(b.uri));
    return result;
}
/**
 * Generate code action for a finding (if fix is available)
 */
function generateCodeAction(finding, fileUri) {
    if (!finding.suggestedFix)
        return null;
    // For now, just provide a "learn more" action
    // In the future, we could generate actual code fixes
    return {
        title: `💡 Fix: ${finding.suggestedFix.slice(0, 50)}${finding.suggestedFix.length > 50 ? '...' : ''}`,
        kind: 'quickfix',
        diagnostics: [formatDiagnostic(finding, fileUri)],
        isPreferred: true,
    };
}
/**
 * Format for VS Code Problems panel (simplified text format)
 */
function formatForProblemsPanel(vulnerabilities) {
    const byFile = new Map();
    for (const finding of vulnerabilities) {
        if (!byFile.has(finding.filePath)) {
            byFile.set(finding.filePath, []);
        }
        byFile.get(finding.filePath).push(finding);
    }
    let output = '';
    for (const [filePath, findings] of byFile) {
        output += `\n${filePath}\n`;
        for (const finding of findings) {
            const icon = finding.severity === 'critical' || finding.severity === 'high' ? '❌' :
                finding.severity === 'medium' ? '⚠️' : 'ℹ️';
            output += `  ${icon} Line ${finding.lineNumber}: ${finding.title}\n`;
        }
    }
    return output.trim();
}
//# sourceMappingURL=vscode-diagnostic.js.map