import { getKeybindings, Input, truncateToWidth } from "@earendil-works/pi-tui";
import { theme } from "../theme/theme.js";
import { keyHint } from "./keybinding-hints.js";
/** Label input component shown when editing a label */
export class LabelInput {
    get focused() {
        return this._focused;
    }
    set focused(value) {
        this._focused = value;
        this.input.focused = value;
    }
    constructor(entryId, currentLabel) {
        // Focusable implementation - propagate to input for IME cursor positioning
        this._focused = false;
        this.entryId = entryId;
        this.input = new Input();
        if (currentLabel) {
            this.input.setValue(currentLabel);
        }
    }
    invalidate() { }
    render(width) {
        const lines = [];
        const indent = "  ";
        const availableWidth = width - indent.length;
        lines.push(truncateToWidth(`${indent}${theme.fg("muted", "Label (empty to remove):")}`, width));
        lines.push(...this.input.render(availableWidth).map((line) => truncateToWidth(`${indent}${line}`, width)));
        lines.push(truncateToWidth(`${indent}${keyHint("tui.select.confirm", "save")}  ${keyHint("tui.select.cancel", "cancel")}`, width));
        return lines;
    }
    handleInput(keyData) {
        const kb = getKeybindings();
        if (kb.matches(keyData, "tui.select.confirm")) {
            const value = this.input.getValue().trim();
            this.onSubmit?.(this.entryId, value || undefined);
        }
        else if (kb.matches(keyData, "tui.select.cancel")) {
            this.onCancel?.();
        }
        else {
            this.input.handleInput(keyData);
        }
    }
}
//# sourceMappingURL=tree-selector-label-input.js.map