import fs from 'node:fs';
import { config, mesclarConfigExcludes } from '../../core/config/index.js';
export function processPatternListAchatado(raw) {
    if (!raw || raw.length === 0)
        return [];
    return Array.from(new Set(raw.flatMap(raiz => raiz.split(/[\s,]+/)).map(string => string.trim()).filter(Boolean)));
}
export function processPatternGroups(raw) {
    if (!raw || raw.length === 0)
        return [];
    return raw.map(grupo => grupo.split(/[\s,]+/).map(string => string.trim()).filter(Boolean)).filter(grupo => grupo.length > 0);
}
export function expandIncludes(list) {
    const META = /[\\*\?\{\}\[\]]/;
    const out = new Set();
    for (const caminho of list) {
        out.add(caminho);
        if (!META.test(caminho)) {
            const normalized = caminho.replace(/[\\\/]+$/, '');
            out.add(`${normalized}/**`);
            if (!caminho.includes('/') && !caminho.includes('\\')) {
                out.add(`**/${normalized}/**`);
            }
        }
    }
    return Array.from(out);
}
export function detectarTipoProjeto(baseDir = process.cwd()) {
    try {
        const files = fs.readdirSync(baseDir);
        const hasFile = (name) => files.includes(name);
        if (hasFile('tsconfig.json') && hasFile('package.json')) {
            return 'typescript';
        }
        if (hasFile('package.json')) {
            return 'nodejs';
        }
        if (hasFile('requirements.txt') || hasFile('pyproject.toml') || hasFile('setup.py')) {
            return 'python';
        }
        if (files.some(arquivo => arquivo.endsWith('.csproj')) || files.some(arquivo => arquivo.endsWith('.sln'))) {
            return 'dotnet';
        }
        return 'generico';
    }
    catch {
        return 'generico';
    }
}
export function getDefaultExcludes(tipoProjeto) {
    const configIncluirExcluir = config.INCLUDE_EXCLUDE_RULES;
    if (configIncluirExcluir?.globalExcludeGlob) {
        if (Array.isArray(configIncluirExcluir.globalExcludeGlob) && configIncluirExcluir.globalExcludeGlob.length > 0) {
            return Array.from(new Set(configIncluirExcluir.globalExcludeGlob));
        }
    }
    const tipo = tipoProjeto || detectarTipoProjeto();
    return mesclarConfigExcludes(null, tipo);
}
export function processarFiltros(opcoes) {
    const tipoProjeto = detectarTipoProjeto();
    const includeGroups = processPatternGroups(opcoes.include);
    const includeFlat = includeGroups.flat();
    const includeExpanded = expandIncludes(includeFlat);
    const excludeFlat = processPatternListAchatado(opcoes.exclude);
    let excludePadroes;
    if (excludeFlat.length > 0) {
        excludePadroes = excludeFlat;
    }
    else {
        excludePadroes = getDefaultExcludes(tipoProjeto);
    }
    let incluiNodeModules = opcoes.forceIncludeNodeModules || false;
    if (includeFlat.some(caminho => /node_modules/.test(caminho))) {
        incluiNodeModules = true;
    }
    if (incluiNodeModules) {
        excludePadroes = excludePadroes.filter(caminho => !/node_modules/.test(caminho));
    }
    if (opcoes.forceIncludeTests && !includeFlat.some(caminho => /tests?/.test(caminho))) {
        includeExpanded.push('tests/**', 'test/**', '**/*.test.*', '**/*.spec.*');
    }
    return {
        includeGroups,
        includeFlat: includeExpanded,
        excludePadroes,
        incluiNodeModules,
        tipoProjeto
    };
}
export function aplicarFiltrosAoConfig(filtros) {
    if (filtros.includeFlat.length > 0) {
        config.CLI_INCLUDE_GROUPS = filtros.includeGroups;
        config.CLI_INCLUDE_PATTERNS = filtros.includeFlat;
    }
    else {
        config.CLI_INCLUDE_GROUPS = [];
        config.CLI_INCLUDE_PATTERNS = [];
    }
    config.CLI_EXCLUDE_PATTERNS = filtros.excludePadroes;
}
export function configurarFiltros(opcoes) {
    const filtros = processarFiltros(opcoes);
    aplicarFiltrosAoConfig(filtros);
    return filtros;
}
//# sourceMappingURL=filters.js.map