import { messages } from '../../../core/messages/index.js';
import { createLineLookup, maskRustComments } from '../../../shared/helpers/index.js';
import { criarAnalista, criarOcorrencia } from '../../../types/index.js';
const disableEnv = process.env.TUTANUS_DISABLE_PLUGIN_RUST === '1';
function warn(message, relPath, line, nivel) {
    return criarOcorrencia({
        relPath,
        mensagem: message,
        linha: line,
        nivel: nivel ?? messages.SeverityNiveis.warning,
        origem: messages.AnalystOrigens.rust,
        tipo: messages.AnalystTipos.rust
    });
}
function collectBestPractices(src, relPath) {
    const ocorrencias = [];
    const lineOf = createLineLookup(src).lineAt;
    const scan = maskRustComments(src);
    for (const match of scan.matchAll(/\bprintln!\s*\(/g)) {
        const line = lineOf(match.index);
        if (!relPath.includes('src/bin/') && !relPath.includes('examples/') && !relPath.includes('tests/')) {
            ocorrencias.push(warn(messages.RustMensagens.printlnInLib, relPath, line));
        }
    }
    for (const match of scan.matchAll(/(?:if|match|let|return|&&|\|\|)\s+.*[=!]=\s*(\d{4,})\b/g)) {
        const line = lineOf(match.index);
        ocorrencias.push(warn(messages.RustMensagens.magicNumber, relPath, line, messages.SeverityNiveis.suggestion));
    }
    const funcBlocks = src.matchAll(/(?:^|\n)\s*fn\s+\w+.*?\{/gs);
    for (const funcBlock of funcBlocks) {
        const startIdx = (funcBlock.index ?? 0) + (funcBlock[0]?.length ?? 0);
        let braceCount = 1;
        let endIdx = startIdx;
        for (let i = startIdx; i < src.length && braceCount > 0; i++) {
            if (src[i] === '{')
                braceCount++;
            if (src[i] === '}')
                braceCount--;
            endIdx = i;
        }
        const funcBody = src.slice(startIdx, endIdx);
        const funcLines = funcBody.split('\n').length;
        if (funcLines > 80) {
            const line = lineOf(funcBlock.index ?? 0);
            ocorrencias.push(warn(messages.RustMensagens.longFunction, relPath, line));
        }
    }
    const declaredVars = new Set();
    for (const match of scan.matchAll(/\blet\s+(?:mut\s+)?(\w+)\s*=/g)) {
        const varName = match[1];
        if (varName.startsWith('_'))
            continue;
        if (declaredVars.has(varName)) {
            const line = lineOf(match.index);
            ocorrencias.push(warn(`Variable '${varName}' shadows previous declaration; consider renaming`, relPath, line, messages.SeverityNiveis.info));
        }
        declaredVars.add(varName);
    }
    return ocorrencias;
}
export const analistaRustBoasPraticas = criarAnalista({
    nome: 'analista-rust-boas-praticas',
    categoria: 'boas-praticas',
    descricao: 'Detecta desvios de boas práticas em código Rust.',
    global: false,
    test: (relPath) => /\.(rs)$/i.test(relPath),
    aplicar: async (src, relPath) => {
        if (disableEnv)
            return null;
        const msgs = collectBestPractices(src, relPath);
        return msgs.length ? msgs : null;
    }
});
//# sourceMappingURL=analyst-best-practices.js.map