"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_math = require('../primordials/math.js');
const require_primordials_number = require('../primordials/number.js');

//#region src/abort/signal.ts
/**
* @file Abort signal utilities — composite signal construction from multiple
*   sources and timeout-driven signal creation.
*/
/**
* Create a composite AbortSignal from multiple signals.
*
* @example
*   ;```typescript
*   const ac1 = new AbortController()
*   const ac2 = new AbortController()
*   const signal = createCompositeAbortSignal(ac1.signal, ac2.signal)
*   ```
*/
function createCompositeAbortSignal(...signals) {
	const validSignals = signals.filter((s) => s != null);
	if (validSignals.length === 0) return new AbortController().signal;
	if (validSignals.length === 1) return validSignals[0];
	const controller = new AbortController();
	for (const signal of validSignals) {
		if (signal.aborted) {
			controller.abort();
			return controller.signal;
		}
		signal.addEventListener("abort", () => controller.abort(), { once: true });
	}
	return controller.signal;
}
/**
* Create an AbortSignal that triggers after a timeout.
*
* @example
*   ;```typescript
*   const signal = createTimeoutSignal(5000) // aborts after 5 seconds
*   fetch('https://example.com', { signal })
*   ```
*
* @throws {TypeError} If `ms` is not a number, is NaN, is not finite, or is not
*   positive.
*/
function createTimeoutSignal(ms) {
	if (typeof ms !== "number" || require_primordials_number.NumberIsNaN(ms)) throw new require_primordials_error.TypeErrorCtor("timeout must be a number");
	if (!require_primordials_number.NumberIsFinite(ms)) throw new require_primordials_error.TypeErrorCtor("timeout must be a finite number");
	if (ms <= 0) throw new require_primordials_error.TypeErrorCtor("timeout must be a positive number");
	return AbortSignal.timeout(require_primordials_math.MathCeil(ms));
}

//#endregion
exports.createCompositeAbortSignal = createCompositeAbortSignal;
exports.createTimeoutSignal = createTimeoutSignal;