// engine/auditor-security/performance-checks.ts — N+1, sync I/O, spread-in-loop checks
export function checkPerformance(file, content, lines, ext) {
    const findings = [];
    if (/for\s*\(.*\)\s*\{[\s\S]*?(?:await|query|find|fetch|get)\s*\(/.exec(content)) {
        findings.push({
            file,
            category: 'performance',
            severity: 'warning',
            rule: 'n-plus-one',
            message: 'Possible N+1 query pattern: database/API call inside a loop.',
            suggestion: 'Batch the queries or use eager loading to reduce round trips.',
        });
    }
    if (['.ts', '.js', '.mjs'].includes(ext)) {
        if (/readFileSync|writeFileSync|readdirSync|statSync|existsSync/.exec(content)) {
            if (!file.includes('config') && !file.includes('setup') && !file.includes('init')) {
                findings.push({
                    file,
                    category: 'performance',
                    severity: 'warning',
                    rule: 'sync-io',
                    message: 'Synchronous I/O operation detected. This blocks the event loop.',
                    suggestion: 'Use async/await versions: readFile, writeFile, readdir, stat.',
                });
            }
        }
    }
    for (let i = 0; i < lines.length; i++) {
        const loopLine = lines[i];
        /* v8 ignore next 2 */
        if (loopLine === undefined) {
            continue;
        }
        if (/for\s*\(/.exec(loopLine) || /\.forEach\s*\(/.exec(loopLine)) {
            for (let j = i; j < Math.min(i + 10, lines.length); j++) {
                const spreadLine = lines[j];
                /* v8 ignore next 3 */
                if (spreadLine === undefined) {
                    continue;
                }
                if (/\.\.\.\w+/.exec(spreadLine)) {
                    findings.push({
                        file,
                        line: j + 1,
                        category: 'performance',
                        severity: 'info',
                        rule: 'spread-in-loop',
                        message: 'Object/array spread inside a loop can cause O(n^2) memory allocation.',
                        suggestion: 'Consider using Object.assign() or building the result incrementally.',
                    });
                    break;
                }
            }
        }
    }
    return findings;
}
//# sourceMappingURL=performance-checks.js.map