import { theme } from "../theme.js";
import { DynamicBorder } from "./dynamic-border.js";
import { Container, Loader, Spacer, Text } from "@earendil-works/pi-tui";
//#region src/tui/components/bash-execution.ts
const PREVIEW_LINES = 20;
const MAX_OUTPUT_CHARS = 4e4;
function stripAnsi(text) {
	return text.replace(/\x1b\[[0-9;]*m/g, "");
}
/** Local `!` / `!!` shell output block (pi coding-agent–aligned). */
var BashExecutionComponent = class extends Container {
	contentContainer = new Container();
	loader;
	outputLines = [];
	status = "running";
	exitCode;
	expanded = false;
	constructor(command, ui, excludeFromContext) {
		super();
		this.excludeFromContext = excludeFromContext;
		const colorKey = excludeFromContext ? "bashExclude" : "bashMode";
		const borderColor = (str) => theme.fg(colorKey, str);
		this.addChild(new Spacer(1));
		this.addChild(new DynamicBorder(borderColor));
		this.addChild(this.contentContainer);
		const header = new Text(theme.fg(colorKey, theme.bold(`$ ${command}`)), 1, 0);
		this.contentContainer.addChild(header);
		this.loader = new Loader(ui, (spinner) => theme.fg(colorKey, spinner), (text) => theme.dim(text), "Running…");
		this.contentContainer.addChild(this.loader);
		this.addChild(new DynamicBorder(borderColor));
	}
	setExpanded(expanded) {
		this.expanded = expanded;
		this.refreshOutput();
	}
	appendOutput(chunk) {
		const newLines = stripAnsi(chunk).replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
		if (this.outputLines.length > 0 && newLines.length > 0) {
			this.outputLines[this.outputLines.length - 1] += newLines[0];
			this.outputLines.push(...newLines.slice(1));
		} else this.outputLines.push(...newLines);
		const joined = this.outputLines.join("\n");
		if (joined.length > MAX_OUTPUT_CHARS) this.outputLines = joined.slice(-MAX_OUTPUT_CHARS).split("\n");
		this.refreshOutput();
	}
	setComplete(exitCode, signal) {
		this.loader.stop();
		if (signal) this.status = "error";
		else if (exitCode != null && exitCode !== 0) this.status = "error";
		else this.status = "complete";
		this.exitCode = exitCode ?? void 0;
		this.refreshOutput();
	}
	setError(message) {
		this.loader.stop();
		this.status = "error";
		this.outputLines = [message];
		this.refreshOutput();
	}
	refreshOutput() {
		while (this.contentContainer.children.length > 2) this.contentContainer.removeChild(this.contentContainer.children[2]);
		const lines = this.outputLines.filter((l) => l.length > 0);
		const visible = this.expanded ? lines : lines.slice(-PREVIEW_LINES);
		const hidden = lines.length - visible.length;
		if (hidden > 0 && !this.expanded) this.contentContainer.addChild(new Text(theme.dim(`… ${hidden} more line${hidden > 1 ? "s" : ""} (Ctrl+O to expand tools/output)`), 1, 0));
		for (const line of visible) this.contentContainer.addChild(new Text(theme.toolOutput(line), 1, 0));
		if (this.status !== "running") {
			const suffix = this.status === "error" ? `exit ${this.exitCode ?? "?"}${this.excludeFromContext ? " · excluded from agent context" : ""}` : `exit ${this.exitCode ?? 0}${this.excludeFromContext ? " · excluded from agent context" : ""}`;
			this.contentContainer.addChild(new Text(theme.dim(suffix), 1, 0));
		}
	}
};
//#endregion
export { BashExecutionComponent };

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