import { selectActivePreviewPaneIndex } from "../state/selectors/derivations.js";
import { selectActiveView } from "../state/selectors/focus.js";
/**
 * View fan-out: drives every component setter from the canonical state via
 * two binding registries. `globalBindings` covers the cross-tab components
 * (chatRow, dialog, submitPicker?, tabBar?); `perTabBindings` covers the
 * per-tab kinds (optionList, preview, multiSelect?). The hand-coded fan-out
 * collapses to one global loop + one nested per-tab loop. The shared inline
 * input is projected through owner-specific draft slots so the option list and
 * chat footer never display each other's in-flight text.
 */
export class QuestionnairePropsAdapter {
    constructor(config) {
        this.tui = config.tui;
        this.questions = config.questions;
        this.itemsByTab = config.itemsByTab;
        this.tabsByIndex = config.tabsByIndex;
        this.inlineInput = config.inlineInput;
        this.globalBindings = config.globalBindings;
        this.perTabBindings = config.perTabBindings;
        this.extraInvalidatables = config.extraInvalidatables ?? [];
    }
    apply(state) {
        const totalQuestions = this.questions.length;
        const activeView = selectActiveView(state, totalQuestions);
        const paneIndex = selectActivePreviewPaneIndex(state.currentTab, totalQuestions);
        const activePreviewPane = this.tabsByIndex[paneIndex]?.preview ?? this.tabsByIndex[0].preview;
        const liveInlineValue = this.inlineInput.getValue();
        const inputBuffer = state.customDraftByTab.get(state.currentTab) ??
            (state.inlineInputOwner === "other" ? liveInlineValue : "");
        const inputCaret = state.customCaretByTab.get(state.currentTab) ?? inputBuffer.length;
        const chatInputBuffer = state.chatDraftByTab.get(state.currentTab) ??
            (state.inlineInputOwner === "chat" ? liveInlineValue : "");
        const chatInputCaret = state.chatCaretByTab.get(state.currentTab) ?? chatInputBuffer.length;
        const ctx = {
            questions: this.questions,
            itemsByTab: this.itemsByTab,
            totalQuestions,
            activeView,
            inputBuffer,
            inputCaret,
            chatInputBuffer,
            chatInputCaret,
            activePreviewPane,
        };
        for (const binding of this.globalBindings) {
            binding.apply(state, ctx);
        }
        for (let i = 0; i < this.tabsByIndex.length; i++) {
            const tab = this.tabsByIndex[i];
            const tabCtx = { ...ctx, tab, i };
            for (const binding of this.perTabBindings) {
                binding.apply(state, tabCtx);
            }
        }
        this.tui.requestRender();
    }
    /**
     * Invalidates every owned renderable. Called by the session in place of
     * the old `dialog.invalidate()` forwarding chain — DialogView no longer
     * reaches into siblings (chatRow, tabBar, notesInput, activePreviewPane).
     * Iterates the same registries used by `apply()` plus
     * `extraInvalidatables` for components outside the binding system.
     */
    invalidate() {
        for (const b of this.globalBindings)
            b.invalidate();
        for (const tab of this.tabsByIndex) {
            tab.optionList.invalidate();
            tab.preview.invalidate();
            tab.multiSelect?.invalidate();
        }
        for (const x of this.extraInvalidatables)
            x.invalidate();
    }
}
//# sourceMappingURL=props-adapter.js.map