const { ArrayPrototypeUnshift: _p_ArrayPrototypeUnshift } = require('../primordials/array.js')
const { ErrorCtor: _p_ErrorCtor } = require('../primordials/error.js')

const { SetCtor: _p_SetCtor } = require('../primordials/map-set.js')

const { ObjectAssign: _p_ObjectAssign, ObjectDefineProperty: _p_ObjectDefineProperty, ObjectGetOwnPropertyDescriptor: _p_ObjectGetOwnPropertyDescriptor } = require('../primordials/object.js')

const { RegExpCtor: _p_RegExpCtor } = require('../primordials/regexp.js')

const { StringPrototypeSubstring: _p_StringPrototypeSubstring } = require('../primordials/string.js')

"use strict";
/**
 * Bundled from which
 * This is a zero-dependency bundle created by rolldown.
 */
'use strict';

//#region \0rolldown/runtime.js
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
	value,
	configurable: true
});
var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);

//#endregion

//#region node_modules/.pnpm/isexe@3.1.1/node_modules/isexe/dist/cjs/posix.js
var require_posix = /* @__PURE__ */ __commonJSMin(((exports) => {
	/**
	* This is the Posix implementation of isexe, which uses the file
	* mode and uid/gid values.
	*
	* @module
	*/
	_p_ObjectDefineProperty(exports, "__esModule", { value: true });
	exports.sync = exports.isexe = void 0;
	const fs_1$1 = require("fs");
	const promises_1$1 = require("fs/promises");
	/**
	* Determine whether a path is executable according to the mode and
	* current (or specified) user and group IDs.
	*/
	const isexe = async (path, options = {}) => {
		const { ignoreErrors = false } = options;
		try {
			return checkStat(await (0, promises_1$1.stat)(path), options);
		} catch (e) {
			const er = e;
			if (ignoreErrors || er.code === "EACCES") return false;
			throw er;
		}
	};
	exports.isexe = isexe;
	/**
	* Synchronously determine whether a path is executable according to
	* the mode and current (or specified) user and group IDs.
	*/
	const sync = (path, options = {}) => {
		const { ignoreErrors = false } = options;
		try {
			return checkStat((0, fs_1$1.statSync)(path), options);
		} catch (e) {
			const er = e;
			if (ignoreErrors || er.code === "EACCES") return false;
			throw er;
		}
	};
	exports.sync = sync;
	const checkStat = (stat, options) => stat.isFile() && checkMode(stat, options);
	const checkMode = (stat, options) => {
		const myUid = options.uid ?? process.getuid?.();
		const myGroups = options.groups ?? process.getgroups?.() ?? [];
		const myGid = options.gid ?? process.getgid?.() ?? myGroups[0];
		if (myUid === void 0 || myGid === void 0) throw new _p_ErrorCtor("cannot get uid or gid");
		const groups = new _p_SetCtor([myGid, ...myGroups]);
		const mod = stat.mode;
		const uid = stat.uid;
		const gid = stat.gid;
		const u = parseInt("100", 8);
		const g = parseInt("010", 8);
		return !!(mod & parseInt("001", 8) || mod & g && groups.has(gid) || mod & u && uid === myUid || mod & 72 && myUid === 0);
	};
}));

//#endregion
//#region node_modules/.pnpm/isexe@3.1.1/node_modules/isexe/dist/cjs/win32.js
var require_win32 = /* @__PURE__ */ __commonJSMin(((exports) => {
	/**
	* This is the Windows implementation of isexe, which uses the file
	* extension and PATHEXT setting.
	*
	* @module
	*/
	_p_ObjectDefineProperty(exports, "__esModule", { value: true });
	exports.sync = exports.isexe = void 0;
	const fs_1 = require("fs");
	const promises_1 = require("fs/promises");
	/**
	* Determine whether a path is executable based on the file extension
	* and PATHEXT environment variable (or specified pathExt option)
	*/
	const isexe = async (path, options = {}) => {
		const { ignoreErrors = false } = options;
		try {
			return checkStat(await (0, promises_1.stat)(path), path, options);
		} catch (e) {
			const er = e;
			if (ignoreErrors || er.code === "EACCES") return false;
			throw er;
		}
	};
	exports.isexe = isexe;
	/**
	* Synchronously determine whether a path is executable based on the file
	* extension and PATHEXT environment variable (or specified pathExt option)
	*/
	const sync = (path, options = {}) => {
		const { ignoreErrors = false } = options;
		try {
			return checkStat((0, fs_1.statSync)(path), path, options);
		} catch (e) {
			const er = e;
			if (ignoreErrors || er.code === "EACCES") return false;
			throw er;
		}
	};
	exports.sync = sync;
	const checkPathExt = (path, options) => {
		const { pathExt = process.env.PATHEXT || "" } = options;
		const peSplit = pathExt.split(";");
		if (peSplit.indexOf("") !== -1) return true;
		for (let i = 0; i < peSplit.length; i++) {
			const p = peSplit[i].toLowerCase();
			const ext = _p_StringPrototypeSubstring(path, path.length - p.length).toLowerCase();
			if (p && ext === p) return true;
		}
		return false;
	};
	const checkStat = (stat, path, options) => stat.isFile() && checkPathExt(path, options);
}));

//#endregion
//#region node_modules/.pnpm/isexe@3.1.1/node_modules/isexe/dist/cjs/options.js
var require_options = /* @__PURE__ */ __commonJSMin(((exports) => {
	_p_ObjectDefineProperty(exports, "__esModule", { value: true });
}));

