"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_events_exit__internal = require('./_internal.js');
const require_events_exit_intercept = require('./intercept.js');

//#region src/events/exit/lifecycle.ts
/**
* @file `load` and `unload` — install and remove the `signal-exit` listeners.
*   Co-located with `getSignalListeners` because the per-signal listener body
*   calls `unload()`; keeping them in the same leaf avoids an inter-leaf import
*   cycle.
*/
let _sigListeners;
/*@__NO_SIDE_EFFECTS__*/
function getSignalListeners() {
	if (_sigListeners === void 0) {
		_sigListeners = { __proto__: null };
		const emitter = /* @__PURE__ */ require_events_exit__internal.getEmitter();
		const sigs = /* @__PURE__ */ require_events_exit__internal.getSignals();
		/* c8 ignore start - Signal-listener body fires only on real
		process signals; can't be triggered in-test without subprocess
		infrastructure. */
		for (const sig of sigs) _sigListeners[sig] = function listener() {
			if ((require_events_exit__internal.globalProcess?.listeners(sig) || []).length === emitter.count) {
				/* @__PURE__ */ require_events_exit__internal.emit("exit", void 0, sig);
				/* @__PURE__ */ require_events_exit__internal.emit("afterexit", void 0, sig);
				const killSig = require_events_exit__internal.WIN32 && sig === "SIGHUP" ? "SIGINT" : sig;
				require_events_exit__internal.globalProcess?.kill(require_events_exit__internal.globalProcess?.pid, killSig);
			}
		};
	}
	return _sigListeners;
}
/**
* Load signal handlers and hook into process exit events.
*
* @example
*   ;```typescript
*   load()
*   // Signal handlers are now active
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function load() {
	/* c8 ignore next - !globalProcess never fires in Node tests. */
	if (require_events_exit__internal.isLoaded() || !require_events_exit__internal.globalProcess) return;
	require_events_exit__internal.setLoaded(true);
	const proc = require_events_exit__internal.globalProcess;
	const emitter = /* @__PURE__ */ require_events_exit__internal.getEmitter();
	/* c8 ignore start */
	if (emitter.count !== void 0) emitter.count += 1;
	/* c8 ignore stop */
	const sigs = /* @__PURE__ */ require_events_exit__internal.getSignals();
	const sigListeners = /* @__PURE__ */ getSignalListeners();
	require_events_exit__internal.setSignals(sigs.filter((sig) => {
		try {
			proc.on(sig, sigListeners[sig]);
			return true;
		} catch {}
		return false;
		/* c8 ignore stop */
	}));
	proc.emit = require_events_exit_intercept.processEmit;
	proc.reallyExit = require_events_exit_intercept.processReallyExit;
}
/**
* Unload signal handlers and restore original process behavior.
*
* @example
*   ;```typescript
*   unload()
*   // Signal handlers are now removed
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function unload() {
	/* c8 ignore start */
	if (!require_events_exit__internal.isLoaded() || !require_events_exit__internal.globalProcess) return;
	/* c8 ignore stop */
	require_events_exit__internal.setLoaded(false);
	const sigs = /* @__PURE__ */ require_events_exit__internal.getSignals();
	const sigListeners = /* @__PURE__ */ getSignalListeners();
	for (const sig of sigs) try {
		require_events_exit__internal.globalProcess.removeListener(sig, sigListeners[sig]);
	} catch {}
	require_events_exit__internal.globalProcess.emit = require_events_exit__internal.originalProcessEmit;
	/* c8 ignore start */
	if (require_events_exit__internal.originalProcessReallyExit !== void 0) require_events_exit__internal.globalProcess.reallyExit = require_events_exit__internal.originalProcessReallyExit;
	/* c8 ignore stop */
	const emitter = /* @__PURE__ */ require_events_exit__internal.getEmitter();
	/* c8 ignore start */
	if (emitter.count !== void 0) emitter.count -= 1;
	/* c8 ignore stop */
}

//#endregion
exports.getSignalListeners = getSignalListeners;
exports.load = load;
exports.unload = unload;