import { maskXmlNonCode } from '../../../shared/helpers/index.js';
export function pontuarXml(src, _relPath) {
    let score = 0;
    const maxScore = 100;
    const issues = [];
    const strengths = [];
    const scan = maskXmlNonCode(src);
    const srcCompleto = scan;
    if (!/<!DOCTYPE\b/i.test(srcCompleto)) {
        score += 30;
        strengths.push('Sem DOCTYPE (mais seguro contra XXE)');
    }
    else {
        score += 10;
        issues.push('DOCTYPE presente - risco XXE');
    }
    const externalEntities = (srcCompleto.match(/<!ENTITY\b.*\b(SYSTEM|PUBLIC)\b/gi) || []).length;
    if (externalEntities === 0) {
        score += 25;
        strengths.push('Sem entidades externas');
    }
    else {
        issues.push(`${externalEntities} entidade(s) externa(s)`);
    }
    if (!/<\s*(?:xi|xinclude):include\b/i.test(srcCompleto)) {
        score += 15;
        strengths.push('Sem XInclude');
    }
    else {
        issues.push('XInclude detectado');
    }
    const declaredNs = new Set();
    for (const metrica of srcCompleto.matchAll(/xmlns(?::([a-zA-Z_][\w.-]*))?\s*=\s*['"][^'"]*['"]/g)) {
        if (metrica[1])
            declaredNs.add(metrica[1]);
    }
    const usedNs = new Set();
    for (const metrica of srcCompleto.matchAll(/([a-zA-Z_][\w.-]*):[a-zA-Z_][\w.-]*/g)) {
        const prefix = metrica[1];
        if (prefix !== 'xml' && prefix !== 'xmlns')
            usedNs.add(prefix);
    }
    const undeclaredNs = [...usedNs].filter(caminho => !declaredNs.has(caminho));
    if (undeclaredNs.length === 0) {
        score += 15;
        strengths.push('Todos namespaces declarados');
    }
    else {
        issues.push(`${undeclaredNs.length} namespace(s) não declarado(s): ${undeclaredNs.join(', ')}`);
    }
    const openTags = (srcCompleto.match(/<([a-zA-Z_][\w.-]*)((?:\s[^>]*)?)>/g) || []).filter(textoTitulo => !textoTitulo.endsWith('/>') && !/<\?/.test(textoTitulo));
    const closeTags = srcCompleto.match(/<\/([a-zA-Z_][\w.-]*)>/g) || [];
    if (openTags.length === closeTags.length) {
        score += 15;
        strengths.push('XML bem formado');
    }
    else {
        issues.push('XML possivelmente mal formado');
    }
    return {
        score,
        maxScore,
        percentage: Math.round(score / maxScore * 100),
        issues,
        strengths
    };
}
export const pontuadorXml = {
    nome: 'pontuador-xml',
    categoria: 'markup',
    descricao: 'Pontua qualidade de XML',
    pontuar: pontuarXml
};
//# sourceMappingURL=scorer-xml.js.map