import { matchesKey, truncateToWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
import { theme } from "../theme/theme.js";
const ROWS = ["chat", "workflow"];
const LABEL_WIDTH = 16;
const DESCRIPTION = "Priority tier for supported openai/* and openai-codex/* models.";
const ROW_DETAILS = {
    chat: {
        label: "Chat",
        scope: "this chat + subagents",
    },
    workflow: {
        label: "Workflow stages",
        scope: "workflow stages",
    },
};
export class FastModeSelectorComponent {
    constructor(config, callbacks) {
        this.selectedRowIndex = 0;
        this.state = { ...config };
        this.callbacks = callbacks;
    }
    invalidate() { }
    render(width) {
        const lines = [truncateToWidth(theme.bold(theme.fg("accent", "Codex fast mode")), width)];
        for (const line of wrapTextWithAnsi(DESCRIPTION, Math.max(20, width))) {
            lines.push(theme.fg("muted", line));
        }
        lines.push("");
        for (const row of ROWS) {
            lines.push(this.renderRow(row, width));
        }
        lines.push("");
        lines.push(truncateToWidth(this.renderHint(), width));
        return lines.map((line) => truncateToWidth(line, width));
    }
    handleInput(data) {
        if (matchesKey(data, "tab") || matchesKey(data, "down")) {
            this.moveRow(1);
            return;
        }
        if (matchesKey(data, "shift+tab") || matchesKey(data, "up")) {
            this.moveRow(-1);
            return;
        }
        if (matchesKey(data, "enter") || data === " ") {
            this.toggleCurrentRow();
            return;
        }
        if (matchesKey(data, "left")) {
            this.setCurrentRow(false);
            return;
        }
        if (matchesKey(data, "right")) {
            this.setCurrentRow(true);
            return;
        }
        if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
            void this.callbacks.onCancel();
        }
    }
    getFocusedRow() {
        return ROWS[this.selectedRowIndex];
    }
    getSettings() {
        return { ...this.state };
    }
    moveRow(delta) {
        this.selectedRowIndex = (this.selectedRowIndex + delta + ROWS.length) % ROWS.length;
    }
    setCurrentRow(enabled) {
        const row = this.getFocusedRow();
        if (this.state[row] === enabled) {
            return;
        }
        this.state = { ...this.state, [row]: enabled };
        this.callbacks.onChange({ ...this.state }, row);
    }
    toggleCurrentRow() {
        const row = this.getFocusedRow();
        this.setCurrentRow(!this.state[row]);
    }
    renderHint() {
        const sep = theme.fg("dim", " · ");
        const hint = (key, label) => theme.fg("dim", key) + theme.fg("muted", ` ${label}`);
        return [hint("↑↓/tab", "row"), hint("space/enter", "toggle"), hint("esc", "close")].join(sep);
    }
    renderRow(row, width) {
        const selected = this.getFocusedRow() === row;
        const detail = ROW_DETAILS[row];
        const prefix = selected ? theme.fg("accent", "› ") : "  ";
        const label = detail.label.padEnd(LABEL_WIDTH, " ");
        const labelText = selected ? theme.bold(theme.fg("accent", label)) : theme.fg("text", label);
        const scope = selected ? theme.fg("muted", detail.scope) : theme.fg("dim", detail.scope);
        return truncateToWidth(`${prefix}${labelText} ${this.renderToggle(row)}  ${scope}`, width);
    }
    renderToggle(row) {
        const enabled = this.state[row];
        const text = enabled ? "[● ON ]" : "[○ OFF]";
        if (enabled) {
            return theme.bold(theme.fg("success", text));
        }
        return this.getFocusedRow() === row ? theme.fg("muted", text) : theme.fg("dim", text);
    }
}
//# sourceMappingURL=fast-mode-selector.js.map