import path from 'node:path';
import { messages } from '../../core/messages/index.js';
export function normalizePath(caminho) {
    const normalized = path.normalize(caminho);
    const resolved = path.resolve(normalized);
    const cwd = process.cwd();
    if (!resolved.startsWith(cwd)) {
        throw new Error(messages.ExcecoesMensagens.caminhoForaDaCwdNaoPermitido(caminho));
    }
    return normalized;
}
export function isPathSafe(fileCaminho) {
    try {
        const normalized = normalizePath(fileCaminho);
        const dangerousPadroes = [/\.\./,
            /^\/etc\//,
            /^\/root\//,
            /^\/sys\//,
            /^\/proc\//,
            /^C:\\Windows\\/i,
            /^C:\\System32\\/i
        ];
        return !dangerousPadroes.some(pattern => pattern.test(normalized));
    }
    catch {
        return false;
    }
}
export function isFilenameSafe(filename) {
    if (!filename || filename.length === 0)
        return false;
    if (filename.length > 255)
        return false;
    const invalidChars = /[<>:"|?*\x00-\x1f]/;
    if (invalidChars.test(filename))
        return false;
    const reservedNomes = /^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\.|$)/i;
    if (reservedNomes.test(filename))
        return false;
    return true;
}
export function sanitizeFilename(filename) {
    return filename.replace(/[<>:"|?*\x00-\x1f]/g, '_').replace(/^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\.|$)/i, 'file_$1$2').slice(0, 255);
}
export function isRelativePathValid(relativePath) {
    if (!relativePath)
        return false;
    if (path.isAbsolute(relativePath))
        return false;
    const normalized = path.normalize(relativePath);
    return !normalized.startsWith('..');
}
export function getFileExtension(filename) {
    const ext = path.extname(filename);
    return ext.toLowerCase();
}
export function isExtensionAllowed(filename, allowedExtensions) {
    const ext = getFileExtension(filename);
    return allowedExtensions.includes(ext);
}
export function normalizarPathLocal(caminho) {
    return normalizePath(caminho);
}
export function validarNumeroPositivo(valor, _nome) {
    if (typeof valor === 'number' && valor > 0)
        return valor;
    if (typeof valor === 'string') {
        const num = Number.parseFloat(valor);
        if (!Number.isNaN(num) && num > 0)
            return num;
    }
    return null;
}
export function validarCombinacoes(flags) {
    const erros = [];
    if (flags.scanOnly && flags.incremental) {
        erros.push({
            codigo: 'SCAN_INCREMENTAL',
            mensagem: messages.ExcecoesMensagens.validacaoScanIncremental
        });
    }
    return erros;
}
export function sanitizarFlags(flags) {
    const erros = validarCombinacoes(flags);
    if (erros.length) {
        const detalhe = erros.map(erro => `${erro.codigo}: ${erro.mensagem}`).join('; ');
        throw new Error(detalhe);
    }
}
//# sourceMappingURL=validation.js.map