//#region extensions/crypto/src/services/event-bus.ts
var EventBus = class {
	handlers = /* @__PURE__ */ new Map();
	allHandlers = /* @__PURE__ */ new Set();
	/** Subscribe to a specific event type. Returns unsubscribe function. */
	on(type, handler) {
		if (!this.handlers.has(type)) this.handlers.set(type, /* @__PURE__ */ new Set());
		this.handlers.get(type).add(handler);
		return () => {
			this.handlers.get(type)?.delete(handler);
		};
	}
	/** Subscribe to all events. Returns unsubscribe function. */
	onAny(handler) {
		this.allHandlers.add(handler);
		return () => {
			this.allHandlers.delete(handler);
		};
	}
	/** Emit an event to all subscribers (async, errors swallowed). */
	emit(type, event) {
		const typeHandlers = this.handlers.get(type);
		if (typeHandlers) for (const handler of typeHandlers) try {
			const result = handler(event);
			if (result && typeof result.catch === "function") result.catch(() => {});
		} catch {}
		for (const handler of this.allHandlers) try {
			const result = handler(event);
			if (result && typeof result.catch === "function") result.catch(() => {});
		} catch {}
	}
	/** Remove all handlers. */
	clear() {
		this.handlers.clear();
		this.allHandlers.clear();
	}
	/** Get handler count for a specific event type (for testing). */
	listenerCount(type) {
		if (type) return (this.handlers.get(type)?.size ?? 0) + this.allHandlers.size;
		let total = this.allHandlers.size;
		for (const set of this.handlers.values()) total += set.size;
		return total;
	}
};
let instance = null;
function getEventBus() {
	if (!instance) instance = new EventBus();
	return instance;
}
//#endregion
export { getEventBus };

//# sourceMappingURL=event-bus-CMN2leCa.mjs.map