//#region packages/gateway-client/src/timeouts.ts
function parseStrictPositiveInteger(value) {
	const trimmed = value.trim();
	if (!/^\+?\d+$/u.test(trimmed)) return;
	const parsed = Number(trimmed);
	return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : void 0;
}
const MAX_SAFE_TIMEOUT_DELAY_MS = 2147483647;
const DEFAULT_PREAUTH_HANDSHAKE_TIMEOUT_MS = 15e3;
const MAX_CONNECT_CHALLENGE_TIMEOUT_MS = DEFAULT_PREAUTH_HANDSHAKE_TIMEOUT_MS;
function resolveSafeTimeoutDelayMs(delayMs, opts) {
	const rawMinMs = opts?.minMs ?? 1;
	const minMs = Math.min(MAX_SAFE_TIMEOUT_DELAY_MS, Math.max(0, Number.isFinite(rawMinMs) ? Math.floor(rawMinMs) : 1));
	return Math.min(MAX_SAFE_TIMEOUT_DELAY_MS, Math.max(minMs, Number.isFinite(delayMs) ? Math.floor(delayMs) : minMs));
}
function addSafeTimeoutDelayGraceMs(delayMs, graceMs, opts) {
	if (!Number.isFinite(delayMs) || !Number.isFinite(graceMs)) return resolveSafeTimeoutDelayMs(MAX_SAFE_TIMEOUT_DELAY_MS, opts);
	const withGrace = delayMs + graceMs;
	return resolveSafeTimeoutDelayMs(Number.isFinite(withGrace) ? withGrace : MAX_SAFE_TIMEOUT_DELAY_MS, opts);
}
function resolveFiniteTimeoutDelayMs(delayMs, fallbackMs, opts) {
	return resolveSafeTimeoutDelayMs(typeof delayMs === "number" && Number.isFinite(delayMs) ? delayMs : fallbackMs, opts);
}
function clampConnectChallengeTimeoutMs(timeoutMs, maxTimeoutMs = MAX_CONNECT_CHALLENGE_TIMEOUT_MS) {
	return Math.max(250, Math.min(Math.max(250, maxTimeoutMs), timeoutMs));
}
function getConnectChallengeTimeoutMsFromEnv(env = process.env) {
	const raw = env.NODMIX_CONNECT_CHALLENGE_TIMEOUT_MS;
	if (raw) {
		const parsed = parseStrictPositiveInteger(raw);
		if (parsed !== void 0) return resolveSafeTimeoutDelayMs(parsed);
	}
}
function normalizePositiveTimeoutMs(timeoutMs) {
	return typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0 ? resolveSafeTimeoutDelayMs(timeoutMs) : void 0;
}
function resolveConnectChallengeTimeoutMs(timeoutMs, params) {
	const configuredPreauthTimeoutMs = resolvePreauthHandshakeTimeoutMs({
		env: params?.env,
		configuredTimeoutMs: params?.configuredTimeoutMs
	});
	const maxTimeoutMs = Math.max(DEFAULT_PREAUTH_HANDSHAKE_TIMEOUT_MS, configuredPreauthTimeoutMs);
	if (typeof timeoutMs === "number" && Number.isFinite(timeoutMs)) return clampConnectChallengeTimeoutMs(timeoutMs, maxTimeoutMs);
	const envOverride = getConnectChallengeTimeoutMsFromEnv(params?.env);
	if (envOverride !== void 0) return clampConnectChallengeTimeoutMs(envOverride, Math.max(maxTimeoutMs, envOverride));
	return clampConnectChallengeTimeoutMs(configuredPreauthTimeoutMs, maxTimeoutMs);
}
function resolvePreauthHandshakeTimeoutMs(params) {
	const env = params?.env ?? process.env;
	const configuredTimeout = env.NODMIX_HANDSHAKE_TIMEOUT_MS || env.VITEST && env.NODMIX_TEST_HANDSHAKE_TIMEOUT_MS;
	if (configuredTimeout) {
		const parsed = parseStrictPositiveInteger(configuredTimeout);
		if (parsed !== void 0) return resolveSafeTimeoutDelayMs(parsed);
	}
	const configured = normalizePositiveTimeoutMs(params?.configuredTimeoutMs);
	if (configured !== void 0) return configured;
	return DEFAULT_PREAUTH_HANDSHAKE_TIMEOUT_MS;
}
//#endregion
export { resolvePreauthHandshakeTimeoutMs as a, resolveFiniteTimeoutDelayMs as i, addSafeTimeoutDelayGraceMs as n, resolveSafeTimeoutDelayMs as o, resolveConnectChallengeTimeoutMs as r, MAX_SAFE_TIMEOUT_DELAY_MS as t };
