"use strict";
/**
 * Windsurf IDE Integration
 * Generates .windsurfrules format
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatWindsurfRules = formatWindsurfRules;
const grouping_1 = require("../grouping");
/**
 * Get security pattern rule for a category
 */
function getPatternRule(category) {
    const rules = {
        sql_injection: 'ALWAYS use parameterized queries for database operations.',
        xss: 'ALWAYS escape user input before inserting into HTML.',
        hardcoded_secret: 'NEVER hardcode secrets, API keys, or credentials in source code.',
        high_entropy_string: 'NEVER commit high-entropy strings that could be secrets.',
        missing_auth: 'ALWAYS add authentication middleware to API endpoints.',
        dangerous_function: 'NEVER use eval(), exec(), or similar functions with user input.',
        command_injection: 'ALWAYS sanitize input before passing to shell commands.',
        sensitive_url: 'NEVER hardcode localhost or internal URLs in production code.',
        insecure_config: 'ALWAYS review configuration files for security issues.',
        data_exposure: 'NEVER log or expose sensitive data in error messages.',
        weak_crypto: 'ALWAYS use strong cryptographic algorithms (avoid MD5, SHA1).',
        ai_prompt_injection: 'ALWAYS sanitize user input before including in AI prompts.',
        ai_unsafe_execution: 'NEVER execute code generated by AI without validation.',
    };
    return rules[category] || null;
}
/**
 * Format scan result as Windsurf rules
 *
 * @param result - The scan result to format
 * @returns Markdown string for .windsurfrules
 */
function formatWindsurfRules(result) {
    const { vulnerabilities, timestamp } = result;
    // Sort by severity
    const sorted = (0, grouping_1.sortBySeverity)(vulnerabilities);
    let md = '';
    // Header
    md += `# Oculum Security Rules\n\n`;
    md += `> Generated: ${timestamp}\n\n`;
    // No findings case
    if (vulnerabilities.length === 0) {
        md += `## Status\n\n`;
        md += `No security issues found.\n\n`;
        md += `*Run \`oculum scan --windsurf --clear\` to remove this file.*\n`;
        return md;
    }
    // Security Issues Section
    md += `## Security Issues\n\n`;
    md += `The following security issues were detected and need attention:\n\n`;
    // Group by file
    const byFile = new Map();
    for (const vuln of sorted) {
        const group = byFile.get(vuln.filePath) || [];
        group.push(vuln);
        byFile.set(vuln.filePath, group);
    }
    // List by file
    for (const [filePath, vulns] of byFile) {
        md += `### ${filePath}\n\n`;
        for (const vuln of vulns) {
            const severityEmoji = vuln.severity === 'critical' ? '!!' :
                vuln.severity === 'high' ? '!' :
                    vuln.severity === 'medium' ? '*' : '-';
            md += `- ${severityEmoji} **Line ${vuln.lineNumber}:** ${vuln.title}`;
            if (vuln.suggestedFix) {
                md += ` - ${vuln.suggestedFix}`;
            }
            md += '\n';
        }
        md += '\n';
    }
    // Code Patterns Section
    md += `## Code Patterns\n\n`;
    md += `Follow these security patterns when writing code:\n\n`;
    // Collect unique pattern rules
    const patternRules = new Set();
    for (const vuln of vulnerabilities) {
        const rule = getPatternRule(vuln.category);
        if (rule) {
            patternRules.add(rule);
        }
    }
    for (const rule of patternRules) {
        md += `- ${rule}\n`;
    }
    md += '\n';
    // Quick Reference
    md += `## Quick Reference\n\n`;
    md += `| Severity | Count | Action |\n`;
    md += `|----------|-------|--------|\n`;
    const { severityCounts } = result;
    if (severityCounts.critical > 0) {
        md += `| Critical | ${severityCounts.critical} | Fix immediately |\n`;
    }
    if (severityCounts.high > 0) {
        md += `| High | ${severityCounts.high} | Fix before commit |\n`;
    }
    if (severityCounts.medium > 0) {
        md += `| Medium | ${severityCounts.medium} | Review and fix |\n`;
    }
    if (severityCounts.low > 0) {
        md += `| Low | ${severityCounts.low} | Consider fixing |\n`;
    }
    if (severityCounts.info > 0) {
        md += `| Info | ${severityCounts.info} | For awareness |\n`;
    }
    md += '\n';
    // Footer
    md += `---\n\n`;
    md += `*Run \`oculum scan\` to verify fixes. Run \`oculum scan --windsurf --clear\` to remove this file.*\n`;
    return md;
}
//# sourceMappingURL=windsurf.js.map