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

//#region src/events/exit/intercept.ts
/**
* @file Process-method interceptors — `processEmit` replaces `process.emit` and
*   `processReallyExit` replaces `process.reallyExit` while `load()` is active.
*   Both forward to the originals captured in `_internal` and broadcast `exit`
*   / `afterexit` through the signal-exit emitter.
*/
/* c8 ignore start - processEmit + processReallyExit interceptors
only fire on real process exit/emit; can't be triggered in-test. */
/*@__NO_SIDE_EFFECTS__*/
function processEmit(eventName, exitCode, ...args) {
	if (eventName === "exit") {
		let actualExitCode = exitCode;
		if (actualExitCode === void 0) {
			const processExitCode = require_events_exit__internal.globalProcess?.exitCode;
			actualExitCode = typeof processExitCode === "number" ? processExitCode : void 0;
		} else if (require_events_exit__internal.globalProcess) require_events_exit__internal.globalProcess.exitCode = actualExitCode;
		const result = require_primordials_reflect.ReflectApply(require_events_exit__internal.originalProcessEmit, this, [
			eventName,
			actualExitCode,
			...args
		]);
		const numExitCode = typeof actualExitCode === "number" ? actualExitCode : void 0;
		/* @__PURE__ */ require_events_exit__internal.emit("exit", numExitCode, void 0);
		/* @__PURE__ */ require_events_exit__internal.emit("afterexit", numExitCode, void 0);
		return result;
	}
	return require_primordials_reflect.ReflectApply(require_events_exit__internal.originalProcessEmit, this, [
		eventName,
		exitCode,
		...args
	]);
}
/*@__NO_SIDE_EFFECTS__*/
function processReallyExit(code) {
	const exitCode = code || 0;
	if (require_events_exit__internal.globalProcess) require_events_exit__internal.globalProcess.exitCode = exitCode;
	/* @__PURE__ */ require_events_exit__internal.emit("exit", exitCode, void 0);
	/* @__PURE__ */ require_events_exit__internal.emit("afterexit", exitCode, void 0);
	require_primordials_reflect.ReflectApply(require_events_exit__internal.originalProcessReallyExit, require_events_exit__internal.globalProcess, [exitCode]);
	throw new require_primordials_error.ErrorCtor("processReallyExit should never return");
}
/* c8 ignore stop */

//#endregion
exports.processEmit = processEmit;
exports.processReallyExit = processReallyExit;