//#endregion
//#region node_modules/.pnpm/isexe@3.1.1/node_modules/isexe/dist/cjs/index.js
var require_cjs = /* @__PURE__ */ __commonJSMin(((exports) => {
	var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m, k, k2) {
		if (k2 === void 0) k2 = k;
		var desc = _p_ObjectGetOwnPropertyDescriptor(m, k);
		if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) desc = {
			enumerable: true,
			get: function() {
				return m[k];
			}
		};
		_p_ObjectDefineProperty(o, k2, desc);
	}) : (function(o, m, k, k2) {
		if (k2 === void 0) k2 = k;
		o[k2] = m[k];
	}));
	var __setModuleDefault = exports && exports.__setModuleDefault || (Object.create ? (function(o, v) {
		_p_ObjectDefineProperty(o, "default", {
			enumerable: true,
			value: v
		});
	}) : function(o, v) {
		o["default"] = v;
	});
	var __importStar = exports && exports.__importStar || function(mod) {
		if (mod && mod.__esModule) return mod;
		var result = {};
		if (mod != null) {
			for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
		}
		__setModuleDefault(result, mod);
		return result;
	};
	var __exportStar = exports && exports.__exportStar || function(m, exports$1) {
		for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$1, p)) __createBinding(exports$1, m, p);
	};
	_p_ObjectDefineProperty(exports, "__esModule", { value: true });
	exports.sync = exports.isexe = exports.posix = exports.win32 = void 0;
	const posix = __importStar(require_posix());
	exports.posix = posix;
	const win32 = __importStar(require_win32());
	exports.win32 = win32;
	__exportStar(require_options(), exports);
	const impl = (process.env._ISEXE_TEST_PLATFORM_ || process.platform) === "win32" ? win32 : posix;
	/**
	* Determine whether a path is executable on the current platform.
	*/
	exports.isexe = impl.isexe;
	/**
	* Synchronously determine whether a path is executable on the
	* current platform.
	*/
	exports.sync = impl.sync;
}));

//#endregion
//#region node_modules/.pnpm/which@5.0.0/node_modules/which/lib/index.js
var require_lib = /* @__PURE__ */ __commonJSMin(((exports, module) => {
	const { isexe, sync: isexeSync } = require_cjs();
	const { join, delimiter, sep, posix } = require("path");
	const isWindows = process.platform === "win32";
	/* istanbul ignore next */
	const rSlash = new _p_RegExpCtor(`[${posix.sep}${sep === posix.sep ? "" : sep}]`.replace(/(\\)/g, "\\$1"));
	const rRel = new _p_RegExpCtor(`^\\.${rSlash.source}`);
	const getNotFoundError = (cmd) => _p_ObjectAssign(/* @__PURE__ */ new _p_ErrorCtor(`not found: ${cmd}`), { code: "ENOENT" });
	const getPathInfo = (cmd, { path: optPath = process.env.PATH, pathExt: optPathExt = process.env.PATHEXT, delimiter: optDelimiter = delimiter }) => {
		const pathEnv = cmd.match(rSlash) ? [""] : [...isWindows ? [process.cwd()] : [], ...(optPath || "").split(optDelimiter)];
		if (isWindows) {
			const pathExtExe = optPathExt || [
				".EXE",
				".CMD",
				".BAT",
				".COM"
			].join(optDelimiter);
			const pathExt = pathExtExe.split(optDelimiter).flatMap((item) => [item, item.toLowerCase()]);
			if (cmd.includes(".") && pathExt[0] !== "") _p_ArrayPrototypeUnshift(pathExt, "");
			return {
				pathEnv,
				pathExt,
				pathExtExe
			};
		}
		return {
			pathEnv,
			pathExt: [""]
		};
	};
	const getPathPart = (raw, cmd) => {
		const pathPart = /^".*"$/.test(raw) ? raw.slice(1, -1) : raw;
		return (!pathPart && rRel.test(cmd) ? cmd.slice(0, 2) : "") + join(pathPart, cmd);
	};
	const which = async (cmd, opt = {}) => {
		const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
		const found = [];
		for (const envPart of pathEnv) {
			const p = getPathPart(envPart, cmd);
			for (const ext of pathExt) {
				const withExt = p + ext;
				if (await isexe(withExt, {
					pathExt: pathExtExe,
					ignoreErrors: true
				})) {
					if (!opt.all) return withExt;
					found.push(withExt);
				}
			}
		}
		if (opt.all && found.length) return found;
		if (opt.nothrow) return null;
		throw getNotFoundError(cmd);
	};
	const whichSync = (cmd, opt = {}) => {
		const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
		const found = [];
		for (const pathEnvPart of pathEnv) {
			const p = getPathPart(pathEnvPart, cmd);
			for (const ext of pathExt) {
				const withExt = p + ext;
				if (isexeSync(withExt, {
					pathExt: pathExtExe,
					ignoreErrors: true
				})) {
					if (!opt.all) return withExt;
					found.push(withExt);
				}
			}
		}
		if (opt.all && found.length) return found;
		if (opt.nothrow) return null;
		throw getNotFoundError(cmd);
	};
	module.exports = which;
	which.sync = whichSync;
}));

//#endregion
//#region src/external/which.js
module.exports = require_lib();

//#endregion