import { theme } from "../theme.js";
import { BashExecutionComponent } from "./bash-execution.js";
import { AssistantMessageComponent } from "./assistant-message.js";
import { ToolExecutionComponent } from "./tool-execution.js";
import { UserMessageComponent } from "./user-message.js";
import { Container, Spacer, Text } from "@earendil-works/pi-tui";
//#region src/tui/components/chat-log.ts
const MAX_COMPONENTS = 180;
var ChatLog = class extends Container {
	toolById = /* @__PURE__ */ new Map();
	bashBlocks = [];
	streamingRuns = /* @__PURE__ */ new Map();
	/** After finalizeAssistant, late tool_start can still arrive; keep the bubble to insert tools above. */
	assistantAnchorByRunId = /* @__PURE__ */ new Map();
	toolsExpanded = false;
	pruneOverflow() {
		while (this.children.length > MAX_COMPONENTS) {
			const oldest = this.children[0];
			if (!oldest) return;
			this.removeChild(oldest);
			this.dropReferences(oldest);
		}
	}
	dropReferences(component) {
		for (const [id, tool] of this.toolById.entries()) if (tool === component) this.toolById.delete(id);
		for (const [runId, msg] of this.streamingRuns.entries()) if (msg === component) this.streamingRuns.delete(runId);
		for (const [runId, msg] of this.assistantAnchorByRunId.entries()) if (msg === component) this.assistantAnchorByRunId.delete(runId);
	}
	append(component) {
		this.addChild(component);
		this.pruneOverflow();
	}
	clearAll() {
		this.clear();
		this.toolById.clear();
		this.bashBlocks = [];
		this.streamingRuns.clear();
		this.assistantAnchorByRunId.clear();
	}
	addSystem(text) {
		const entry = new Container();
		entry.addChild(new Spacer(1));
		entry.addChild(new Text(theme.system(text), 1, 0));
		this.append(entry);
	}
	addUser(text) {
		this.assistantAnchorByRunId.clear();
		this.append(new UserMessageComponent(text));
	}
	/** Stream local `!command` output in a bordered block. */
	addBashExecution(command, ui, excludeFromContext) {
		this.assistantAnchorByRunId.clear();
		const component = new BashExecutionComponent(command, ui, excludeFromContext);
		component.setExpanded(this.toolsExpanded);
		this.bashBlocks.push(component);
		this.append(component);
		return component;
	}
	startAssistant(text, runId) {
		const existing = this.streamingRuns.get(runId);
		if (existing) {
			existing.setText(text);
			return;
		}
		const component = new AssistantMessageComponent(text);
		this.streamingRuns.set(runId, component);
		this.append(component);
	}
	updateAssistant(text, runId) {
		const existing = this.streamingRuns.get(runId);
		if (!existing) {
			this.startAssistant(text, runId);
			return;
		}
		existing.setText(text);
	}
	finalizeAssistant(text, runId) {
		const existing = this.streamingRuns.get(runId);
		if (existing) {
			existing.setText(text);
			this.streamingRuns.delete(runId);
			this.assistantAnchorByRunId.set(runId, existing);
			return;
		}
		const finalMessage = new AssistantMessageComponent(text);
		this.append(finalMessage);
		if (text.trim()) this.assistantAnchorByRunId.set(runId, finalMessage);
	}
	dropAssistant(runId) {
		const existing = this.streamingRuns.get(runId);
		if (!existing) return;
		this.removeChild(existing);
		this.streamingRuns.delete(runId);
	}
	startTool(toolCallId, toolName, args, runId) {
		const existing = this.toolById.get(toolCallId);
		if (existing) {
			existing.setArgs(args);
			return;
		}
		const component = new ToolExecutionComponent(toolName, args);
		component.setExpanded(this.toolsExpanded);
		this.toolById.set(toolCallId, component);
		const assistant = this.streamingRuns.get(runId) ?? this.assistantAnchorByRunId.get(runId);
		if (assistant) {
			this.removeChild(assistant);
			this.addChild(component);
			this.addChild(assistant);
		} else this.addChild(component);
		this.pruneOverflow();
	}
	updateToolResult(toolCallId, result, isError) {
		const existing = this.toolById.get(toolCallId);
		if (!existing) return;
		existing.setResult(result, isError);
	}
	setToolsExpanded(expanded) {
		this.toolsExpanded = expanded;
		for (const tool of this.toolById.values()) tool.setExpanded(expanded);
		for (const bash of this.bashBlocks) bash.setExpanded(expanded);
	}
};
//#endregion
export { ChatLog };

//# sourceMappingURL=chat-log.js.map