// ── EventBus ───────────────────────────────────────────────
export class EventBus {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    listeners = new Map();
    on(event, handler) {
        if (!this.listeners.has(event)) {
            this.listeners.set(event, new Set());
        }
        this.listeners.get(event).add(handler);
        return () => this.off(event, handler);
    }
    off(event, handler) {
        this.listeners.get(event)?.delete(handler);
    }
    emit(event, payload) {
        const handlers = this.listeners.get(event);
        if (!handlers)
            return;
        for (const handler of handlers) {
            handler(payload);
        }
    }
    once(event, handler) {
        const wrapper = (payload) => {
            this.off(event, wrapper);
            handler(payload);
        };
        return this.on(event, wrapper);
    }
    /** Remove all listeners, freeing closures that may retain large scopes. */
    removeAllListeners() {
        for (const set of this.listeners.values()) {
            set.clear();
        }
        this.listeners.clear();
    }
    forwardAgentEvent(event) {
        switch (event.type) {
            case "text_delta":
                this.emit("text_delta", { text: event.text });
                break;
            case "thinking_delta":
                this.emit("thinking_delta", { text: event.text });
                break;
            case "tool_call_start":
                this.emit("tool_call_start", {
                    toolCallId: event.toolCallId,
                    name: event.name,
                    args: event.args,
                });
                break;
            case "tool_call_update":
                this.emit("tool_call_update", {
                    toolCallId: event.toolCallId,
                    update: event.update,
                });
                break;
            case "tool_call_end":
                this.emit("tool_call_end", {
                    toolCallId: event.toolCallId,
                    result: event.result,
                    isError: event.isError,
                    durationMs: event.durationMs,
                    details: event.details,
                });
                break;
            case "turn_end":
                this.emit("turn_end", {
                    turn: event.turn,
                    stopReason: event.stopReason,
                    usage: event.usage,
                });
                break;
            case "agent_done":
                this.emit("agent_done", {
                    totalTurns: event.totalTurns,
                    totalUsage: event.totalUsage,
                });
                break;
            case "max_turns":
                this.emit("max_turns", {
                    totalTurns: event.totalTurns,
                    maxTurns: event.maxTurns,
                });
                break;
            case "server_tool_call":
                this.emit("server_tool_call", {
                    id: event.id,
                    name: event.name,
                    input: event.input,
                });
                break;
            case "server_tool_result":
                this.emit("server_tool_result", {
                    toolUseId: event.toolUseId,
                    resultType: event.resultType,
                    data: event.data,
                });
                break;
            case "error":
                this.emit("error", { error: event.error });
                break;
        }
    }
}
//# sourceMappingURL=event-bus.js.map