/**
 * Controller for workbook scope selection in interactive mode.
 *
 * Before each user turn, ensureScopeChosen() checks which workbooks are open
 * and prompts the user to select a scope when multiple workbooks are available.
 * Automatically re-prompts when the set of open workbooks changes.
 */
import { resolveWorkbookScope, SCOPE_ALL, SCOPE_NONE, SCOPE_SELECTED, SCOPE_UNSET } from '../../app/workbook-scope.js';
/**
 * Manages the workbook scope lifecycle: fetch open workbooks, detect changes,
 * prompt the user via a selector overlay, and persist the choice in stateRef.
 */
export class WorkbookScopeController {
    stateRef;
    showSelector;
    deps;
    constructor(stateRef, showSelector, deps) {
        this.stateRef = stateRef;
        this.showSelector = showSelector;
        this.deps = deps;
    }
    /** Ensure a workbook scope has been chosen, prompting the user if needed. */
    async ensureScopeChosen() {
        const ref = this.stateRef;
        if (!ref) {
            return;
        }
        const state = await this.deps.fetchState();
        ref.prefetchedState = state;
        if (!state.connected) {
            ref.current = { mode: SCOPE_UNSET };
            ref.knownWorkbooks = undefined;
            return;
        }
        const openWorkbooks = state.workbooks;
        // If the set of open workbooks changed since last time, re-prompt.
        if (ref.current.mode !== SCOPE_UNSET && ref.knownWorkbooks != null) {
            const known = new Set(ref.knownWorkbooks);
            const changed = openWorkbooks.length !== known.size || openWorkbooks.some((name) => !known.has(name));
            if (changed) {
                ref.current = { mode: SCOPE_UNSET };
            }
        }
        if (openWorkbooks.length === 0) {
            ref.knownWorkbooks = [];
            return;
        }
        if (ref.current.mode !== SCOPE_UNSET) {
            ref.knownWorkbooks = openWorkbooks;
            return;
        }
        if (openWorkbooks.length === 1) {
            ref.current = { mode: SCOPE_SELECTED, workbooks: [openWorkbooks[0]] };
            ref.knownWorkbooks = openWorkbooks;
            return;
        }
        const resolved = resolveWorkbookScope(ref.current, openWorkbooks);
        const initialSelection = resolved.mode === SCOPE_NONE
            ? { mode: SCOPE_NONE }
            : resolved.mode === SCOPE_ALL
                ? { mode: SCOPE_ALL }
                : { mode: SCOPE_SELECTED, workbooks: resolved.workbooks };
        const selection = await this.showSelector(openWorkbooks, initialSelection);
        ref.current = selection ?? { mode: SCOPE_ALL };
        ref.knownWorkbooks = openWorkbooks;
    }
    /** Clear the current scope, forcing a re-prompt on next turn. */
    reset() {
        if (!this.stateRef) {
            return;
        }
        this.stateRef.current = { mode: SCOPE_UNSET };
        this.stateRef.prefetchedState = undefined;
        this.stateRef.knownWorkbooks = undefined;
    }
}
//# sourceMappingURL=workbook-scope-controller.js.map