import { Context } from "@lit/context"; import { DataTag, DefaultLayerError, ErrorOf, InferValidatorOutput, LayerCallContext, LayerClient, LayerComponentProps, LayerGroupOptions, LayerHandle, LayerKey, LayerOptions, LayerState, OmitKeyof, OpenLayerOptions, ResponseOf, ValidatedLayerHandle, Validator } from "@stainless-code/layers"; import { LitElement, PropertyValues, ReactiveController, ReactiveControllerHost, TemplateResult, nothing } from "lit"; export * from "@stainless-code/layers"; //#region src/index.d.ts /** * `@lit/context` key for the nearest {@link LayerClient}. * Exported for advanced `ContextProvider` wiring; prefer * {@link provideLayerClient} / {@link useLayerClient}. */ declare const layerClientContext: Context; /** Lit host that can own reactive controllers and participate in `@lit/context`. */ type LitControllerHost = ReactiveControllerHost & HTMLElement; /** * Provides a {@link LayerClient} to descendant consumers via `@lit/context`. * * Attaches a `@lit/context` `ContextProvider` to `host`; descendants resolve it * with {@link useLayerClient} or by omitting `client` on a hook. A new client is * created when `client` is omitted. * * @param host Lit element host that owns the context provider. * @param client Optional client to provide; a new {@link LayerClient} is created when omitted. * @returns The provided client. */ declare function provideLayerClient(host: LitControllerHost, client?: LayerClient): LayerClient; /** * Reactive controller that resolves the nearest {@link LayerClient} from * `@lit/context` and exposes it via {@link LayerClientConsumer.current}. * * The value is undefined until the host connects under a provider; `.current` * throws when accessed before a client is available. */ declare class LayerClientConsumer implements ReactiveController { #private; constructor(host: LitControllerHost); hostConnected(): void; hostDisconnected(): void; /** The resolved client; throws when no provider has supplied one yet. */ get current(): LayerClient; } /** * Resolve the nearest {@link LayerClient} from `@lit/context`. * * Returns a {@link LayerClientConsumer} controller; read the client via * `.current` (throws when no provider has supplied one yet). Prefer passing * `client` explicitly on `useStack` / `useLayer` for synchronous access at * construction time — context only resolves once the host is connected. * * @returns A {@link LayerClientConsumer} controller; read `.current` for the client. */ declare function useLayerClient(host: LitControllerHost): LayerClientConsumer; type NoValidateOptions = Opts extends { validate: Validator; } ? never : Opts; interface UseStackOptions { stack?: string; select?: (states: LayerState[]) => T; compare?: (a: T, b: T) => boolean; client?: LayerClient; } interface UseLayerStateOptions, ErrorOf, D>[]> { key: Key; stack?: string; select?: (states: LayerState, ErrorOf, D>[]) => U; compare?: (a: U, b: U) => boolean; } /** * Reactive controller that mirrors a {@link LayerClient} stack snapshot. * Prefer {@link useStack} / {@link useQueuedStack}; `queued` / `deferClient` / * {@link bindClient} are for adapter internals (shared lazy context resolve). */ declare class StackController implements ReactiveController { #private; /** * @param queued When `true`, observe {@link LayerStack.getQueuedSnapshot} * instead of the mounted snapshot (`useQueuedStack`). * @param deferClient When `true`, skip lazy context resolve so a sibling * controller can call {@link bindClient} after one shared resolve. */ constructor(host: ReactiveControllerHost, options?: UseStackOptions, client?: LayerClient, queued?: boolean, deferClient?: boolean); /** * Bind a {@link LayerClient} when constructed with `deferClient` (internal: * {@link LayerController} / {@link LayerGroupController} share one lazy * context resolve across their stack controllers). */ bindClient(client: LayerClient): void; /** * Tear down the current subscription, apply new options, and re-subscribe when * the host is connected. */ reconfigure(options?: UseStackOptions, client?: LayerClient): void; hostConnected(): void; hostDisconnected(): void; /** Selected stack value; updates when the stack publishes a new snapshot. */ get current(): T; } /** * Subscribe a Lit host to a {@link LayerClient} stack via {@link StackController}. * * @param host Reactive controller host (typically `this` on a `LitElement`). * @param opts `stack`, `select`, `compare`, and optional `client`. * @param client Client to observe when not passed on `opts`. * @returns A {@link StackController} whose `.current` mirrors the selected snapshot. * @default opts.stack `"default"` * @default opts.select all mounted states (identity) * @default opts.compare `Object.is` */ declare function useStack(host: ReactiveControllerHost, opts?: UseStackOptions, client?: LayerClient): StackController; /** * Subscribe a Lit host to a stack's queued snapshot via {@link StackController}. * * @param host Reactive controller host (typically `this` on a `LitElement`). * @param opts `stack`, `select`, `compare`, and optional `client`. * @param client Client to observe when not passed on `opts`. * @returns A {@link StackController} whose `.current` mirrors the queued snapshot. */ declare function useQueuedStack(host: ReactiveControllerHost, opts?: UseStackOptions, client?: LayerClient): StackController; /** * Observe all mounted layers matching a key. * * A {@link DataTag} key infers its response and error types. */ declare function useLayerState, ErrorOf, D>[]>(host: ReactiveControllerHost, opts: UseLayerStateOptions, client?: LayerClient): StackController; /** Observe all queued layers matching a key. */ declare function useLayerQueuedState, ErrorOf, D>[]>(host: ReactiveControllerHost, opts: UseLayerStateOptions, client?: LayerClient): StackController; type WiredLayerHandle = LayerHandle & { state: StackController[]>; queued: StackController[]>; top: LayerState | null; }; type WiredValidatedLayerHandle, R, E = DefaultLayerError, D = unknown, RP = unknown> = ValidatedLayerHandle & { state: StackController, R, E, D>[]>; queued: StackController, R, E, D>[]>; top: LayerState, R, E, D> | null; }; /** * Reactive controller wiring `createLayer` with reactive `state` / `queued` / * `top` for a single layer key. */ declare class LayerController implements ReactiveController { #private; constructor(host: ReactiveControllerHost, options: LayerOptions & { key: LayerKey; }, client?: LayerClient); hostConnected(): void; hostDisconnected(): void; get open(): LayerHandle["open"]; get upsert(): LayerHandle["upsert"]; get dismiss(): LayerHandle["dismiss"]; get update(): LayerHandle["update"]; get cancelQueued(): LayerHandle["cancelQueued"]; get client(): LayerHandle["client"]; get stack(): LayerHandle["stack"]; get options(): LayerHandle["options"]; get current(): LayerHandle["current"]; get state(): StackController[]>; get queued(): StackController[]>; get top(): LayerState | null; } /** Wired handle: `createLayer` + reactive `state`/`queued`/`top`. */ declare function useLayer, R, E = DefaultLayerError, D = unknown, RP = unknown>(host: ReactiveControllerHost, options: LayerOptions, R, E, D, RP> & { key: LayerKey; validate: V; }, client?: LayerClient): WiredValidatedLayerHandle; declare function useLayer(host: ReactiveControllerHost, options: NoValidateOptions & { key: LayerKey; }>, client?: LayerClient): WiredLayerHandle; interface StackHandles { states: StackController; getCall: (state: LayerState) => LayerCallContext; } /** * Reactive controller exposing the states and call contexts needed to render * a stack headlessly (without `StackOutlet`). */ declare class StackHandlesController implements ReactiveController { #private; constructor(host: ReactiveControllerHost, stack?: string, rootProps?: unknown, client?: LayerClient); hostConnected(): void; hostDisconnected(): void; get states(): StackController[]>; getCall: (state: LayerState) => LayerCallContext; } /** Return the states and call contexts needed to render a stack headlessly. */ declare function useStackHandles(host: ReactiveControllerHost, stack?: string, rootProps?: unknown, client?: LayerClient): StackHandlesController; interface MutationRun { /** On success, end the layer with `response`; on failure, leave it open and rethrow. */ orEnd: (response: R) => Promise; } interface MutationFlow { /** True while a `run(...)` async action is in flight. Mirrors the layer's `actionStatus: "running"`. */ pending: boolean; run: (fn: () => Promise | void) => MutationRun; } /** * Reactive controller coordinating a layer's pending state with an async * mutation and ending it on success. */ declare class MutationFlowController implements ReactiveController { #private; constructor(host: ReactiveControllerHost, call: LayerCallContext); hostConnected(): void; hostDisconnected(): void; get pending(): boolean; run: (fn: () => Promise | void) => MutationRun; } /** * Coordinate a layer's pending state with an async mutation and end it on success. * * @example * ```ts * @customElement("confirm-dialog") * class ConfirmDialog extends LitElement { * @property({ attribute: false }) call!: LayerCallContext; * #flow = new MutationFlowController(this, this.call); * render() { * return html``; * } * } * ``` */ declare function useMutationFlow(host: ReactiveControllerHost, call: LayerCallContext): MutationFlowController; /** Open a layer on a pre-bound stack, with {@link DataTag} response and error inference. */ interface ScopedOpen { (options: OmitKeyof & { key: DataTag; }, "stack" | "validate">): Promise; (options: OmitKeyof, "stack">): Promise; } interface LayerGroup { open: ScopedOpen; dismissAll: (response?: unknown) => void; states: StackController; /** Renders the child stack inline — place inside the parent layer's DOM. */ outlet: (rootProps?: unknown) => TemplateResult; stackId: string; } /** * Reactive controller for a child stack scoped to the calling layer's lifetime. * * The child stack is disposed and cleared via `cancelAll` when its parent * layer unmounts (`LayerCancelledError`). * {@link LayerGroupController.outlet} returns a `TemplateResult` that renders the * child stack inline (router `Routes.outlet()`-shaped) — share the render helper * with `StackOutlet`. */ declare class LayerGroupController implements ReactiveController { #private; constructor(host: ReactiveControllerHost, call: LayerCallContext, options?: LayerGroupOptions, client?: LayerClient); hostConnected(): void; hostDisconnected(): void; get stackId(): string; get states(): StackController; open: ScopedOpen; dismissAll: (response?: unknown) => void; outlet: (rootProps?: unknown) => TemplateResult; } /** * Create a child stack scoped to the calling layer's lifetime. * * The child stack is disposed and cleared via `cancelAll` when its parent * layer unmounts (`LayerCancelledError`). */ declare function useLayerGroup(host: ReactiveControllerHost, call: LayerCallContext, options?: LayerGroupOptions, client?: LayerClient): LayerGroup; /** A layer component is either a `LitElement` constructor or a render function. */ type LitLayerComponent = (new () => LitElement) | ((props: LayerComponentProps) => TemplateResult); /** * `` — provides a {@link LayerClient} via `@lit/context`. * Shadow root + ``; composed `context-request` still reaches light children. * * Register with {@link defineStackElements}. Omitting `.client` creates one. */ declare class StackProvider extends LitElement { #private; static properties: { client: { attribute: boolean; }; }; client: LayerClient | undefined; constructor(); connectedCallback(): void; updated(changed: PropertyValues): void; render(): TemplateResult; } /** * `` — renders every active layer in a stack with its registered * component. Light DOM (`createRenderRoot()` returns `this`) so overlays stack * inline where mounted. Id-keyed `repeat` keeps instances stable across updates. */ declare class StackOutlet extends LitElement { #private; static properties: { stack: { type: StringConstructor; }; rootProps: { attribute: boolean; }; client: { attribute: boolean; }; }; stack: string; rootProps: unknown; client: LayerClient | undefined; constructor(); createRenderRoot(): this; updated(changed: PropertyValues): void; connectedCallback(): void; render(): TemplateResult | typeof nothing; } /** * `` — renders a selected stack value through a `.renderer` * callback (virtualizer `.renderItem`-shaped). Set `.stack`, `.selector`, and * `.renderer` (a `(value) => TemplateResult`). */ declare class StackSubscribe extends LitElement { #private; static properties: { stack: { type: StringConstructor; }; selector: { attribute: boolean; }; renderer: { attribute: boolean; }; }; stack: string; selector: (states: LayerState[]) => unknown; renderer: (value: unknown) => TemplateResult; constructor(); createRenderRoot(): this; connectedCallback(): void; updated(changed: PropertyValues): void; render(): TemplateResult; } interface AppStack { open: ScopedOpen; dismissAll: (response?: unknown) => void; states: StackController; } /** * Reactive controller exposing `open` / `dismissAll` / `states` bound to one * stack. Returned by {@link createStackHook} as `useAppStack`. */ declare class AppStackController implements ReactiveController { #private; constructor(host: ReactiveControllerHost, client: LayerClient | undefined, stackId: string); hostConnected(): void; hostDisconnected(): void; get states(): StackController; open: ScopedOpen; dismissAll: (response?: unknown) => void; } /** * `` — light-DOM host that renders a `stack-outlet` for its `.stack`. * Forward host props to layers via `.rootProps` (defaults to the host element). * Register with {@link defineStackElements}; {@link createStackHook} returns a * subclass bound to its stack id. */ declare class AppHostElement extends LitElement { static properties: { stack: { type: StringConstructor; }; rootProps: { attribute: boolean; }; }; stack: string; rootProps: unknown; constructor(); createRenderRoot(): this; render(): TemplateResult; } interface AppLayerProps { /** Layer definition with the stack supplied by the factory. */ options: OmitKeyof, "stack">; /** Controlled visibility. `true` opens the layer; `false` dismisses it. */ open: boolean; payload: P; /** Called when the layer resolves. */ onResolved?: (response: R) => void; } /** * Reactive controller for a controlled layer bound to one stack. Set `.open` to * `true` to open and `false` to dismiss; `.payload` / `.options` / `.onResolved` * update the live layer. Dismisses automatically when the host disconnects. */ declare class AppLayerController

