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 isRustFile(relPath) {
    return /\.(rs)$/i.test(relPath);
}
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 collectGeneralRustIssues(src, relPath) {
    const ocorrencias = [];
    const lineOf = createLineLookup(src).lineAt;
    const scan = maskRustComments(src);
    const firstItem = scan.match(/^\s*(?:pub\s+)?(?:fn|mod|struct|enum|trait|impl)\s+\w+/m);
    if (firstItem) {
        const srcBefore = src.slice(0, firstItem.index ?? 0);
        if (!/\/\/!/.test(srcBefore) && !/\/\*!/.test(srcBefore)) {
            const line = lineOf(firstItem.index ?? 0);
            ocorrencias.push(warn('Module or crate missing documentation comment (//! or /*! */)', relPath, line, messages.SeverityNiveis.suggestion));
        }
    }
    for (const match of scan.matchAll(/^\s*(?:pub\s+)?fn\s+(\w+)\s*\(/gm)) {
        const fnName = match[1];
        if (/[A-Z]/.test(fnName)) {
            const line = lineOf(match.index);
            ocorrencias.push(warn(`Function '${fnName}' should follow snake_case convention`, relPath, line, messages.SeverityNiveis.suggestion));
        }
    }
    for (const match of scan.matchAll(/^\s*(?:pub\s+)?(?:struct|enum|trait|type)\s+(\w+)/gm)) {
        const typeName = match[1];
        if (/^[a-z_]/.test(typeName)) {
            const line = lineOf(match.index);
            ocorrencias.push(warn(`Type '${typeName}' should follow PascalCase convention`, relPath, line, messages.SeverityNiveis.suggestion));
        }
    }
    let maxDepth = 0;
    const lines = src.split('\n');
    for (const line of lines) {
        const indent = line.match(/^\s*/)?.[0].length ?? 0;
        const depth = Math.floor(indent / 4);
        maxDepth = Math.max(maxDepth, depth);
    }
    if (maxDepth > 8) {
        ocorrencias.push(warn(`Deep nesting detected (${maxDepth} levels); consider refactoring`, relPath, 1, messages.SeverityNiveis.suggestion));
    }
    return ocorrencias;
}
export const analistaRust = criarAnalista({
    nome: 'analista-rust',
    categoria: 'linguagem',
    descricao: 'Analisa código Rust em busca de convenções de nomenclatura e organização.',
    global: false,
    test: (relPath) => isRustFile(relPath),
    aplicar: async (src, relPath) => {
        if (disableEnv)
            return null;
        const msgs = collectGeneralRustIssues(src, relPath);
        return msgs.length ? msgs : null;
    }
});
//# sourceMappingURL=analyst-rust.js.map