function parseSemver(versao) {
    const re = /^(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z0-9.]+))?$/;
    const match = versao.match(re);
    if (!match)
        return null;
    return { major: parseInt(match[1]), minor: parseInt(match[2]), patch: parseInt(match[3]), pre: match[4] };
}
function compareSemver(a, b) {
    const pa = parseSemver(a);
    const pb = parseSemver(b);
    if (!pa || !pb)
        return 0;
    if (pa.major !== pb.major)
        return pa.major - pb.major;
    if (pa.minor !== pb.minor)
        return pa.minor - pb.minor;
    if (pa.patch !== pb.patch)
        return pa.patch - pb.patch;
    if (pa.pre && !pb.pre)
        return -1;
    if (!pa.pre && pb.pre)
        return 1;
    return 0;
}
function satisfaz(versao, requisito) {
    if (requisito === '*' || requisito === 'latest')
        return true;
    if (requisito.startsWith('^')) {
        const base = requisito.slice(1);
        const parsed = parseSemver(base);
        if (!parsed)
            return false;
        const pv = parseSemver(versao);
        if (!pv)
            return false;
        return pv.major === parsed.major && compareSemver(versao, base) >= 0;
    }
    if (requisito.startsWith('~')) {
        const base = requisito.slice(1);
        const parsed = parseSemver(base);
        if (!parsed)
            return false;
        const pv = parseSemver(versao);
        if (!pv)
            return false;
        return pv.major === parsed.major && pv.minor === parsed.minor && compareSemver(versao, base) >= 0;
    }
    return versao === requisito;
}
export function resolverMelhorVersao(versoes, requisito) {
    const compativeis = versoes.filter(v => satisfaz(v.semver || v.version, requisito));
    if (compativeis.length === 0)
        return null;
    compativeis.sort((a, b) => compareSemver(b.version, a.version));
    return compativeis[0];
}
export function ordenarVersoes(versoes) {
    return [...versoes].sort((a, b) => compareSemver(b.version, a.version));
}
export function versaoMaisRecente(versoes) {
    if (versoes.length === 0)
        return null;
    return ordenarVersoes(versoes)[0];
}
//# sourceMappingURL=versionamento.js.map