implements ReactiveController { #private; options: OmitKeyof, "stack">; payload: P; onResolved?: (response: R) => void; constructor(host: ReactiveControllerHost, client: LayerClient | undefined, stackId: string, props: AppLayerProps); get open(): boolean; set open(v: boolean); hostConnected(): void; hostDisconnected(): void; } interface StackHook { /** Provider CE subclass bound to the hook's default client. */ StackProvider: typeof StackProvider; /** Reactive controller for the bound stack's `open` / `dismissAll` / `states`. */ useAppStack: (host: ReactiveControllerHost) => AppStackController; /** `` subclass bound to the hook's stack id. */ AppHost: typeof AppHostElement; /** Controlled-layer controller constructor bound to the hook's stack. */ AppLayer: new (host: ReactiveControllerHost, props: AppLayerProps) => AppLayerController; } /** * Create a provider, app-stack controller, app-host, and controlled-layer * controller bound to one stack. Mirrors the React/Vue/Solid `createStackHook`. */ declare function createStackHook(config?: { client?: LayerClient; stack?: string; }): StackHook; /** * Idempotently register the stack custom elements: `stack-provider`, * `stack-outlet`, `stack-subscribe`, and `app-host`. Safe to call multiple * times; not auto-invoked on import (tenets 2 + 4). */ declare function defineStackElements(): void; //#endregion export { AppHostElement, AppLayerController, AppLayerProps, AppStack, AppStackController, LayerClientConsumer, LayerController, LayerGroup, LayerGroupController, LitControllerHost, LitLayerComponent, MutationFlow, MutationFlowController, MutationRun, ScopedOpen, StackController, StackHandles, StackHandlesController, StackHook, StackOutlet, StackProvider, StackSubscribe, UseLayerStateOptions, UseStackOptions, WiredLayerHandle, WiredValidatedLayerHandle, createStackHook, defineStackElements, layerClientContext, provideLayerClient, useLayer, useLayerClient, useLayerGroup, useLayerQueuedState, useLayerState, useMutationFlow, useQueuedStack, useStack, useStackHandles };