import { theme } from "../theme.js";
import { formatContextUsageLabel } from "../tui-context-usage.js";
import { getGitBranchCached } from "../tui-git-branch.js";
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
//#region src/tui/components/tui-bottom-bar.ts
const BUSY_ACTIVITY = new Set([
	"sending",
	"waiting",
	"streaming",
	"running"
]);
/** Compact token counts (aligned with pi coding-agent footer). */
function formatTokens(count) {
	if (count < 1e3) return count.toString();
	if (count < 1e4) return `${(count / 1e3).toFixed(1)}k`;
	if (count < 1e6) return `${Math.round(count / 1e3)}k`;
	if (count < 1e7) return `${(count / 1e6).toFixed(1)}M`;
	return `${Math.round(count / 1e6)}M`;
}
function shortenPath(cwd) {
	const home = process.env.HOME || process.env.USERPROFILE;
	if (home && cwd.startsWith(home)) return `~${cwd.slice(home.length)}`;
	return cwd;
}
/**
* Multi-line footer below the input editor (pi coding-agent style):
* cwd · session, then a single padded row: status/tokens on the left, model on the right.
*/
var TuiBottomBar = class {
	extensionLines = [];
	extensionStatusParts = [];
	constructor(getState, getThinkingDefault) {
		this.getState = getState;
		this.getThinkingDefault = getThinkingDefault;
	}
	setExtensionLines(lines) {
		this.extensionLines = lines;
	}
	setExtensionStatusParts(parts) {
		this.extensionStatusParts = parts;
	}
	invalidate() {}
	render(width) {
		const state = this.getState();
		const cwdRaw = process.cwd();
		const cwd = shortenPath(cwdRaw);
		const branch = getGitBranchCached(cwdRaw);
		const cwdWithBranch = branch ? `${cwd} (${branch})` : cwd;
		const sessionLine = state.sessionInfo.displayName ?? `session ${state.currentSessionKey}`;
		const pwdLine = truncateToWidth(theme.dim(`${cwdWithBranch} · ${sessionLine}`), width, theme.dim("…"));
		const busy = BUSY_ACTIVITY.has(state.activityStatus);
		const leftParts = [state.connectionStatus];
		if (!busy && state.activityStatus && state.activityStatus !== "idle") leftParts.push(state.activityStatus);
		if (state.sessionInfo.totalTokens != null) leftParts.push(formatTokens(state.sessionInfo.totalTokens));
		const ctxLabel = formatContextUsageLabel(state.sessionInfo.contextUsagePercent ?? null);
		if (ctxLabel) leftParts.push(ctxLabel);
		if (state.isCompacting) leftParts.push("compact…");
		else if (state.compactionQueue.length > 0) leftParts.push(`C${state.compactionQueue.length}`);
		if (state.messageFollowUpQueue.length > 0) leftParts.push(`Q${state.messageFollowUpQueue.length}`);
		for (const part of this.extensionStatusParts) leftParts.push(part);
		let statsLeft = leftParts.join(" · ");
		const modelId = state.sessionInfo.model ?? "unknown";
		const prov = state.sessionInfo.modelProvider;
		const thinkingHint = state.showThinking ? `thinking:${this.getThinkingDefault() ?? "on"}` : "thinking:off";
		let rightSide = prov ? `(${prov}) ${modelId}` : modelId;
		rightSide = `${rightSide} • ${thinkingHint}`;
		const minPadding = 2;
		let statsLeftWidth = visibleWidth(statsLeft);
		if (statsLeftWidth > width) {
			statsLeft = truncateToWidth(statsLeft, width, "…");
			statsLeftWidth = visibleWidth(statsLeft);
		}
		const rightWidth = visibleWidth(rightSide);
		const totalNeeded = statsLeftWidth + minPadding + rightWidth;
		let statsLine;
		if (totalNeeded <= width) {
			const padding = " ".repeat(width - statsLeftWidth - rightWidth);
			statsLine = statsLeft + padding + rightSide;
		} else {
			const availableForRight = width - statsLeftWidth - minPadding;
			if (availableForRight > 0) {
				const truncatedRight = truncateToWidth(rightSide, availableForRight, "");
				const tw = visibleWidth(truncatedRight);
				const padding = " ".repeat(Math.max(0, width - statsLeftWidth - tw));
				statsLine = statsLeft + padding + truncatedRight;
			} else statsLine = statsLeft;
		}
		const lines = [pwdLine, theme.dim(statsLine)];
		for (const extLine of this.extensionLines) lines.push(truncateToWidth(theme.dim(extLine), width, theme.dim("…")));
		return lines;
	}
};
//#endregion
export { TuiBottomBar, formatTokens };

//# sourceMappingURL=tui-bottom-bar.js.map