/**
 * Generic selector component for extensions.
 * Displays a list of options with optional descriptions and keyboard navigation.
 */
import { Container, getEditorKeybindings, Spacer, Text } from '../../../tui/index.js';
import { theme } from '../../theme/runtime.js';
import { CountdownTimer } from '../primitives/countdown-timer.js';
import { DynamicBorder } from '../primitives/dynamic-border.js';
import { keyHint, rawKeyHint } from '../primitives/keybinding-hints.js';
export class ExtensionSelectorComponent extends Container {
    options;
    selectedIndex = 0;
    listContainer;
    descriptionContainer;
    onSelectCallback;
    onCancelCallback;
    titleText;
    baseTitle;
    countdown;
    constructor(title, options, onSelect, onCancel, opts) {
        super();
        this.options = options.map((option) => typeof option === 'string'
            ? {
                value: option,
                label: option
            }
            : option);
        this.onSelectCallback = onSelect;
        this.onCancelCallback = onCancel;
        this.baseTitle = title;
        this.addChild(new DynamicBorder());
        this.addChild(new Spacer(1));
        this.titleText = new Text(theme.fg('accent', title), 1, 0);
        this.addChild(this.titleText);
        this.addChild(new Spacer(1));
        if (opts?.timeout && opts.timeout > 0 && opts.tui) {
            this.countdown = new CountdownTimer(opts.timeout, opts.tui, (s) => this.titleText.setText(theme.fg('accent', `${this.baseTitle} (${s}s)`)), () => this.onCancelCallback());
        }
        this.listContainer = new Container();
        this.addChild(this.listContainer);
        this.descriptionContainer = new Container();
        this.addChild(this.descriptionContainer);
        this.addChild(new Spacer(1));
        this.addChild(new Text(rawKeyHint('↑↓', 'navigate') +
            '  ' +
            keyHint('selectConfirm', 'select') +
            '  ' +
            keyHint('selectCancel', 'cancel'), 1, 0));
        this.addChild(new Spacer(1));
        this.addChild(new DynamicBorder());
        this.updateList();
    }
    updateList() {
        this.listContainer.clear();
        for (let i = 0; i < this.options.length; i++) {
            const isSelected = i === this.selectedIndex;
            const option = this.options[i];
            const text = isSelected
                ? theme.fg('accent', '→ ') + theme.fg('accent', option.label)
                : `  ${theme.fg('text', option.label)}`;
            this.listContainer.addChild(new Text(text, 1, 0));
        }
        this.descriptionContainer.clear();
        const selected = this.options[this.selectedIndex];
        if (selected?.description) {
            this.descriptionContainer.addChild(new Spacer(1));
            this.descriptionContainer.addChild(new Text(`  ${theme.fg('dim', selected.description)}`, 1, 0));
        }
    }
    handleInput(keyData) {
        const kb = getEditorKeybindings();
        if (kb.matches(keyData, 'selectUp') || keyData === 'k') {
            this.selectedIndex = Math.max(0, this.selectedIndex - 1);
            this.updateList();
        }
        else if (kb.matches(keyData, 'selectDown') || keyData === 'j') {
            this.selectedIndex = Math.min(this.options.length - 1, this.selectedIndex + 1);
            this.updateList();
        }
        else if (kb.matches(keyData, 'selectConfirm') || keyData === '\n') {
            const selected = this.options[this.selectedIndex];
            if (selected)
                this.onSelectCallback(selected.value);
        }
        else if (kb.matches(keyData, 'selectCancel')) {
            this.onCancelCallback();
        }
    }
    dispose() {
        this.countdown?.dispose();
    }
}
//# sourceMappingURL=extension-selector.js.map