import { messages } from '../../../core/messages/index.js';
import { createLineLookup, maskGoComments } from '../../../shared/helpers/index.js';
import { criarAnalista, criarOcorrencia } from '../../../types/index.js';
const disableEnv = process.env.TUTANUS_DISABLE_PLUGIN_GO === '1';
function warn(message, relPath, line, nivel) {
    return criarOcorrencia({
        relPath,
        mensagem: message,
        linha: line,
        nivel: nivel ?? messages.SeverityNiveis.warning,
        origem: messages.AnalystOrigens.go,
        tipo: messages.AnalystTipos.go
    });
}
function collectGeneralIssues(src, relPath) {
    const ocorrencias = [];
    const lineOf = createLineLookup(src).lineAt;
    const scan = maskGoComments(src);
    const packageMatch = scan.match(/^\s*package\s+(\w+)/m);
    if (packageMatch) {
        const pkgName = packageMatch[1];
        if (/[A-Z_-]/.test(pkgName)) {
            const line = lineOf(packageMatch.index ?? 0);
            ocorrencias.push(warn(`Package name '${pkgName}' should be lowercase without underscores or hyphens`, relPath, line, messages.SeverityNiveis.suggestion));
        }
    }
    if (packageMatch && !/^(main|\w+_test)$/.test(packageMatch[1])) {
        const srcBeforePackage = src.slice(0, packageMatch.index ?? 0);
        if (!/\/\/\s*(Package|pacote)\s/i.test(srcBeforePackage) && !/\/\*[\s\S]*?(Package|pacote)/i.test(srcBeforePackage)) {
            const line = lineOf(packageMatch.index ?? 0);
            ocorrencias.push(warn(`Package '${packageMatch[1]}' missing package-level comment`, relPath, line, messages.SeverityNiveis.suggestion));
        }
    }
    for (const match of scan.matchAll(/_ "(.*?)"/g)) {
        const line = lineOf(match.index);
        const importPath = match[1];
        const srcBefore = src.slice(0, match.index);
        if (!/\/\/\s*why|\/\/\s*required|\/\/\s*driver/i.test(srcBefore)) {
            ocorrencias.push(warn(`Blank import '${importPath}' should have a comment explaining why`, relPath, line, messages.SeverityNiveis.info));
        }
    }
    for (const match of scan.matchAll(/func\s+\((\w+)\s+\*?\w+\)\s+\w+/g)) {
        const receiver = match[1];
        if (receiver.length > 2 && !/^[a-z]{1,2}$/.test(receiver)) {
            const line = lineOf(match.index);
            ocorrencias.push(warn(`Receiver '${receiver}' should be 1-2 lowercase letters; prefer '${receiver[0].toLowerCase()}'`, 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 / 2);
        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 analistaGo = criarAnalista({
    nome: 'analista-go',
    categoria: 'linguagem',
    descricao: 'Analisa código Go em busca de organização, nomenclatura e boas práticas gerais.',
    global: false,
    test: (relPath) => /\.go$/i.test(relPath),
    aplicar: async (src, relPath) => {
        if (disableEnv)
            return null;
        const msgs = collectGeneralIssues(src, relPath);
        return msgs.length ? msgs : null;
    }
});
//# sourceMappingURL=analyst-go.js.map