const { ErrorCtor: _p_ErrorCtor, TypeErrorCtor: _p_TypeErrorCtor } = require('../primordials/error.js')
const { JSONParse: _p_JSONParse, JSONStringify: _p_JSONStringify } = require('../primordials/json.js')

const { MathRandom: _p_MathRandom } = require('../primordials/math.js')

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

const { StringPrototypeCharAt: _p_StringPrototypeCharAt } = require('../primordials/string.js')

"use strict";
/**
 * Bundled from shell-quote
 * 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/shell-quote@1.8.4/node_modules/shell-quote/quote.js
var require_quote = /* @__PURE__ */ __commonJSMin(((exports, module) => {
	var OPS = [
		"||",
		"&&",
		";;",
		"|&",
		"<(",
		"<<<",
		">>",
		">&",
		"<&",
		"&",
		";",
		"(",
		")",
		"|",
		"<",
		">"
	];
	var LINE_TERMINATORS = /[\n\r\u2028\u2029]/;
	var GLOB_SHELL_SPECIAL = /[\s#!"$&'():;<=>@\\^`|]/g;
	module.exports = function quote(xs) {
		return xs.map(function(s) {
			if (s === "") return "''";
			if (s && typeof s === "object") {
				if (s.op === "glob") {
					if (typeof s.pattern !== "string") throw new _p_TypeErrorCtor("glob token requires a string `pattern`");
					if (LINE_TERMINATORS.test(s.pattern)) throw new _p_TypeErrorCtor("glob `pattern` must not contain line terminators");
					return s.pattern.replace(GLOB_SHELL_SPECIAL, "\\$&");
				}
				if (typeof s.op === "string") {
					if (OPS.indexOf(s.op) < 0) throw new _p_TypeErrorCtor("invalid `op` value: " + _p_JSONStringify(s.op));
					return s.op.replace(/[\s\S]/g, "\\$&");
				}
				if (typeof s.comment === "string") {
					if (LINE_TERMINATORS.test(s.comment)) throw new _p_TypeErrorCtor("`comment` must not contain line terminators");
					return "#" + s.comment;
				}
				throw new _p_TypeErrorCtor("unrecognized object token shape");
			}
			if (/["\s\\]/.test(s) && !/'/.test(s)) return "'" + s.replace(/(['])/g, "\\$1") + "'";
			if (/["'\s]/.test(s)) return "\"" + s.replace(/(["\\$`!])/g, "\\$1") + "\"";
			return String(s).replace(/([A-Za-z]:)?([#!"$&'()*,:;<=>?@[\\\]^`{|}])/g, "$1\\$2");
		}).join(" ");
	};
}));

//#endregion
//#region node_modules/.pnpm/shell-quote@1.8.4/node_modules/shell-quote/parse.js
var require_parse = /* @__PURE__ */ __commonJSMin(((exports, module) => {
	var CONTROL = "(?:" + [
		"\\|\\|",
		"\\&\\&",
		";;",
		"\\|\\&",
		"\\<\\(",
		"\\<\\<\\<",
		">>",
		">\\&",
		"<\\&",
		"[&;()|<>]"
	].join("|") + ")";
	var controlRE = new _p_RegExpCtor("^" + CONTROL + "$");
	var META = "|&;()<> \\t";
	var SINGLE_QUOTE = "\"((\\\\\"|[^\"])*?)\"";
	var DOUBLE_QUOTE = "'((\\\\'|[^'])*?)'";
	var hash = /^#$/;
	var SQ = "'";
	var DQ = "\"";
	var DS = "$";
	var TOKEN = "";
	var mult = 4294967296;
	for (var i = 0; i < 4; i++) TOKEN += (mult * _p_MathRandom()).toString(16);
	var startsWithToken = new _p_RegExpCtor("^" + TOKEN);
	function matchAll(s, r) {
		var origIndex = r.lastIndex;
		var matches = [];
		var matchObj;
		while (matchObj = r.exec(s)) {
			matches.push(matchObj);
			if (r.lastIndex === matchObj.index) r.lastIndex += 1;
		}
		r.lastIndex = origIndex;
		return matches;
	}
	function getVar(env, pre, key) {
		var r = typeof env === "function" ? env(key) : env[key];
		if (typeof r === "undefined" && key != "") r = "";
		else if (typeof r === "undefined") r = "$";
		if (typeof r === "object") return pre + TOKEN + _p_JSONStringify(r) + TOKEN;
		return pre + r;
	}
	function parseInternal(string, env, opts) {
		if (!opts) opts = {};
		var BS = opts.escape || "\\";
		var BAREWORD = "(\\" + BS + "['\"" + META + "]|[^\\s'\"" + META + "])+";
		var matches = matchAll(string, new RegExp(["(" + CONTROL + ")", "(" + BAREWORD + "|" + SINGLE_QUOTE + "|" + DOUBLE_QUOTE + ")+"].join("|"), "g"));
		if (matches.length === 0) return [];
		if (!env) env = {};
		var commented = false;
		return matches.map(function(match) {
			var s = match[0];
			if (!s || commented) return;
			if (controlRE.test(s)) return { op: s };
			var quote = false;
			var esc = false;
			var out = "";
			var isGlob = false;
			var i;
			function parseEnvVar() {
				i += 1;
				var varend;
				var varname;
				var char = _p_StringPrototypeCharAt(s, i);
				if (char === "{") {
					i += 1;
					if (_p_StringPrototypeCharAt(s, i) === "}") throw new _p_ErrorCtor("Bad substitution: " + s.slice(i - 2, i + 1));
					varend = s.indexOf("}", i);
					if (varend < 0) throw new _p_ErrorCtor("Bad substitution: " + s.slice(i));
					varname = s.slice(i, varend);
					i = varend;
				} else if (/[*@#?$!_-]/.test(char)) {
					varname = char;
					i += 1;
				} else {
					var slicedFromI = s.slice(i);
					varend = slicedFromI.match(/[^\w\d_]/);
					if (!varend) {
						varname = slicedFromI;
						i = s.length;
					} else {
						varname = slicedFromI.slice(0, varend.index);
						i += varend.index - 1;
					}
				}
				return getVar(env, "", varname);
			}
			for (i = 0; i < s.length; i++) {
				var c = _p_StringPrototypeCharAt(s, i);
				isGlob = isGlob || !quote && (c === "*" || c === "?");
				if (esc) {
					out += c;
					esc = false;
				} else if (quote) if (c === quote) quote = false;
				else if (quote == SQ) out += c;
				else if (c === BS) {
					i += 1;
					c = _p_StringPrototypeCharAt(s, i);
					if (c === DQ || c === BS || c === DS) out += c;
					else out += BS + c;
				} else if (c === DS) out += parseEnvVar();
				else out += c;
				else if (c === DQ || c === SQ) quote = c;
				else if (controlRE.test(c)) return { op: s };
				else if (hash.test(c)) {
					commented = true;
					var commentObj = { comment: string.slice(match.index + i + 1) };
					if (out.length) return [out, commentObj];
					return [commentObj];
				} else if (c === BS) esc = true;
				else if (c === DS) out += parseEnvVar();
				else out += c;
			}
			if (isGlob) return {
				op: "glob",
				pattern: out
			};
			return out;
		}).reduce(function(prev, arg) {
			return typeof arg === "undefined" ? prev : prev.concat(arg);
		}, []);
	}
	module.exports = function parse(s, env, opts) {
		var mapped = parseInternal(s, env, opts);
		if (typeof env !== "function") return mapped;
		return mapped.reduce(function(acc, s) {
			if (typeof s === "object") return acc.concat(s);
			var xs = s.split(RegExp("(" + TOKEN + ".*?" + TOKEN + ")", "g"));
			if (xs.length === 1) return acc.concat(xs[0]);
			return acc.concat(xs.filter(Boolean).map(function(x) {
				if (startsWithToken.test(x)) return _p_JSONParse(x.split(TOKEN)[1]);
				return x;
			}));
		}, []);
	};
}));

//#endregion
//#region node_modules/.pnpm/shell-quote@1.8.4/node_modules/shell-quote/index.js
var require_shell_quote = /* @__PURE__ */ __commonJSMin(((exports) => {
	exports.quote = require_quote();
	exports.parse = require_parse();
}));

//#endregion
//#region src/external/shell-quote.js
module.exports = require_shell_quote();

//#endregion