/**
 * Resolve system binaries by searching PATH while excluding ~/.nvu/bin
 * This mirrors the Go binary's findSystemBinary() function
 */ "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 getPathWithoutNvuBin () {
        return getPathWithoutNvuBin;
    },
    get resolveSystemBinary () {
        return resolveSystemBinary;
    }
});
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
    };
}
var isWindows = process.platform === 'win32' || /^(msys|cygwin)$/.test(process.env.OSTYPE);
var nvuBinDir = _path.default.join((0, _compatts.homedir)(), '.nvu', 'bin');
/**
 * Check if two paths are equal (case-insensitive on Windows)
 */ function pathsEqual(a, b) {
    if (isWindows) {
        return a.toLowerCase() === b.toLowerCase();
    }
    return a === b;
}
/**
 * Check if a path is within the nvu bin directory
 */ function isInNvuBin(filePath) {
    try {
        var realPath = _fs.default.realpathSync(filePath);
        return realPath.indexOf(_path.default.join('.nvu', 'bin')) >= 0 || pathsEqual(_path.default.dirname(realPath), nvuBinDir);
    } catch (_e) {
        return false;
    }
}
function resolveSystemBinary(name) {
    var pathEnv = process.env.PATH || '';
    var pathSep = isWindows ? ';' : ':';
    var dirs = pathEnv.split(pathSep);
    for(var i = 0; i < dirs.length; i++){
        var dir = dirs[i];
        if (!dir) continue;
        // Skip ~/.nvu/bin
        if (pathsEqual(dir, nvuBinDir)) continue;
        // Build candidate path with appropriate extension
        var candidates = isWindows ? [
            _path.default.join(dir, "".concat(name, ".exe")),
            _path.default.join(dir, "".concat(name, ".cmd")),
            _path.default.join(dir, name)
        ] : [
            _path.default.join(dir, name)
        ];
        for(var j = 0; j < candidates.length; j++){
            var candidate = candidates[j];
            try {
                var stat = _fs.default.statSync(candidate);
                if (!stat.isFile()) continue;
                // Make sure it's not in ~/.nvu/bin (via symlink)
                if (isInNvuBin(candidate)) continue;
                return candidate;
            } catch (_e) {
            // File doesn't exist, continue
            }
        }
    }
    return null;
}
function getPathWithoutNvuBin() {
    var pathEnv = process.env.PATH || '';
    var pathSep = isWindows ? ';' : ':';
    var dirs = pathEnv.split(pathSep);
    var filtered = [];
    for(var i = 0; i < dirs.length; i++){
        var dir = dirs[i];
        if (!dir) continue;
        if (pathsEqual(dir, nvuBinDir)) continue;
        if (dir.indexOf(_path.default.join('.nvu', 'bin')) >= 0) continue;
        filtered.push(dir);
    }
    return filtered.join(pathSep);
}
/* 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; }