//#region extensions/crypto/src/services/interrupt-service.ts
/** Interrupt flags expire after 30 seconds. */
const TTL_MS = 3e4;
/** Sweep stale entries every 60 seconds. */
const SWEEP_INTERVAL_MS = 6e4;
var InterruptService = class {
	flags = /* @__PURE__ */ new Map();
	sweepTimer = null;
	constructor() {
		this.sweepTimer = setInterval(() => this.sweep(), SWEEP_INTERVAL_MS);
		if (this.sweepTimer && typeof this.sweepTimer.unref === "function") this.sweepTimer.unref();
	}
	/**
	* Set the interrupt flag for a session.
	* Returns true if a flag was newly set (wasn't already active).
	*/
	interrupt(sessionKey, reason) {
		const existing = this.flags.get(sessionKey);
		if (existing && Date.now() - existing.timestamp < TTL_MS) return false;
		this.flags.set(sessionKey, {
			timestamp: Date.now(),
			reason
		});
		return true;
	}
	/**
	* Check and consume the interrupt flag.
	* Returns true if an interrupt was pending (and consumes it).
	*/
	consume(sessionKey) {
		const flag = this.flags.get(sessionKey);
		if (!flag) return false;
		if (Date.now() - flag.timestamp > TTL_MS) {
			this.flags.delete(sessionKey);
			return false;
		}
		this.flags.delete(sessionKey);
		return true;
	}
	/**
	* Check if an interrupt is pending without consuming it.
	*/
	isPending(sessionKey) {
		const flag = this.flags.get(sessionKey);
		if (!flag) return false;
		if (Date.now() - flag.timestamp > TTL_MS) {
			this.flags.delete(sessionKey);
			return false;
		}
		return true;
	}
	/** Remove expired entries. */
	sweep() {
		const now = Date.now();
		for (const [key, flag] of this.flags) if (now - flag.timestamp > TTL_MS) this.flags.delete(key);
	}
	/** Stop the sweep timer (for shutdown). */
	stop() {
		if (this.sweepTimer) {
			clearInterval(this.sweepTimer);
			this.sweepTimer = null;
		}
	}
	/** Number of active flags (for testing). */
	get size() {
		return this.flags.size;
	}
};
let instance = null;
function getInterruptService() {
	if (!instance) instance = new InterruptService();
	return instance;
}
function resetInterruptService() {
	instance?.stop();
	instance = null;
}
//#endregion
export { getInterruptService, resetInterruptService };

//# sourceMappingURL=interrupt-service.mjs.map