import { messages } from '../../../core/messages/index.js';
import { createLineLookup, maskRustComments } from '../../../shared/helpers/index.js';
import { criarAnalista } from '../../../types/index.js';
const disableEnv = process.env.TUTANUS_DISABLE_PLUGIN_RUST === '1';
function detectUnsafePatterns(src, relPath) {
    const ocorrencias = [];
    const lineOf = createLineLookup(src).lineAt;
    const scan = maskRustComments(src);
    for (const match of scan.matchAll(/\bunsafe\s*\{/g)) {
        const line = lineOf(match.index);
        const srcBefore = src.slice(0, match.index);
        const lineStart = srcBefore.lastIndexOf('\n') + 1;
        const lineText = src.slice(lineStart, match.index).trim();
        const precedingLines = src.split('\n').slice(Math.max(0, line - 4), line).join('\n');
        if (!precedingLines.includes('// SAFETY:') && !precedingLines.includes('/* SAFETY:')) {
            ocorrencias.push({
                relPath,
                mensagem: messages.RustMensagens.unsafeBlock,
                linha: line,
                nivel: messages.SeverityNiveis.error,
                origem: 'detector-unsafe',
                tipo: 'rust/rule'
            });
        }
    }
    for (const match of scan.matchAll(/\b(?:std::)?mem::transmute\b/g)) {
        const line = lineOf(match.index);
        const srcBefore = src.slice(0, match.index);
        const lineText = srcBefore.split('\n').pop() ?? '';
        if (!lineText.includes('//') && !lineText.includes('/*')) {
            ocorrencias.push({
                relPath,
                mensagem: messages.RustMensagens.transmuteUsage,
                linha: line,
                nivel: messages.SeverityNiveis.error,
                origem: 'detector-unsafe',
                tipo: 'rust/rule'
            });
        }
    }
    for (const match of scan.matchAll(/\b(?:const|mut)\s+ptr\s*:\s*\*/g)) {
        const line = lineOf(match.index);
        const srcAfter = src.slice(match.index, match.index + 200);
        if (srcAfter.includes('unsafe')) {
            ocorrencias.push({
                relPath,
                mensagem: messages.RustMensagens.rawPointer,
                linha: line,
                nivel: messages.SeverityNiveis.info,
                origem: 'detector-unsafe',
                tipo: 'rust/rule'
            });
        }
    }
    for (const match of scan.matchAll(/#\[repr\(C\)\]/g)) {
        const line = lineOf(match.index);
        const srcBefore = src.slice(0, match.index);
        const lineText = srcBefore.split('\n').pop() ?? '';
        if (!lineText.includes('//') && !lineText.includes('FFI') && !lineText.includes('extern')) {
            ocorrencias.push({
                relPath,
                mensagem: messages.RustMensagens.reprC,
                linha: line,
                nivel: messages.SeverityNiveis.info,
                origem: 'detector-unsafe',
                tipo: 'rust/rule'
            });
        }
    }
    return ocorrencias;
}
export const detectorUnsafe = criarAnalista({
    nome: 'detector-unsafe-rust',
    categoria: 'seguranca',
    descricao: 'Detecta uso inadequado de unsafe e operações inseguras em código Rust.',
    global: false,
    test: (relPath) => /\.(rs)$/i.test(relPath),
    aplicar: async (src, relPath) => {
        if (disableEnv)
            return null;
        const msgs = detectUnsafePatterns(src, relPath);
        return msgs.length ? msgs : null;
    }
});
//# sourceMappingURL=detector-unsafe.js.map