const require_rolldown_runtime = require('../../../../../../../../../_virtual/rolldown_runtime.cjs');
const require_spawnPosix = require('./spawnPosix.cjs');
const require_spawnWin32 = require('./spawnWin32.cjs');
let node_path = require("node:path");

//#region ../@intlayer/cli/dist/esm/chokidar/dist/esm/utils/runParallel/index.mjs
/**
* Start a cross-platform parallel process using npm-run-all approach.
* Accepts either a single string (e.g., 'next start') or an array of tokens (e.g., ['next', 'start']).
*/
const runParallel = (proc) => {
	if (!proc || Array.isArray(proc) && proc.length === 0) throw new Error("Invalid command");
	const commandText = Array.isArray(proc) ? proc.join(" ") : proc;
	const isArray = Array.isArray(proc);
	const command = isArray ? proc[0] : commandText;
	const args = isArray ? proc.slice(1) : [];
	const cwdBin = (0, node_path.join)(process.cwd(), "node_modules", ".bin");
	const PATH_KEY = Object.keys(process.env).find((key) => key.toLowerCase() === "path") ?? "PATH";
	const extendedPath = [cwdBin, process.env[PATH_KEY] ?? ""].filter(Boolean).join(node_path.delimiter);
	const childEnv = {
		...process.env,
		[PATH_KEY]: extendedPath
	};
	const isWin = process.platform === "win32";
	const spawnFunc = isWin ? require_spawnWin32.spawnWin32 : require_spawnPosix.spawnPosix;
	const spawnOptions = {
		cwd: process.cwd(),
		stdio: "inherit",
		env: childEnv,
		shell: false
	};
	const child = isArray ? args.length === 0 && /\s/.test(command) ? isWin ? spawnFunc(process.env.ComSpec ?? "cmd.exe", [
		"/d",
		"/s",
		"/c",
		command
	], spawnOptions) : spawnFunc(process.env.SHELL ?? "/bin/sh", ["-c", command], spawnOptions) : spawnFunc(command, args, spawnOptions) : isWin ? spawnFunc(process.env.ComSpec ?? "cmd.exe", [
		"/d",
		"/s",
		"/c",
		commandText
	], spawnOptions) : spawnFunc(process.env.SHELL ?? "/bin/sh", ["-c", commandText], spawnOptions);
	const result = new Promise((resolve$1, reject) => {
		child.on("error", (err) => {
			try {
				console.error(`[runParallel] Failed to start: ${err?.message ?? String(err)}`);
			} catch {}
			cleanupHandlers();
			reject(err);
		});
		child.on("exit", (code, signal) => {
			cleanupHandlers();
			if (code === 0 || signal && [
				"SIGINT",
				"SIGTERM",
				"SIGQUIT",
				"SIGHUP"
			].includes(signal)) resolve$1();
			else reject(Object.assign(/* @__PURE__ */ new Error("Parallel process failed"), {
				code,
				signal
			}));
		});
	});
	const cleanup = () => {
		try {
			child.kill("SIGTERM");
		} catch {}
	};
	const signalHandlers = [
		{
			event: "SIGINT",
			handler: cleanup
		},
		{
			event: "SIGTERM",
			handler: cleanup
		},
		{
			event: "SIGQUIT",
			handler: cleanup
		},
		{
			event: "SIGHUP",
			handler: cleanup
		}
	];
	signalHandlers.forEach(({ event, handler }) => {
		process.on(event, handler);
	});
	const cleanupHandlers = () => {
		signalHandlers.forEach(({ event, handler }) => {
			process.off(event, handler);
		});
	};
	const kill = () => {
		try {
			child.kill("SIGTERM");
		} catch {}
	};
	return {
		kill,
		result,
		commandText
	};
};

//#endregion
exports.runParallel = runParallel;
//# sourceMappingURL=index.cjs.map