"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComplexityAnalyzer = void 0;
class ComplexityAnalyzer {
    analyze(code, file) {
        const reports = [];
        const lines = code.split('\n');
        let currentFunction = '';
        let functionStart = 0;
        let depth = 0;
        for (let i = 0; i < lines.length; i++) {
            const line = lines[i];
            const funcMatch = line.match(/(?:function|const)\s+(\w+)/);
            if (funcMatch && (line.includes('{') || line.includes('=>'))) {
                currentFunction = funcMatch[1];
                functionStart = i + 1;
                depth = 1;
                continue;
            }
            if (currentFunction) {
                const opens = (line.match(/\{/g) || []).length;
                const closes = (line.match(/\}/g) || []).length;
                depth += opens - closes;
                if (depth <= 0) {
                    const funcBody = lines.slice(functionStart - 1, i + 1).join('\n');
                    const complexity = this.computeCyclomaticComplexity(funcBody);
                    const level = this.getLevel(complexity);
                    reports.push({
                        file,
                        function: currentFunction,
                        line: functionStart,
                        complexity,
                        level,
                        suggestion: this.getSuggestion(level, currentFunction, complexity),
                    });
                    currentFunction = '';
                    depth = 0;
                }
            }
        }
        return reports.sort((a, b) => b.complexity - a.complexity);
    }
    getHotspots(reports) {
        return reports.filter((r) => r.level === 'high' || r.level === 'medium');
    }
    getHeatmapData(reports) {
        const fileMap = new Map();
        for (const report of reports) {
            if (!fileMap.has(report.file)) {
                fileMap.set(report.file, { hotspots: 0, maxComplexity: 0 });
            }
            const data = fileMap.get(report.file);
            if (report.level !== 'low')
                data.hotspots++;
            data.maxComplexity = Math.max(data.maxComplexity, report.complexity);
        }
        return Array.from(fileMap.entries()).map(([file, data]) => ({
            file,
            hotspots: data.hotspots,
            maxComplexity: data.maxComplexity,
        }));
    }
    getStats(reports) {
        const high = reports.filter((r) => r.level === 'high').length;
        const medium = reports.filter((r) => r.level === 'medium').length;
        const low = reports.filter((r) => r.level === 'low').length;
        const avg = reports.length > 0
            ? Math.round(reports.reduce((a, r) => a + r.complexity, 0) / reports.length)
            : 0;
        return { total: reports.length, high, medium, low, avgComplexity: avg };
    }
    computeCyclomaticComplexity(code) {
        let complexity = 1;
        const branches = [
            /\bif\b/g,
            /\bfor\b/g,
            /\bwhile\b/g,
            /\bcase\b/g,
            /\bcatch\b/g,
            /\?\s*.*\s*:/g,
            /&&/g,
            /\|\|/g,
        ];
        for (const pattern of branches) {
            const matches = code.match(pattern);
            if (matches)
                complexity += matches.length;
        }
        return complexity;
    }
    getLevel(complexity) {
        if (complexity > 20)
            return 'high';
        if (complexity >= 10)
            return 'medium';
        return 'low';
    }
    getSuggestion(level, funcName, complexity) {
        switch (level) {
            case 'high':
                return `CRITICAL: '${funcName}' has complexity ${complexity}. Strongly consider splitting into smaller functions.`;
            case 'medium':
                return `WARNING: '${funcName}' has complexity ${complexity}. Consider refactoring if it grows further.`;
            default:
                return `OK: '${funcName}' has acceptable complexity (${complexity}).`;
        }
    }
}
exports.ComplexityAnalyzer = ComplexityAnalyzer;
//# sourceMappingURL=complexity-analyzer.js.map