import { promises as fs } from 'node:fs';
import path from 'node:path';
import { config, isInsideSrc, LIMITES_PADRAO } from '../../core/config/index.js';
import { scanRepository } from '../../core/execution/index.js';
import { isTestArquivo, normalizePath } from '../../shared/helpers/index.js';
import { grafoDependencias } from './detector-dependencies.js';
const EXTENSOES_ALVO = ['.js', '.ts', '.jsx', '.tsx', '.mjs', '.cjs'];
const INATIVIDADE_DIAS = Number(process.env.GHOST_DAYS) || LIMITES_PADRAO.GUARDIAN.DIAS_INATIVIDADE_GHOST;
const MILIS_POR_DIA = 86_400_000;
const ENTRYPOINT_PADROES = [/^(src\/)?index\.(ts|js|tsx|jsx|mjs|cjs)$/i, /^(src\/)?main\.(ts|js|tsx|jsx|mjs|cjs)$/i, /^(src\/)?server\.(ts|js|tsx|jsx|mjs|cjs)$/i, /^(src\/)?app\.(ts|js|tsx|jsx|mjs|cjs)$/i, /^(src\/)?cli\.(ts|js|tsx|jsx|mjs|cjs)$/i, /(^|\/)bin\/[^/]+\.(ts|js|mjs|cjs)$/i, /\/index\.(ts|js|tsx|jsx|mjs|cjs)$/i, /config\.(ts|js|cjs|mjs)$/i, /\.config\.(ts|js|cjs|mjs)$/i, /\.d\.ts$/i];
function isEntrypoint(relPath) {
    const normalized = normalizePath(relPath);
    return ENTRYPOINT_PADROES.some(caminho => caminho.test(normalized));
}
function estaSendoReferenciado(relPath, grafo) {
    const normalized = normalizePath(relPath);
    const variations = new Set([normalized]);
    const ext = path.posix.extname(normalized);
    if (ext) {
        variations.add(normalized.slice(0, -ext.length));
    }
    const base = ext ? normalized.slice(0, -ext.length) : normalized;
    for (const erro of ['.ts', '.js', '.tsx', '.jsx', '.mjs', '.cjs']) {
        variations.add(base + erro);
    }
    for (const dependencias of grafo.values()) {
        for (const dep of dependencias) {
            const depNorm = normalizePath(dep);
            if (variations.has(depNorm))
                return true;
        }
    }
    return false;
}
function importaOutros(relPath, grafo) {
    const normalized = normalizePath(relPath);
    for (const [chave, deps] of grafo.entries()) {
        if (normalizePath(chave) === normalized && deps.size > 0) {
            return true;
        }
    }
    return false;
}
export async function detectarFantasmas(baseDir = process.cwd()) {
    const fileMap = await scanRepository(baseDir);
    const agora = Date.now();
    const fantasmas = [];
    const testConfiguracao = config.testPadroes;
    const excludeTestsFromOrphan = testConfiguracao?.excludeFromOrphanCheck !== false;
    for (const entrada of Object.values(fileMap)) {
        const { relPath, fullCaminho } = entrada;
        const ext = path.extname(relPath).toLowerCase();
        if (!EXTENSOES_ALVO.includes(ext))
            continue;
        if (excludeTestsFromOrphan && isTestArquivo(relPath)) {
            continue;
        }
        if (isEntrypoint(relPath)) {
            continue;
        }
        try {
            const stat = await fs.stat(fullCaminho);
            const diasInativo = Math.floor((agora - stat.mtimeMs) / MILIS_POR_DIA);
            if (grafoDependencias.size === 0)
                continue;
            const referenciado = estaSendoReferenciado(relPath, grafoDependencias);
            if (importaOutros(relPath, grafoDependencias)) {
                continue;
            }
            if (isInsideSrc(relPath)) {
                if (diasInativo < 7)
                    continue;
            }
            if (!referenciado && diasInativo > INATIVIDADE_DIAS) {
                fantasmas.push({
                    arquivo: relPath,
                    referenciado,
                    diasInativo
                });
            }
        }
        catch {
        }
    }
    return {
        total: fantasmas.length,
        fantasmas
    };
}
//# sourceMappingURL=detector-phantoms.js.map