"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, /**
 * nvu uninstall <version>
 *
 * Remove an installed Node version.
 */ "default", {
    enumerable: true,
    get: function() {
        return uninstallCmd;
    }
});
var _exitcompat = /*#__PURE__*/ _interop_require_default(require("exit-compat"));
var _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
var _path = /*#__PURE__*/ _interop_require_default(require("path"));
var _compatts = require("../compat.js");
var _constantsts = require("../constants.js");
var _findInstalledVersionsts = require("../lib/findInstalledVersions.js");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function uninstallCmd(args) {
    if (args.length === 0) {
        console.log('Usage: nvu uninstall <version>');
        console.log('Example: nvu uninstall 20');
        console.log('         nvu uninstall v20.19.6');
        (0, _exitcompat.default)(1);
        return;
    }
    var version = args[0].trim();
    var versionsPath = _path.default.join(_constantsts.storagePath, 'installed');
    // Find all matching installed versions
    var matches = (0, _findInstalledVersionsts.findInstalledVersions)(versionsPath, version);
    if (matches.length === 0) {
        console.log("Node ".concat(version, " is not installed."));
        console.log('');
        console.log('Installed versions:');
        listInstalledVersions(versionsPath);
        (0, _exitcompat.default)(1);
        return;
    }
    if (matches.length > 1) {
        console.log('Multiple versions match "'.concat(version, '":'));
        for(var i = 0; i < matches.length; i++){
            console.log("  ".concat(matches[i]));
        }
        console.log('');
        console.log('Please specify the exact version to uninstall.');
        (0, _exitcompat.default)(1);
        return;
    }
    var installedVersion = matches[0];
    var versionPath = _path.default.join(versionsPath, installedVersion);
    // Check if this is the current default (exact match since we store exact versions)
    var defaultPath = _path.default.join(_constantsts.storagePath, 'default');
    if (_fs.default.existsSync(defaultPath)) {
        var defaultVersion = _fs.default.readFileSync(defaultPath, 'utf8').trim();
        // Normalize both for comparison
        var normalizedDefault = defaultVersion.replace(/^v/, '');
        var normalizedInstalled = installedVersion.replace(/^v/, '');
        if (normalizedInstalled === normalizedDefault) {
            console.error("Cannot uninstall default version ".concat(installedVersion, "."));
            console.error('');
            console.error('Change your default first:');
            console.error('  nvu default <version>');
            (0, _exitcompat.default)(1);
            return;
        }
    }
    // Remove the version directory
    try {
        (0, _compatts.rmSync)(versionPath);
        console.log("Removed Node ".concat(installedVersion));
    } catch (err) {
        console.error("Failed to remove Node ".concat(installedVersion, ":"), err.message);
        (0, _exitcompat.default)(1);
        return;
    }
    (0, _exitcompat.default)(0);
}
/**
 * List installed versions for user reference
 */ function listInstalledVersions(versionsPath) {
    var versions = (0, _findInstalledVersionsts.getAllInstalledVersions)(versionsPath);
    if (versions.length === 0) {
        console.log('  (none)');
    } else {
        for(var i = 0; i < versions.length; i++){
            console.log("  ".concat(versions[i]));
        }
    }
}
/* 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; }