import { formatAgentRunErrorForDisplay } from "../agent/client-error-format.js";
//#region src/tui/tui-agent-events.ts
const pendingToolCallIds = /* @__PURE__ */ new Map();
function clearPendingToolCallIds() {
	pendingToolCallIds.clear();
}
const STREAM_TOUCH_EVENTS = new Set([
	"status",
	"token",
	"thinking",
	"tool_start",
	"tool_end",
	"progress"
]);
function dispatchAgentSSE(event, data, state, chatLog, assembler, tui, setActivityStatus, touchStreamingActivity, onRunEnded) {
	if (STREAM_TOUCH_EVENTS.has(event)) touchStreamingActivity?.();
	const runId = state.activeRunId ?? "default";
	switch (event) {
		case "status":
			state.activeRunId = typeof data.runId === "string" ? data.runId : runId;
			setActivityStatus("waiting");
			break;
		case "token": {
			const content = typeof data.content === "string" ? data.content : typeof data.delta === "string" ? data.delta : typeof data.text === "string" ? data.text : "";
			if (!content) break;
			setActivityStatus("streaming");
			const display = assembler.ingestToken(runId, content, state.showThinking);
			if (display !== null) {
				chatLog.updateAssistant(display, runId);
				tui.requestRender();
			}
			break;
		}
		case "thinking": {
			const thinkContent = String(data.content ?? "");
			const isDelta = Boolean(data.delta);
			if (data.status === "started") break;
			setActivityStatus("streaming");
			const display = assembler.ingestThinking(runId, thinkContent, isDelta, state.showThinking);
			if (display !== null) {
				chatLog.updateAssistant(display, runId);
				tui.requestRender();
			}
			break;
		}
		case "thinking_end":
		case "message_end": break;
		case "tool_start": {
			const toolName = String(data.toolName ?? "unknown");
			const toolCallId = String(data.toolCallId || crypto.randomUUID());
			const stack = pendingToolCallIds.get(toolName) ?? [];
			stack.push(toolCallId);
			pendingToolCallIds.set(toolName, stack);
			setActivityStatus("running");
			chatLog.startTool(toolCallId, toolName, data.args, runId);
			tui.requestRender();
			break;
		}
		case "tool_end": {
			const toolName = String(data.toolName ?? "");
			let toolCallId = typeof data.toolCallId === "string" && data.toolCallId ? data.toolCallId : "";
			if (!toolCallId && toolName) {
				const stack = pendingToolCallIds.get(toolName);
				if (stack && stack.length > 0) {
					toolCallId = stack.shift();
					if (stack.length === 0) pendingToolCallIds.delete(toolName);
				}
			}
			const resultText = String(data.result ?? "");
			const isError = Boolean(data.isError);
			if (toolCallId) chatLog.updateToolResult(toolCallId, resultText, isError);
			setActivityStatus("streaming");
			tui.requestRender();
			break;
		}
		case "error": {
			const errorContent = formatAgentRunErrorForDisplay(String(data.content ?? "Unknown error"));
			const finalText = assembler.finalize(runId, state.showThinking);
			if (finalText) chatLog.finalizeAssistant(finalText, runId);
			chatLog.addSystem(`❌ ${errorContent}`);
			state.activeRunId = null;
			setActivityStatus("idle");
			onRunEnded?.();
			tui.requestRender();
			break;
		}
		case "result": {
			const finalText = assembler.finalize(runId, state.showThinking);
			if (finalText) chatLog.finalizeAssistant(finalText, runId);
			state.activeRunId = null;
			setActivityStatus("idle");
			onRunEnded?.();
			tui.requestRender();
			break;
		}
		case "progress": {
			const stage = typeof data.stage === "string" ? data.stage : "";
			const message = typeof data.message === "string" ? data.message : stage;
			state.progressMessage = message || null;
			setActivityStatus(message ? `progress: ${message}` : "running");
			tui.requestRender();
			break;
		}
		default: break;
	}
}
const DEFAULT_STREAMING_WATCHDOG_MS = 3e4;
//#endregion
export { DEFAULT_STREAMING_WATCHDOG_MS, clearPendingToolCallIds, dispatchAgentSSE };

//# sourceMappingURL=tui-agent-events.js.map