"use strict";
// Real-Time Guardian — Streaming code analysis via WebSocket
// Detects: SQL injection, hardcoded secrets, XSS, null refs, build failures
Object.defineProperty(exports, "__esModule", { value: true });
exports.analyzeLine = analyzeLine;
exports.recordFeedback = recordFeedback;
exports.getStats = getStats;
let totalAlerts = 0;
let falsePositives = 0;
let userDismissed = new Map();
let mode = 'full';
const SQL_INJECTION_PATTERN = /(\bselect\b.*\bfrom\b.*\+|execute\s*\(\s*['"]\s*\+|\.query\s*\(\s*['"].*\+)/i;
const HARDCODED_SECRET = /(api[_-]?key|secret|password|token)\s*[:=]\s*['"][A-Za-z0-9_\-]{8,}['"]/i;
const NULL_REF_PATTERN = /(\w+)\.(\w+)\s*\(\)(?!.*\?\.)/;
const XSS_PATTERN = /innerHTML\s*=|dangerouslySetInnerHTML|document\.write\s*\(/i;
function analyzeLine(code, lineNumber, communityPrior) {
    const alerts = [];
    if (SQL_INJECTION_PATTERN.test(code)) {
        alerts.push({ type: 'critical', line: lineNumber, message: 'Potential SQL injection', confidence: 0.95 });
    }
    if (HARDCODED_SECRET.test(code)) {
        alerts.push({ type: 'critical', line: lineNumber, message: 'Hardcoded secret detected', confidence: 0.92 });
    }
    if (XSS_PATTERN.test(code)) {
        alerts.push({ type: 'critical', line: lineNumber, message: 'Potential XSS vector', confidence: 0.88 });
    }
    if (NULL_REF_PATTERN.test(code) && !code.includes('?.')) {
        alerts.push({ type: 'warning', line: lineNumber, message: 'Potential null reference', confidence: 0.75 });
    }
    if (communityPrior) {
        for (const [patternId, failRate] of communityPrior) {
            if (code.includes(patternId) && failRate > 0.7) {
                alerts.push({ type: 'predictive', line: lineNumber, message: `Pattern caused build failure ${Math.round(failRate * 100)}% of the time`, confidence: failRate, patternId });
            }
        }
    }
    return alerts.filter(a => {
        if (mode === 'critical-only' && a.type !== 'critical')
            return false;
        const dismissCount = userDismissed.get(a.message) || 0;
        if (dismissCount >= 3)
            return false;
        return true;
    });
}
function recordFeedback(alertMessage, wasHelpful) {
    totalAlerts++;
    if (!wasHelpful) {
        falsePositives++;
        const count = (userDismissed.get(alertMessage) || 0) + 1;
        userDismissed.set(alertMessage, count);
    }
    const fpr = totalAlerts > 0 ? falsePositives / totalAlerts : 0;
    if (fpr > 0.10)
        mode = 'critical-only';
    else if (fpr < 0.05 && mode === 'critical-only')
        mode = 'full';
}
function getStats() {
    return { totalAlerts, falsePositives, fpr: totalAlerts > 0 ? falsePositives / totalAlerts : 0, dismissed: Array.from(userDismissed.values()).reduce((a, b) => a + b, 0), mode };
}
//# sourceMappingURL=realtime-guardian.js.map