import { theme } from "../theme.js";
import { isDiffFriendlyTool, looksLikeUnifiedDiff, renderUnifiedDiff } from "../tui-tool-diff.js";
import { renderToolWithExtensions } from "../extension-host/tool-renderers.js";
import { Container, Spacer, Text } from "@earendil-works/pi-tui";
//#region src/tui/components/tool-execution.ts
const MAX_ARG_VALUE_LENGTH = 120;
function sanitize(text) {
	return text.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, "");
}
function formatArgsSummary(args) {
	if (!args || typeof args !== "object") return "";
	const entries = Object.entries(args);
	if (entries.length === 0) return "";
	return entries.map(([key, value]) => {
		const stringValue = typeof value === "string" ? value : JSON.stringify(value);
		return `${key}=${stringValue.length > MAX_ARG_VALUE_LENGTH ? `${stringValue.slice(0, MAX_ARG_VALUE_LENGTH - 3)}...` : stringValue}`;
	}).join(", ");
}
var ToolExecutionComponent = class extends Container {
	contentText;
	toolName;
	args;
	resultText = "";
	expanded = false;
	isError = false;
	isPartial = true;
	constructor(toolName, args) {
		super();
		this.toolName = toolName;
		this.args = args;
		this.addChild(new Spacer(1));
		const bgFn = (text) => theme.toolPendingBg(text);
		this.contentText = new Text("", 1, 1, bgFn);
		this.addChild(this.contentText);
		this.refresh();
	}
	setArgs(args) {
		this.args = args;
		this.refresh();
	}
	setExpanded(expanded) {
		this.expanded = expanded;
		this.refresh();
	}
	setResult(result, isError) {
		this.resultText = sanitize(result);
		this.isError = isError;
		this.isPartial = false;
		this.refresh();
	}
	refresh() {
		const bgFn = this.isPartial ? (text) => theme.toolPendingBg(text) : this.isError ? (text) => theme.toolErrorBg(text) : (text) => theme.toolSuccessBg(text);
		this.contentText.setCustomBgFn(bgFn);
		this.contentText.setText(this.formatToolExecution());
	}
	formatToolExecution() {
		const custom = renderToolWithExtensions({
			toolName: this.toolName,
			args: this.args,
			resultText: this.resultText,
			isError: this.isError,
			expanded: this.expanded
		});
		if (custom && custom.length > 0) return custom.join("\n");
		const argsStr = formatArgsSummary(this.args);
		const titleParts = [theme.toolTitle(theme.bold(this.toolName))];
		if (argsStr) titleParts.push(theme.dim(`(${argsStr})`));
		let text = titleParts.join(" ");
		const output = this.resultText;
		if (output) if (this.expanded && isDiffFriendlyTool(this.toolName) && looksLikeUnifiedDiff(output)) text += `\n${renderUnifiedDiff(output)}`;
		else if (this.expanded) text += `\n${theme.toolOutput(output)}`;
		else {
			const oneLine = output.replace(/\n/g, " ").replace(/\s+/g, " ").trim();
			const lineCount = output.split("\n").length;
			const maxPreviewLength = 200;
			const preview = oneLine.length > maxPreviewLength ? `${oneLine.slice(0, maxPreviewLength)}…` : oneLine;
			const suffix = lineCount > 1 ? theme.dim(` (${lineCount} lines)`) : "";
			text += `\n${theme.toolOutput(preview)}${suffix}`;
		}
		else if (this.isPartial) text += `\n${theme.dim("…")}`;
		return text;
	}
};
//#endregion
export { ToolExecutionComponent };

//# sourceMappingURL=tool-execution.js.map