import { visibleWidth } from "@earendil-works/pi-tui";
import { truncateFromEndByWidth } from "./components/one_line_segments.js";
export class FooterComponent {
    ui;
    theme;
    idleIcon = "○";
    iconFrames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
    currentIconFrame = 0;
    iconIntervalId = null;
    toastTimeoutId = null;
    status = null;
    toast = null;
    constructor(theme, ui) {
        this.theme = theme;
        this.ui = ui;
    }
    setTheme(theme) {
        if (this.theme === theme)
            return;
        this.theme = theme;
        this.ui.requestRender();
    }
    startWorkingIcon() {
        if (this.iconIntervalId)
            return;
        this.currentIconFrame = 0;
        this.ui.requestRender();
        this.iconIntervalId = setInterval(() => {
            this.currentIconFrame = (this.currentIconFrame + 1) % this.iconFrames.length;
            this.ui.requestRender();
        }, 80);
    }
    stop() {
        if (this.iconIntervalId) {
            clearInterval(this.iconIntervalId);
            this.iconIntervalId = null;
        }
        this.currentIconFrame = 0;
        this.ui.requestRender();
    }
    setStatus(status) {
        this.status = status;
        this.ui.requestRender();
    }
    showToast(text, kind, durationMs = 3000) {
        if (this.toastTimeoutId) {
            clearTimeout(this.toastTimeoutId);
        }
        this.toast = { text, kind };
        this.ui.requestRender();
        this.toastTimeoutId = setTimeout(() => {
            this.toast = null;
            this.toastTimeoutId = null;
            this.ui.requestRender();
        }, durationMs);
    }
    invalidate() { }
    render(width) {
        if (width <= 0)
            return [""];
        const { palette } = this.theme;
        const iconChar = this.iconIntervalId ? this.iconFrames[this.currentIconFrame] : this.idleIcon;
        const icon = this.iconIntervalId ? palette.brandAccent(iconChar) : palette.textDim(iconChar);
        const iconWidth = visibleWidth(iconChar);
        const leftFull = this.status
            ? `${this.status.duration ? `${this.status.duration} · ` : ""}${this.status.contextUsage} · ${this.status.sessionCost}`
            : "";
        const toast = this.toast;
        const commandHint = this.status?.commandHint?.trim();
        const leftRaw = toast ? toast.text : commandHint || leftFull;
        const leftStyle = toast ? this.getToastStyle(toast.kind) : palette.textDim;
        const rightPrefixRaw = "";
        const { riskText, riskStyled } = this.status
            ? this.formatRiskLabel(this.status.riskLevel)
            : { riskText: "", riskStyled: "" };
        const rightWidth = visibleWidth(`${rightPrefixRaw}${riskText}`);
        const leftWidth = visibleWidth(leftRaw);
        let line;
        const totalContentWidth = 1 + iconWidth + 1 + leftWidth + rightWidth + 1 + 1;
        if (totalContentWidth > width) {
            const availableLeft = Math.max(0, width - 1 - iconWidth - 1 - rightWidth - 1 - 1);
            const truncatedLeft = truncateFromEndByWidth(leftRaw, availableLeft);
            const leftStyled = leftStyle(truncatedLeft);
            const rightStyled = `${palette.textDim(rightPrefixRaw)}${riskStyled}`;
            line = ` ${icon} ${leftStyled} ${rightStyled} `;
        }
        else {
            const spaces = " ".repeat(Math.max(1, width - 1 - iconWidth - 1 - leftWidth - rightWidth - 1));
            const leftStyled = leftStyle(leftRaw);
            const rightStyled = `${palette.textDim(rightPrefixRaw)}${riskStyled}`;
            line = ` ${icon} ${leftStyled + spaces}${rightStyled} `;
        }
        return [line];
    }
    getToastStyle(kind) {
        const { palette } = this.theme;
        if (kind === "error")
            return palette.toastError;
        if (kind === "warn")
            return palette.toastWarn;
        if (kind === "muted")
            return palette.textMuted;
        return palette.toastSuccess;
    }
    formatRiskLabel(riskLevel) {
        const { palette } = this.theme;
        switch (riskLevel) {
            case "read-only":
                return {
                    riskText: "read-only",
                    riskStyled: palette.riskReadOnlyText("read-only"),
                };
            case "read-write":
                return {
                    riskText: "read-write",
                    riskStyled: palette.riskReadWriteText("read-write"),
                };
        }
    }
}
//# sourceMappingURL=footer.js.map