"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 lifecycle_exports = {};
__export(lifecycle_exports, {
  getSignalListeners: () => getSignalListeners,
  load: () => load,
  unload: () => unload
});
module.exports = __toCommonJS(lifecycle_exports);
var import_internal = require("./_internal");
var import_intercept = require("./intercept");
let _sigListeners;
// @__NO_SIDE_EFFECTS__
function getSignalListeners() {
  if (_sigListeners === void 0) {
    _sigListeners = { __proto__: null };
    const emitter = (0, import_internal.getEmitter)();
    const sigs = (0, import_internal.getSignals)();
    for (const sig of sigs) {
      _sigListeners[sig] = function listener() {
        const listeners = import_internal.globalProcess?.listeners(sig) || [];
        if (listeners.length === emitter.count) {
          /* @__PURE__ */ unload();
          (0, import_internal.emit)("exit", void 0, sig);
          (0, import_internal.emit)("afterexit", void 0, sig);
          const killSig = import_internal.WIN32 && sig === "SIGHUP" ? "SIGINT" : sig;
          import_internal.globalProcess?.kill(import_internal.globalProcess?.pid, killSig);
        }
      };
    }
  }
  return _sigListeners;
}
// @__NO_SIDE_EFFECTS__
function load() {
  if ((0, import_internal.isLoaded)() || !import_internal.globalProcess) {
    return;
  }
  (0, import_internal.setLoaded)(true);
  const proc = import_internal.globalProcess;
  const emitter = (0, import_internal.getEmitter)();
  if (emitter.count !== void 0) {
    emitter.count += 1;
  }
  const sigs = (0, import_internal.getSignals)();
  const sigListeners = /* @__PURE__ */ getSignalListeners();
  (0, import_internal.setSignals)(
    sigs.filter((sig) => {
      try {
        proc.on(sig, sigListeners[sig]);
        return true;
      } catch {
      }
      return false;
    })
  );
  proc.emit = import_intercept.processEmit;
  proc.reallyExit = import_intercept.processReallyExit;
}
// @__NO_SIDE_EFFECTS__
function unload() {
  if (!(0, import_internal.isLoaded)() || !import_internal.globalProcess) {
    return;
  }
  (0, import_internal.setLoaded)(false);
  const sigs = (0, import_internal.getSignals)();
  const sigListeners = /* @__PURE__ */ getSignalListeners();
  for (const sig of sigs) {
    try {
      import_internal.globalProcess.removeListener(
        sig,
        sigListeners[sig]
      );
    } catch {
    }
  }
  import_internal.globalProcess.emit = import_internal.originalProcessEmit;
  if (import_internal.originalProcessReallyExit !== void 0) {
    import_internal.globalProcess.reallyExit = import_internal.originalProcessReallyExit;
  }
  const emitter = (0, import_internal.getEmitter)();
  if (emitter.count !== void 0) {
    emitter.count -= 1;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getSignalListeners,
  load,
  unload
});
