"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_error = require('../primordials/error.js');
const require_constants_platform = require('../constants/platform.js');
const require_paths_predicates = require('../paths/predicates.js');
const require_primordials_array = require('../primordials/array.js');
const require_node_fs = require('../node/fs.js');
const require_bin__internal = require('./_internal.js');
const require_bin_resolve = require('./resolve.js');
const require_bin_which = require('./which.js');
const require_process_spawn_child = require('../process/spawn/child.js');

//#region src/bin/exec.ts
/**
* @file `execBin` — spawn a binary with PATH resolution, wrapper-script
*   unwrapping, and Windows shell handling. Order of operations:
*
*   1. If the input looks like a path (absolute or relative), `resolveRealBinSync`
*      it directly so we can spawn the underlying script rather than a
*      wrapper.
*   2. Otherwise treat it as a binary name and:
*
*   - Hit `binPathCache`. If valid (`existsSync`), use the cached path; if stale,
*     evict.
*   - Fall through to `whichReal` for a fresh PATH search; cache the result on
*     success.
*
*   3. Spawn with `shell: WIN32` so `.cmd` wrappers work on Windows; POSIX bypass
*      since direct `execve` is faster.
*   4. On not-found, throw a typed `ENOENT` error with operator guidance — far
*      more useful than a bare "command not found".
*/
/**
* Execute a binary with the given arguments.
*
* @example
*   ;```typescript
*   await execBin('pnpm', ['install'])
*   await execBin('/usr/local/bin/node', ['script.js'], { cwd: '/tmp' })
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
async function execBin(binPath, args, options) {
	let resolvedPath;
	if (/* @__PURE__ */ require_paths_predicates.isPath(binPath)) resolvedPath = /* @__PURE__ */ require_bin_resolve.resolveRealBinSync(binPath);
	else {
		const cached = require_bin__internal.binPathCache.get(binPath);
		/* c8 ignore start */
		if (cached) if ((/* @__PURE__ */ require_node_fs.getNodeFs()).existsSync(cached)) resolvedPath = cached;
		else require_bin__internal.binPathCache.delete(binPath);
		/* c8 ignore stop */
		if (!resolvedPath) {
			resolvedPath = await require_bin_which.whichReal(binPath);
			if (typeof resolvedPath === "string") require_bin__internal.binPathCache.set(binPath, resolvedPath);
		}
	}
	if (!resolvedPath) {
		const error = new require_primordials_error.ErrorCtor(`Binary not found: ${binPath}\nPossible causes:
  - Binary "${binPath}" is not installed or not in PATH\n  - Binary name is incorrect or misspelled
  - Installation directory is not in system PATH
To resolve:
  1. Verify "${binPath}" is installed: which ${binPath} (Unix) or where ${binPath} (Windows)\n  2. Install the binary if missing, ex: npm install -g ${binPath}\n  3. Check PATH environment variable includes the binary location`);
		error.code = "ENOENT";
		throw error;
	}
	/* c8 ignore stop */
	return await require_process_spawn_child.spawn(require_primordials_array.ArrayIsArray(resolvedPath) ? resolvedPath[0] : resolvedPath, args ?? [], {
		shell: require_constants_platform.WIN32,
		...options
	});
}

//#endregion
exports.execBin = execBin;