"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, /**
 * nvu which
 *
 * Show which Node binary would be used based on current directory.
 * This simulates what the nvu binary would do.
 */ "default", {
    enumerable: true,
    get: function() {
        return whichCmd;
    }
});
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 _constantsts = require("../constants.js");
var _findInstalledVersionsts = require("../lib/findInstalledVersions.js");
var _resolveSystemBinaryts = require("../lib/resolveSystemBinary.js");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function whichCmd(_args) {
    var cwd = process.cwd();
    // Resolve version using the same logic as the nvu binary
    var version = resolveVersion(cwd);
    if (!version) {
        console.log('No Node version configured for this directory.');
        console.log('');
        console.log('To configure a version:');
        console.log('  nvu local <version>   - Set version for this project');
        console.log('  nvu default <version> - Set global default');
        (0, _exitcompat.default)(1);
        return;
    }
    // Handle "system" version specially
    if (version === 'system') {
        console.log('Version: system');
        console.log("Source: ".concat(getVersionSource(cwd)));
        var systemNode = (0, _resolveSystemBinaryts.resolveSystemBinary)('node');
        console.log("Binary: ".concat(systemNode || 'not found'));
        if (systemNode) {
            console.log('Status: Available');
        } else {
            console.log('Status: Not found (install Node.js on your system)');
        }
        (0, _exitcompat.default)(0);
        return;
    }
    // Resolve partial version to exact installed version
    var versionsPath = _path.default.join(_constantsts.storagePath, 'installed');
    var matches = (0, _findInstalledVersionsts.findInstalledVersions)(versionsPath, version);
    var resolvedVersion = matches.length > 0 ? matches[matches.length - 1] : null;
    // Display version (show resolution if different)
    if (resolvedVersion && resolvedVersion !== version && resolvedVersion !== "v".concat(version)) {
        console.log("Version: ".concat(version, " → ").concat(resolvedVersion));
    } else {
        console.log("Version: ".concat(resolvedVersion || version));
    }
    console.log("Source: ".concat(getVersionSource(cwd)));
    if (resolvedVersion) {
        var actualVersionPath = _path.default.join(versionsPath, resolvedVersion);
        var nodePath = _path.default.join(actualVersionPath, 'bin', 'node');
        console.log("Binary: ".concat(nodePath));
        if (_fs.default.existsSync(nodePath)) {
            console.log('Status: Installed');
        } else {
            console.log('Status: Directory exists but binary not found');
        }
    } else {
        console.log("Status: Not installed (run: nvu install ".concat(version, ")"));
    }
    (0, _exitcompat.default)(0);
}
/**
 * Resolve version from config files (mirrors nvu binary logic)
 */ function resolveVersion(cwd) {
    // Walk up directories looking for .nvurc or .nvmrc
    var dir = cwd;
    while(true){
        // Check .nvurc first
        var nvurcPath = _path.default.join(dir, '.nvurc');
        if (_fs.default.existsSync(nvurcPath)) {
            return _fs.default.readFileSync(nvurcPath, 'utf8').trim();
        }
        // Check .nvmrc
        var nvmrcPath = _path.default.join(dir, '.nvmrc');
        if (_fs.default.existsSync(nvmrcPath)) {
            return _fs.default.readFileSync(nvmrcPath, 'utf8').trim();
        }
        // Move to parent
        var parent = _path.default.dirname(dir);
        if (parent === dir) break;
        dir = parent;
    }
    // Check global default
    var defaultPath = _path.default.join(_constantsts.storagePath, 'default');
    if (_fs.default.existsSync(defaultPath)) {
        return _fs.default.readFileSync(defaultPath, 'utf8').trim();
    }
    return null;
}
/**
 * Determine the source of the version (for display)
 */ function getVersionSource(cwd) {
    var dir = cwd;
    while(true){
        var nvurcPath = _path.default.join(dir, '.nvurc');
        if (_fs.default.existsSync(nvurcPath)) {
            return nvurcPath;
        }
        var nvmrcPath = _path.default.join(dir, '.nvmrc');
        if (_fs.default.existsSync(nvmrcPath)) {
            return nvmrcPath;
        }
        var parent = _path.default.dirname(dir);
        if (parent === dir) break;
        dir = parent;
    }
    var defaultPath = _path.default.join(_constantsts.storagePath, 'default');
    if (_fs.default.existsSync(defaultPath)) {
        return "".concat(defaultPath, " (global default)");
    }
    return 'none';
}
/* 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; }