import { Container, Spacer, Text } from "@earendil-works/pi-tui";
import { theme } from "../theme/theme.js";
import { DynamicBorder } from "./dynamic-border.js";
import { SearchLine, TreeHelp } from "./tree-selector-help.js";
import { LabelInput } from "./tree-selector-label-input.js";
import { TreeList } from "./tree-selector-list.js";
/** Component that renders a session tree selector for navigation */
export class TreeSelectorComponent extends Container {
    get focused() {
        return this._focused;
    }
    set focused(value) {
        this._focused = value;
        // Propagate to labelInput when it's active
        if (this.labelInput) {
            this.labelInput.focused = value;
        }
    }
    constructor(tree, currentLeafId, terminalHeight, onSelect, onCancel, onLabelChange, initialSelectedId, initialFilterMode) {
        super();
        this.labelInput = null;
        // Focusable implementation - propagate to labelInput when active for IME cursor positioning
        this._focused = false;
        this.onLabelChangeCallback = onLabelChange;
        const maxVisibleLines = Math.max(5, Math.floor(terminalHeight / 2));
        this.treeList = new TreeList(tree, currentLeafId, maxVisibleLines, initialSelectedId, initialFilterMode);
        this.treeList.onSelect = onSelect;
        this.treeList.onCancel = onCancel;
        this.treeList.onLabelEdit = (entryId, currentLabel) => this.showLabelInput(entryId, currentLabel);
        this.treeContainer = new Container();
        this.treeContainer.addChild(this.treeList);
        this.labelInputContainer = new Container();
        this.addChild(new Spacer(1));
        this.addChild(new DynamicBorder());
        this.addChild(new Text(theme.bold("  Session Tree"), 1, 0));
        this.addChild(new TreeHelp());
        this.addChild(new SearchLine(this.treeList));
        this.addChild(new DynamicBorder());
        this.addChild(new Spacer(1));
        this.addChild(this.treeContainer);
        this.addChild(this.labelInputContainer);
        this.addChild(new Spacer(1));
        this.addChild(new DynamicBorder());
        if (tree.length === 0) {
            setTimeout(() => onCancel(), 100);
        }
    }
    showLabelInput(entryId, currentLabel) {
        this.labelInput = new LabelInput(entryId, currentLabel);
        this.labelInput.onSubmit = (id, label) => {
            this.treeList.updateNodeLabel(id, label);
            this.onLabelChangeCallback?.(id, label);
            this.hideLabelInput();
        };
        this.labelInput.onCancel = () => this.hideLabelInput();
        // Propagate current focused state to the new labelInput
        this.labelInput.focused = this._focused;
        this.treeContainer.clear();
        this.labelInputContainer.clear();
        this.labelInputContainer.addChild(this.labelInput);
    }
    hideLabelInput() {
        this.labelInput = null;
        this.labelInputContainer.clear();
        this.treeContainer.clear();
        this.treeContainer.addChild(this.treeList);
    }
    handleInput(keyData) {
        if (this.labelInput) {
            this.labelInput.handleInput(keyData);
        }
        else {
            this.treeList.handleInput(keyData);
        }
    }
    getTreeList() {
        return this.treeList;
    }
}
//# sourceMappingURL=tree-selector-component.js.map