import { messages } from '../../../core/messages/index.js';
import { criarAnalista, criarOcorrencia } from '../../../types/index.js';
const disableEnv = process.env.TUTANUS_DISABLE_PLUGIN_CSS_IN_JS === '1';
function findLine(src, index = 0) {
    return src.slice(0, index).split(/\n/).length;
}
function emitir(message, relPath, nivel, line = 1) {
    return criarOcorrencia({
        relPath,
        mensagem: message,
        linha: line,
        nivel,
        origem: messages.AnalystOrigens.cssInJs,
        tipo: messages.AnalystTipos.cssInJs
    });
}
function detectarStyledComponents(src) {
    const indices = [];
    for (const metrica of src.matchAll(/from\s+['"]styled-components['"]/g)) {
        if (typeof metrica.index === 'number')
            indices.push(metrica.index);
    }
    for (const metrica of src.matchAll(/require\(\s*['"]styled-components['"]\s*\)/g)) {
        if (typeof metrica.index === 'number')
            indices.push(metrica.index);
    }
    for (const metrica of src.matchAll(/\bstyled\s*\./g)) {
        if (typeof metrica.index === 'number')
            indices.push(metrica.index);
    }
    for (const metrica of src.matchAll(/\bstyled\s*\(/g)) {
        if (typeof metrica.index === 'number')
            indices.push(metrica.index);
    }
    for (const metrica of src.matchAll(/\bcreateGlobalStyle\b/g)) {
        if (typeof metrica.index === 'number')
            indices.push(metrica.index);
    }
    for (const metrica of src.matchAll(/\bcss\s*`/g)) {
        if (typeof metrica.index === 'number')
            indices.push(metrica.index);
    }
    const detected = indices.length > 0;
    const global = /\bcreateGlobalStyle\b/.test(src);
    return {
        detected,
        global,
        indices
    };
}
function detectarStyledJsx(src) {
    const indices = [];
    for (const metrica of src.matchAll(/<style\b[^>]*\bjsx\b[^>]*>/gi)) {
        if (typeof metrica.index === 'number')
            indices.push(metrica.index);
    }
    for (const metrica of src.matchAll(/from\s+['"]styled-jsx\/css['"]/g)) {
        if (typeof metrica.index === 'number')
            indices.push(metrica.index);
    }
    for (const metrica of src.matchAll(/from\s+['"]styled-jsx['"]/g)) {
        if (typeof metrica.index === 'number')
            indices.push(metrica.index);
    }
    const detected = indices.length > 0;
    const global = /<style\b[^>]*\bjsx\b[^>]*\bglobal\b[^>]*>/i.test(src);
    return {
        detected,
        global,
        indices
    };
}
export const analistaCssInJs = criarAnalista({
    nome: 'analista-css-in-js',
    categoria: 'estilo',
    descricao: 'Detecta padrões de CSS-in-JS (styled-components, styled-jsx).',
    global: false,
    test: (relPath) => /\.(js|jsx|ts|tsx|mjs|cjs)$/i.test(relPath),
    aplicar: async (src, relPath) => {
        if (disableEnv)
            return null;
        if (relPath.includes('src/analistas/css-in-js/'))
            return null;
        if (!/styled-components|styled-jsx|<style\b[^>]*\bjsx\b|\bcreateGlobalStyle\b|\bstyled\b/i.test(src)) {
            return null;
        }
        const ocorrencias = [];
        const sc = detectarStyledComponents(src);
        const sj = detectarStyledJsx(src);
        if (!sc.detected && !sj.detected)
            return null;
        if (sc.detected) {
            const first = sc.indices[0] ?? 0;
            ocorrencias.push(emitir(messages.CssInJsMensagens.detectedStyledComponents, relPath, messages.SeverityNiveis.info, findLine(src, first)));
            if (sc.global) {
                ocorrencias.push(emitir(messages.CssInJsMensagens.globalStyles('styled-components'), relPath, messages.SeverityNiveis.warning, findLine(src, first)));
            }
        }
        if (sj.detected) {
            const first = sj.indices[0] ?? 0;
            ocorrencias.push(emitir(messages.CssInJsMensagens.detectedStyledJsx, relPath, messages.SeverityNiveis.info, findLine(src, first)));
            if (sj.global) {
                ocorrencias.push(emitir(messages.CssInJsMensagens.globalStyles('styled-jsx'), relPath, messages.SeverityNiveis.warning, findLine(src, first)));
            }
        }
        if (/!important/.test(src)) {
            ocorrencias.push(emitir(messages.CssInJsMensagens.importantUsage, relPath, messages.SeverityNiveis.warning, 1));
        }
        if (/url\(\s*['"]?http:\/\//i.test(src)) {
            ocorrencias.push(emitir(messages.CssInJsMensagens.httpUrl, relPath, messages.SeverityNiveis.warning, 1));
        }
        return ocorrencias.length ? ocorrencias : null;
    }
});
//# sourceMappingURL=analyst-css-in-js.js.map