import { splitLines } from '../../../shared/helpers/index.js';
export function pontuarSql(src, _relPath) {
    let score = 0;
    const maxScore = 100;
    const issues = [];
    const strengths = [];
    const _linhas = splitLines(src);
    const srcCompleto = src;
    const hardcodedCreds = (srcCompleto.match(/\b(password|senha|secret|key|token)\b\s*[:=]\s*['"]/gi) || []).length;
    if (hardcodedCreds === 0) {
        score += 25;
        strengths.push('Sem credenciais hardcoded');
    }
    else {
        issues.push(`${hardcodedCreds} credencial(is) hardcoded`);
    }
    const deletes = (srcCompleto.match(/\bdelete\s+/gi) || []).length;
    const updates = (srcCompleto.match(/\bupdate\s+\w+\s+set/gi) || []).length;
    const withWhere = (srcCompleto.match(/\bwhere\b/gi) || []).length;
    const totalMutations = deletes + updates;
    if (totalMutations === 0 || withWhere >= totalMutations) {
        score += 20;
        strengths.push('WHERE clauses adequados');
    }
    else {
        score += Math.max(0, 20 - (totalMutations - withWhere) * 10);
        issues.push(`${totalMutations - withWhere} operação(ões) sem WHERE`);
    }
    const selectStar = (srcCompleto.match(/\bselect\s+\*\s+from/gi) || []).length;
    if (selectStar === 0) {
        score += 15;
        strengths.push('Colunas explicitamente listadas');
    }
    else {
        score += Math.max(0, 15 - selectStar * 5);
        issues.push(`${selectStar} SELECT * detectado(s)`);
    }
    const creates = (srcCompleto.match(/\bcreate\s+(table|procedure|view)\b/gi) || []).length;
    const ifNotExists = (srcCompleto.match(/\bif\s+not\s+exists\b/gi) || []).length;
    if (creates === 0 || ifNotExists >= creates) {
        score += 15;
        strengths.push('Scripts idempotentes');
    }
    else {
        issues.push(`${creates - ifNotExists} CREATE sem IF NOT EXISTS`);
    }
    if (!/\btruncate\s+table\b/i.test(srcCompleto)) {
        score += 10;
        strengths.push('Sem TRUNCATE (operações seguras)');
    }
    else {
        issues.push('TRUNCATE TABLE detectado');
    }
    const varcharWithoutSize = (srcCompleto.match(/\bvarchar\b\s*(\(|\s|$)/gi) || []).filter(metrica => !/varchar\(\d+\)/i.test(metrica)).length;
    if (varcharWithoutSize === 0) {
        score += 15;
        strengths.push('VARCHAR com tamanho definido');
    }
    else {
        issues.push(`${varcharWithoutSize} VARCHAR sem tamanho`);
    }
    return {
        score,
        maxScore,
        percentage: Math.round(score / maxScore * 100),
        issues,
        strengths
    };
}
export const pontuadorSql = {
    nome: 'pontuador-sql',
    categoria: 'database',
    descricao: 'Pontua qualidade de SQL',
    pontuar: pontuarSql
};
//# sourceMappingURL=scorer-sql.js.map