/**
 * TUI selector for choosing which open workbooks the agent session can access.
 *
 * Options: All (default), None, or a multi-select subset of open workbook names.
 * Space toggles individual workbooks; Enter confirms the current selection.
 */
import { SCOPE_ALL, SCOPE_NONE, SCOPE_SELECTED } from '../../../app/workbook-scope.js';
import { Container, getEditorKeybindings, matchesKey, Spacer, Text } from '../../../tui/index.js';
import { theme } from '../../theme/runtime.js';
import { DynamicBorder } from '../primitives/dynamic-border.js';
import { cancelKeyHint, keyHint, rawKeyHint } from '../primitives/keybinding-hints.js';
/** Bordered multi-select list for picking workbook scope (All / None / specific workbooks). */
export class WorkbookScopeSelectorComponent extends Container {
    workbookNames;
    onSelectCallback;
    onCancelCallback;
    selectedIndex = 0;
    draftSelected = new Set();
    noneSelected = false;
    listContainer;
    hintText;
    constructor(workbookNames, initialSelection, onSelectCallback, onCancelCallback) {
        super();
        this.workbookNames = workbookNames;
        this.onSelectCallback = onSelectCallback;
        this.onCancelCallback = onCancelCallback;
        if (initialSelection.mode === SCOPE_NONE) {
            this.noneSelected = true;
        }
        else if (initialSelection.mode === SCOPE_SELECTED) {
            for (const name of initialSelection.workbooks) {
                this.draftSelected.add(name);
            }
        }
        this.addChild(new DynamicBorder());
        this.addChild(new Spacer(1));
        this.addChild(new Text(theme.fg('accent', 'Choose workbook scope for this session'), 1, 0));
        this.addChild(new Spacer(1));
        this.listContainer = new Container();
        this.addChild(this.listContainer);
        this.addChild(new Spacer(1));
        this.hintText = new Text('', 1, 0);
        this.addChild(this.hintText);
        this.addChild(new Spacer(1));
        this.addChild(new DynamicBorder());
        this.updateList();
    }
    /** Total options: All + None + individual workbooks */
    getOptionCount() {
        return this.workbookNames.length + 2;
    }
    getCurrentWorkbook() {
        if (this.selectedIndex <= 1) {
            return undefined; // "All" or "None" — not an individual workbook
        }
        return this.workbookNames[this.selectedIndex - 2];
    }
    updateList() {
        this.listContainer.clear();
        const confirmLabel = this.draftSelected.size > 0 ? 'confirm selection' : 'select current';
        this.hintText.setText(rawKeyHint('↑↓', 'navigate') +
            '  ' +
            keyHint('selectConfirm', confirmLabel) +
            '  ' +
            rawKeyHint('space', 'toggle') +
            '  ' +
            cancelKeyHint());
        const allSelected = this.draftSelected.size === 0 && !this.noneSelected;
        const allPrefix = this.selectedIndex === 0 ? theme.fg('accent', '→ ') : '  ';
        const allCheck = allSelected ? '[x]' : '[ ]';
        const allText = allPrefix +
            (this.selectedIndex === 0
                ? theme.fg('accent', `${allCheck} All open workbooks`)
                : theme.fg('text', `${allCheck} All open workbooks`));
        this.listContainer.addChild(new Text(allText, 1, 0));
        // "None" option — no workbook context
        const nonePrefix = this.selectedIndex === 1 ? theme.fg('accent', '→ ') : '  ';
        const noneCheck = this.noneSelected ? '[x]' : '[ ]';
        const noneLabel = `${noneCheck} None`;
        const noneText = nonePrefix +
            (this.selectedIndex === 1 ? theme.fg('accent', noneLabel) : theme.fg('text', noneLabel));
        this.listContainer.addChild(new Text(noneText, 1, 0));
        for (let i = 0; i < this.workbookNames.length; i++) {
            const name = this.workbookNames[i];
            const isSelectedRow = i + 2 === this.selectedIndex;
            const checked = this.draftSelected.has(name) ? '[x]' : '[ ]';
            const prefix = isSelectedRow ? theme.fg('accent', '→ ') : '  ';
            const body = isSelectedRow ? theme.fg('accent', `${checked} ${name}`) : `${checked} ${name}`;
            this.listContainer.addChild(new Text(prefix + body, 1, 0));
        }
    }
    handleInput(keyData) {
        const kb = getEditorKeybindings();
        if (kb.matches(keyData, 'selectUp') || keyData === 'k') {
            this.selectedIndex =
                this.selectedIndex === 0 ? this.getOptionCount() - 1 : this.selectedIndex - 1;
            this.updateList();
            return;
        }
        if (kb.matches(keyData, 'selectDown') || keyData === 'j') {
            this.selectedIndex =
                this.selectedIndex === this.getOptionCount() - 1 ? 0 : this.selectedIndex + 1;
            this.updateList();
            return;
        }
        if (matchesKey(keyData, 'space')) {
            if (this.selectedIndex === 0) {
                // "All" — clear individual selections and none
                this.draftSelected.clear();
                this.noneSelected = false;
            }
            else if (this.selectedIndex === 1) {
                // "None" — toggle, clear individual selections
                this.noneSelected = !this.noneSelected;
                if (this.noneSelected)
                    this.draftSelected.clear();
            }
            else {
                const workbook = this.getCurrentWorkbook();
                if (workbook) {
                    this.noneSelected = false;
                    if (this.draftSelected.has(workbook)) {
                        this.draftSelected.delete(workbook);
                    }
                    else {
                        this.draftSelected.add(workbook);
                    }
                }
            }
            this.updateList();
            return;
        }
        if (kb.matches(keyData, 'selectConfirm') || keyData === '\n') {
            if (this.selectedIndex === 0) {
                this.onSelectCallback({ mode: SCOPE_ALL });
                return;
            }
            if (this.selectedIndex === 1) {
                this.onSelectCallback({ mode: SCOPE_NONE });
                return;
            }
            if (this.noneSelected) {
                this.onSelectCallback({ mode: SCOPE_NONE });
                return;
            }
            if (this.draftSelected.size > 0) {
                this.onSelectCallback({
                    mode: SCOPE_SELECTED,
                    workbooks: this.workbookNames.filter((name) => this.draftSelected.has(name))
                });
                return;
            }
            const workbook = this.getCurrentWorkbook();
            if (workbook) {
                this.onSelectCallback({ mode: SCOPE_SELECTED, workbooks: [workbook] });
            }
            return;
        }
        if (kb.matches(keyData, 'selectCancel') || matchesKey(keyData, 'ctrl+c')) {
            this.onCancelCallback();
        }
    }
}
//# sourceMappingURL=workbook-scope-selector.js.map