"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get findInstalledVersions () {
        return findInstalledVersions;
    },
    get getAllInstalledVersions () {
        return getAllInstalledVersions;
    }
});
var _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
var _path = /*#__PURE__*/ _interop_require_default(require("path"));
var _compatts = require("../compat.js");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
/**
 * Compare two semver version strings (e.g., "20.19.0" vs "20.9.1")
 * Returns: negative if a < b, positive if a > b, 0 if equal
 */ function compareVersions(a, b) {
    var aParts = a.replace(/^v/, '').split('.');
    var bParts = b.replace(/^v/, '').split('.');
    var len = Math.max(aParts.length, bParts.length);
    for(var i = 0; i < len; i++){
        var aNum = parseInt(aParts[i], 10) || 0;
        var bNum = parseInt(bParts[i], 10) || 0;
        if (aNum !== bNum) {
            return aNum - bNum;
        }
    }
    return 0;
}
function findInstalledVersions(versionsPath, version) {
    if (!_fs.default.existsSync(versionsPath)) {
        return [];
    }
    var normalizedVersion = version.replace(/^v/, '');
    var matches = [];
    // Try exact matches first
    var exactMatches = [
        version,
        "v".concat(normalizedVersion),
        normalizedVersion
    ];
    for(var i = 0; i < exactMatches.length; i++){
        var v = exactMatches[i];
        var versionPath = _path.default.join(versionsPath, v);
        if (_fs.default.existsSync(versionPath) && _fs.default.statSync(versionPath).isDirectory()) {
            if (matches.indexOf(v) === -1) {
                matches.push(v);
            }
        }
    }
    // If we have an exact match, return just that
    if (matches.length > 0) {
        return matches;
    }
    // Try partial match (e.g., "20" matches "v20.19.6")
    var entries = (0, _compatts.readdirWithTypes)(versionsPath);
    for(var j = 0; j < entries.length; j++){
        var entry = entries[j];
        if (!entry.isDirectory()) continue;
        var dirVersion = entry.name.replace(/^v/, '');
        if (dirVersion.indexOf("".concat(normalizedVersion, ".")) === 0) {
            matches.push(entry.name);
        }
    }
    // Sort by semver (ascending) so highest version is last
    matches.sort(compareVersions);
    return matches;
}
function getAllInstalledVersions(versionsPath) {
    if (!_fs.default.existsSync(versionsPath)) {
        return [];
    }
    var entries = (0, _compatts.readdirWithTypes)(versionsPath);
    var versions = [];
    for(var i = 0; i < entries.length; i++){
        if (entries[i].isDirectory()) {
            versions.push(entries[i].name);
        }
    }
    return versions;
}
/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }