"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var errors_exports = {};
__export(errors_exports, {
  enhanceSpawnError: () => enhanceSpawnError,
  isSpawnError: () => isSpawnError
});
module.exports = __toCommonJS(errors_exports);
var import_stack = require("../errors/stack");
var import_predicates = require("../objects/predicates");
var import_error = require("../primordials/error");
var import_object = require("../primordials/object");
var import_reflect = require("../primordials/reflect");
var import_internal = require("./_internal");
// @__NO_SIDE_EFFECTS__
function enhanceSpawnError(error) {
  if (error === null || typeof error !== "object") {
    return error;
  }
  if (!/* @__PURE__ */ isSpawnError(error)) {
    return error;
  }
  const err = error;
  const { args, cmd, code, signal, stderr } = err;
  const stderrText = typeof stderr === "string" ? stderr : stderr?.toString() ?? "";
  let enhancedMessage = `Command failed: ${cmd}`;
  if (args && args.length > 0) {
    const argsStr = args.join(" ");
    if (argsStr.length < 100) {
      enhancedMessage += ` ${argsStr}`;
    } else {
      enhancedMessage += ` ${argsStr.slice(0, 97)}...`;
    }
  }
  if (signal) {
    enhancedMessage += ` (terminated by ${signal})`;
  } else if (code !== void 0) {
    enhancedMessage += ` (exit code ${code})`;
  }
  const trimmedStderr = stderrText.trim();
  if (trimmedStderr) {
    const firstLine = trimmedStderr.split("\n")[0] ?? "";
    if (firstLine.length < 200) {
      enhancedMessage += `
${firstLine}`;
    } else {
      enhancedMessage += `
${firstLine.slice(0, 197)}...`;
    }
  }
  const isSynthetic = err.message === "command failed";
  if (isSynthetic) {
    (0, import_object.ObjectDefineProperty)(err, "message", {
      __proto__: null,
      value: enhancedMessage,
      writable: true,
      enumerable: false,
      configurable: true
    });
    return err;
  }
  const enhancedError = new import_error.ErrorCtor(enhancedMessage, {
    cause: err
  });
  const descriptors = (0, import_object.ObjectGetOwnPropertyDescriptors)(err);
  (0, import_reflect.ReflectDeleteProperty)(descriptors, "message");
  (0, import_reflect.ReflectDeleteProperty)(descriptors, "stack");
  (0, import_object.ObjectDefineProperties)(enhancedError, descriptors);
  (0, import_object.ObjectDefineProperty)(enhancedError, "stack", {
    __proto__: null,
    configurable: true,
    enumerable: false,
    get() {
      let stack = import_internal.stackCache.get(enhancedError);
      if (stack === void 0) {
        try {
          stack = (0, import_stack.stackWithCauses)(err);
        } catch {
          stack = err.stack ?? new import_error.ErrorCtor().stack ?? "";
        }
        import_internal.stackCache.set(enhancedError, stack);
      }
      return stack;
    }
  });
  return enhancedError;
}
// @__NO_SIDE_EFFECTS__
function isSpawnError(value) {
  if (value === null || typeof value !== "object") {
    return false;
  }
  const err = value;
  return (0, import_predicates.hasOwn)(err, "code") && typeof err["code"] !== "undefined" || (0, import_predicates.hasOwn)(err, "errno") && typeof err["errno"] !== "undefined" || (0, import_predicates.hasOwn)(err, "syscall") && typeof err["syscall"] === "string";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  enhanceSpawnError,
  isSpawnError
});
