"use strict";
/* Socket Lib - Built with esbuild */
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var spawn_exports = {};
__export(spawn_exports, {
  isSpawnError: () => isSpawnError,
  isStdioType: () => isStdioType,
  spawn: () => spawn,
  spawnSync: () => spawnSync
});
module.exports = __toCommonJS(spawn_exports);
var import_process = require("./constants/process");
var import_promise_spawn = __toESM(require("./external/@npmcli/promise-spawn"));
var import_node_path = __toESM(require("node:path"));
var import_arrays = require("./arrays");
var import_bin = require("./bin");
var import_normalize = require("./paths/normalize");
var import_objects = require("./objects");
var import_spinner = require("./spinner");
var import_strings = require("./strings");
const abortSignal = (0, import_process.getAbortSignal)();
const spinner = (0, import_spinner.getDefaultSpinner)();
const windowsScriptExtRegExp = /\.(?:cmd|bat|ps1)$/i;
let _child_process;
// @__NO_SIDE_EFFECTS__
function getChildProcess() {
  if (_child_process === void 0) {
    _child_process = require("node:child_process");
  }
  return _child_process;
}
// @__NO_SIDE_EFFECTS__
function isSpawnError(value) {
  if (value === null || typeof value !== "object") {
    return false;
  }
  const err = value;
  return (0, import_objects.hasOwn)(err, "code") && typeof err["code"] !== "undefined" || (0, import_objects.hasOwn)(err, "errno") && typeof err["errno"] !== "undefined" || (0, import_objects.hasOwn)(err, "syscall") && typeof err["syscall"] === "string";
}
// @__NO_SIDE_EFFECTS__
function isStdioType(stdio, type) {
  if (arguments.length === 1) {
    const validTypes = ["pipe", "ignore", "inherit", "overlapped"];
    return typeof stdio === "string" && validTypes.includes(stdio);
  }
  return stdio === type || (stdio === null || stdio === void 0) && type === "pipe" || (0, import_arrays.isArray)(stdio) && stdio.length > 2 && stdio[0] === type && stdio[1] === type && stdio[2] === type;
}
// @__NO_SIDE_EFFECTS__
function stripAnsiFromSpawnResult(result) {
  const res = result;
  const { stderr, stdout } = res;
  if (typeof stdout === "string") {
    res.stdout = (0, import_strings.stripAnsi)(stdout);
  }
  if (typeof stderr === "string") {
    res.stderr = (0, import_strings.stripAnsi)(stderr);
  }
  return res;
}
function spawn(cmd, args, options, extra) {
  const {
    spinner: optionsSpinner = spinner,
    stripAnsi: shouldStripAnsi = true,
    ...rawSpawnOptions
  } = { __proto__: null, ...options };
  const spinnerInstance = optionsSpinner;
  const spawnOptions = { __proto__: null, ...rawSpawnOptions };
  const { env, shell, stdio, stdioString = true } = spawnOptions;
  const cwd = spawnOptions.cwd ? String(spawnOptions.cwd) : void 0;
  let actualCmd = cmd;
  if (!(0, import_normalize.isPath)(cmd)) {
    const resolved = (0, import_bin.whichSync)(cmd, { cwd, nothrow: true });
    if (resolved && typeof resolved === "string") {
      actualCmd = resolved;
    }
  }
  const WIN32 = process.platform === "win32";
  if (WIN32 && shell && windowsScriptExtRegExp.test(actualCmd)) {
    actualCmd = import_node_path.default.basename(actualCmd, import_node_path.default.extname(actualCmd));
  }
  const wasSpinning = !!spinnerInstance?.isSpinning;
  const shouldStopSpinner = wasSpinning && !/* @__PURE__ */ isStdioType(stdio, "ignore") && !/* @__PURE__ */ isStdioType(stdio, "pipe");
  const shouldRestartSpinner = shouldStopSpinner;
  if (shouldStopSpinner) {
    spinnerInstance.stop();
  }
  const envToUse = env ? {
    __proto__: null,
    ...process.env,
    ...env
  } : process.env;
  const promiseSpawnOpts = {
    __proto__: null,
    cwd: typeof spawnOptions.cwd === "string" ? spawnOptions.cwd : void 0,
    env: envToUse,
    signal: abortSignal,
    stdio: spawnOptions.stdio,
    stdioString,
    shell: spawnOptions.shell,
    windowsVerbatimArguments: spawnOptions.windowsVerbatimArguments,
    timeout: spawnOptions.timeout,
    uid: spawnOptions.uid,
    gid: spawnOptions.gid
  };
  const spawnPromise = (0, import_promise_spawn.default)(
    actualCmd,
    args ? [...args] : [],
    promiseSpawnOpts,
    extra
  );
  const oldSpawnPromise = spawnPromise;
  let newSpawnPromise;
  if (shouldStripAnsi && stdioString) {
    newSpawnPromise = spawnPromise.then((result) => {
      const strippedResult = /* @__PURE__ */ stripAnsiFromSpawnResult(result);
      if ("code" in strippedResult) {
        ;
        strippedResult.exitCode = strippedResult.code;
      }
      return strippedResult;
    }).catch((error) => {
      throw /* @__PURE__ */ stripAnsiFromSpawnResult(error);
    });
  } else {
    newSpawnPromise = spawnPromise.then((result) => {
      if ("code" in result) {
        const res = result;
        res.exitCode = result.code;
        return res;
      }
      return result;
    });
  }
  if (shouldRestartSpinner) {
    newSpawnPromise = newSpawnPromise.finally(() => {
      spinnerInstance.start();
    });
  }
  ;
  newSpawnPromise.process = oldSpawnPromise.process;
  newSpawnPromise.stdin = oldSpawnPromise.stdin;
  return newSpawnPromise;
}
function spawnSync(cmd, args, options) {
  let actualCmd = cmd;
  if (!(0, import_normalize.isPath)(cmd)) {
    const resolved = (0, import_bin.whichSync)(cmd, {
      cwd: (0, import_objects.getOwn)(options, "cwd"),
      nothrow: true
    });
    if (resolved && typeof resolved === "string") {
      actualCmd = resolved;
    }
  }
  const shell = (0, import_objects.getOwn)(options, "shell");
  const WIN32 = process.platform === "win32";
  if (WIN32 && shell && windowsScriptExtRegExp.test(actualCmd)) {
    actualCmd = import_node_path.default.basename(actualCmd, import_node_path.default.extname(actualCmd));
  }
  const { stripAnsi: shouldStripAnsi = true, ...rawSpawnOptions } = {
    __proto__: null,
    ...options
  };
  const { stdioString: rawStdioString = true } = rawSpawnOptions;
  const rawEncoding = rawStdioString ? "utf8" : "buffer";
  const spawnOptions = {
    encoding: rawEncoding,
    ...rawSpawnOptions
  };
  const stdioString = spawnOptions.encoding !== "buffer";
  const result = (/* @__PURE__ */ getChildProcess()).spawnSync(actualCmd, args, spawnOptions);
  if (stdioString) {
    const { stderr, stdout } = result;
    if (stdout) {
      result.stdout = stdout.toString().trim();
    }
    if (stderr) {
      result.stderr = stderr.toString().trim();
    }
  }
  return shouldStripAnsi && stdioString ? /* @__PURE__ */ stripAnsiFromSpawnResult(result) : result;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  isSpawnError,
  isStdioType,
  spawn,
  spawnSync
});
