import { getLogger } from "../../../logger.js";

//#region src/domains/run/session/ToolApprovalUiBus.ts
const logger = getLogger("ToolApprovalUiBus");
/**
* In-process event bus keyed by streamRequestId.
*
* Used to propagate approval UI events from delegated agents (who must not write to stream)
* up to the user-facing request handler, which can stream tool UI parts to the client.
*/
var ToolApprovalUiBus = class {
	listeners = /* @__PURE__ */ new Map();
	subscribe(streamRequestId, listener) {
		if (!streamRequestId) return () => {};
		const set = this.listeners.get(streamRequestId) ?? /* @__PURE__ */ new Set();
		set.add(listener);
		this.listeners.set(streamRequestId, set);
		return () => {
			const existing = this.listeners.get(streamRequestId);
			if (!existing) return;
			existing.delete(listener);
			if (existing.size === 0) this.listeners.delete(streamRequestId);
		};
	}
	async publish(streamRequestId, event) {
		if (!streamRequestId) return;
		const set = this.listeners.get(streamRequestId);
		if (!set || set.size === 0) return;
		for (const listener of set) try {
			await listener(event);
		} catch (error) {
			logger.warn({
				streamRequestId,
				eventType: event.type,
				toolCallId: event.toolCallId,
				error: error instanceof Error ? error.message : String(error),
				reason: event.reason
			}, "ToolApprovalUiBus listener failed");
		}
	}
};
const toolApprovalUiBus = new ToolApprovalUiBus();

//#endregion
export { ToolApprovalUiBus, toolApprovalUiBus };