import type { BindingContext, GlobalSelector, PerTabBindingContext, PerTabSelector, } from "../state/selectors/contract.js"; import type { QuestionnaireState } from "../state/state.js"; import type { StatefulView } from "./stateful-view.js"; import type { TabComponents } from "./tab-components.js"; export interface ComponentBinding

{ readonly component: StatefulView

; readonly select: GlobalSelector

; } export interface PerTabBinding

{ readonly resolve: (tab: TabComponents) => StatefulView

| undefined; readonly select: PerTabSelector

; readonly predicate?: PerTabSelector; } export interface BoundGlobalBinding { apply(state: QuestionnaireState, ctx: BindingContext): void; invalidate(): void; } export interface BoundPerTabBinding { apply(state: QuestionnaireState, ctx: PerTabBindingContext): void; } export function globalBinding

(spec: ComponentBinding

): BoundGlobalBinding { return { apply: (state, ctx) => spec.component.setProps(spec.select(state, ctx)), invalidate: () => spec.component.invalidate(), }; } export function perTabBinding

(spec: PerTabBinding

): BoundPerTabBinding { return { apply: (state, ctx) => { if (spec.predicate && !spec.predicate(state, ctx)) return; spec.resolve(ctx.tab)?.setProps(spec.select(state, ctx)); }, }; }