"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../../_virtual/_rolldown/runtime.js');
const require_primordials_map_set = require('../../primordials/map-set.js');
const require_primordials_reflect = require('../../primordials/reflect.js');
let node_process = require("node:process");
node_process = require_runtime.__toESM(node_process);

//#region src/events/warning/suppress.ts
/**
* @file `process.emitWarning` suppression. Single shared wrapper installed on
*   first call, driven by a membership Set so repeated `suppressWarningType`
*   calls are cheap.
*/
let originalEmitWarning;
const suppressedWarnings = new require_primordials_map_set.SetCtor();
/**
* Restore the original process.emitWarning function. Call this to re-enable all
* warnings after suppressing them.
*
* @example
*   ;```typescript
*   suppressMaxListenersWarning()
*   // ... do work ...
*   restoreWarnings() // Re-enable all warnings
*   ```
*/
function restoreWarnings() {
	if (originalEmitWarning) {
		node_process.default.emitWarning = originalEmitWarning;
		originalEmitWarning = void 0;
		suppressedWarnings.clear();
	}
}
/**
* Internal function to set up warning suppression. Only wraps
* process.emitWarning once, regardless of how many times it's called.
*/
function setupSuppression() {
	/* c8 ignore start */
	if (!originalEmitWarning) {
		originalEmitWarning = node_process.default.emitWarning;
		node_process.default.emitWarning = (warning, ...args) => {
			if (typeof warning === "string") {
				for (const suppressedType of suppressedWarnings) if (warning.includes(suppressedType)) return;
			} else if (warning && typeof warning === "object") {
				/* c8 ignore start - Object-shaped warning suppression
				(Error / Warning instances). process.emitWarning rarely
				passes object form in test runs; covered when consumers
				pass real Warning subclasses. */
				const warningObj = warning;
				if (warningObj.name && suppressedWarnings.has(warningObj.name)) return;
			}
			return require_primordials_reflect.ReflectApply(originalEmitWarning, node_process.default, [warning, ...args]);
		};
	}
	/* c8 ignore stop */
}
/**
* Silence `MaxListenersExceededWarning` messages from `process.emitWarning`.
* Installs a single shared wrapper around `process.emitWarning` on first call
* so repeat invocations are cheap.
*
* @example
*   import { suppressMaxListenersWarning } from '@socketsecurity/lib/events/warning/suppress'
*
*   suppressMaxListenersWarning()
*/
function suppressMaxListenersWarning() {
	suppressedWarnings.add("MaxListenersExceededWarning");
	setupSuppression();
}
/**
* Suppress all process warnings of a specific type.
*
* @example
*   import { suppressWarningType } from '@socketsecurity/lib/events/warning/suppress'
*
*   suppressWarningType('ExperimentalWarning')
*
* @param warningType - The warning type to suppress (e.g.,
*   'DeprecationWarning', 'ExperimentalWarning')
*/
function suppressWarningType(warningType) {
	suppressedWarnings.add(warningType);
	setupSuppression();
}
/**
* Suppress warnings temporarily within a callback.
*
* @example
*   import { withSuppressedWarnings } from '@socketsecurity/lib/events/warning/suppress'
*
*   const result = await withSuppressedWarnings(
*     'ExperimentalWarning',
*     async () => {
*       // Code that triggers experimental warnings
*       return someValue
*     },
*   )
*
* @param warningType - The warning type to suppress.
* @param callback - Function to execute with warnings suppressed.
*
* @returns The result of the callback
*/
async function withSuppressedWarnings(warningType, callback) {
	const wasAlreadySuppressed = suppressedWarnings.has(warningType);
	suppressWarningType(warningType);
	try {
		return await callback();
	} finally {
		if (!wasAlreadySuppressed) suppressedWarnings.delete(warningType);
	}
}

//#endregion
exports.restoreWarnings = restoreWarnings;
exports.setupSuppression = setupSuppression;
exports.suppressMaxListenersWarning = suppressMaxListenersWarning;
exports.suppressWarningType = suppressWarningType;
exports.withSuppressedWarnings = withSuppressedWarnings;