import fsSync from 'node:fs';
import path from 'node:path';
const _TOP_DIRS = new Set(['analysts', 'arquitetos', 'cli', 'guardian', 'nucleo', 'reports', 'tipos', 'auto']);
const LEGADO_ALIAS_MAPA = new Map();
function toPosix(caminho) {
    return caminho.replace(/\\/g, '/');
}
function exists(caminho) {
    try {
        return fsSync.existsSync(caminho);
    }
    catch {
        return false;
    }
}
function pathFromAlias(aliasSpec, srcRoot) {
    const spec = aliasSpec.replace(/^@/, '');
    const parts = spec.split('/');
    const top = parts?.shift();
    return path.join(srcRoot, top ?? '', parts.join('/'));
}
export function pickExtForAlias(aliasBase, srcRoot) {
    const absTs = pathFromAlias(`${aliasBase}.ts`, srcRoot);
    const absJs = pathFromAlias(`${aliasBase}.js`, srcRoot);
    if (exists(absTs))
        return `${aliasBase}.ts`;
    if (exists(absJs))
        return `${aliasBase}.js`;
    return `${aliasBase}.ts`;
}
export function rewriteToAlias(spec, ctx) {
    const posixSpec = toPosix(spec);
    if (posixSpec.startsWith('@') || /^([a-zA-Z]+:|node:|[a-zA-Z0-9_-]+)$/.test(posixSpec)) {
        if (!posixSpec.startsWith('@'))
            return {
                changed: false,
                value: spec
            };
        const withoutExt = posixSpec.replace(/\.(ts|js|mjs|cjs|tsx|jsx)$/i, '');
        let mapped = LEGADO_ALIAS_MAPA.get(withoutExt);
        if (!mapped && withoutExt.startsWith('@cli/src/'))
            mapped = withoutExt;
        if (mapped) {
            const withExt = pickExtForAlias(mapped, ctx.srcRoot);
            return {
                changed: true,
                value: withExt
            };
        }
        return {
            changed: false,
            value: spec
        };
    }
    return {
        changed: false,
        value: spec
    };
}
export function transformCodeForTests(code, ctx) {
    let changed = false;
    const rewrite = (spec) => rewriteToAlias(spec, ctx);
    code = code.replace(/(import\s+[^'"\n]+?from\s+)(["'])([^"']+?)\2/g, (metrica, p1, delimitadorString, spec) => {
        const result = rewrite(spec);
        if (!result.changed)
            return metrica;
        changed = true;
        return `${p1}${delimitadorString}${result.value}${delimitadorString}`;
    });
    code = code.replace(/(import\s*)(["'])([^"']+?)\2/g, (metrica, p1, delimitadorString, spec) => {
        const result = rewrite(spec);
        if (!result.changed)
            return metrica;
        changed = true;
        return `${p1}${delimitadorString}${result.value}${delimitadorString}`;
    });
    code = code.replace(/(import\s*\()(\s*["'])([^"']+?)(["']\s*\))/g, (metrica, p1, q1, spec, q2) => {
        const result = rewrite(spec);
        if (!result.changed)
            return metrica;
        changed = true;
        return `${p1}${q1}${result.value}${q2}`;
    });
    code = code.replace(/(vi\.(?:do)?mock\s*\()(\s*["'])([^"']+?)(["'])(\s*,?)/g, (metrica, p1, q1, spec, q2, comma = '') => {
        const result = rewrite(spec);
        if (!result.changed)
            return metrica;
        changed = true;
        return `${p1}${q1}${result.value}${q2}${comma}`;
    });
    return {
        code,
        changed
    };
}
//# sourceMappingURL=rewrite-testes-aliases.js.map