// #region Imports
// #endregion
/**
 * Controller for FlowBoard composer elements (create forms with trigger+form pattern).
 *
 * @description
 * Provides common functionality for FlowBoard composers:
 * - Open/close state management
 * - Keyboard shortcuts (Escape to close)
 * - Focus management when opening
 * - Host attribute synchronization
 *
 * @remarks
 * This controller is completely data-agnostic. The actual form content
 * and data structure is provided by the user via slots.
 *
 * @public
 */
export class FlowBoardComposerController {
    // #region Fields
    _host;
    _open;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `FlowBoardComposerController` class.
     *
     * @param host - The host element.
     *
     * @public
     */
    constructor(host) {
        this._host = host;
        this._open = false;
        host.addController(this);
    }
    // #endregion
    // #region Properties
    /**
     * Gets or sets whether the composer form is open.
     *
     * @public
     */
    get open() {
        return this._open;
    }
    set open(value) {
        if (this._open !== value) {
            this._open = value;
            this.updateHostAttribute();
            this._host.requestUpdate();
            if (value) {
                this.focusFirstElement();
            }
        }
    }
    // #endregion
    // #region Methods
    /**
     * Toggles the composer between open and closed state.
     *
     * @public
     */
    toggle() {
        this.open = !this.open;
    }
    /**
     * Closes the composer form.
     *
     * @public
     */
    close() {
        this.open = false;
    }
    /**
     * Handles keyboard events in the form.
     * Pressing Escape closes the form.
     *
     * @param evt - The keyboard event.
     *
     * @public
     */
    handleFormKeydown(evt) {
        if (evt.key === 'Escape') {
            evt.preventDefault();
            evt.stopPropagation();
            this.close();
        }
    }
    /**
     * @public
     */
    hostConnected() {
        this.updateHostAttribute();
    }
    /**
     * Updates the host element's `open` attribute based on the open state.
     *
     * @private
     */
    updateHostAttribute() {
        if (this._open) {
            this._host.setAttribute('open', '');
        }
        else {
            this._host.removeAttribute('open');
        }
    }
    /**
     * Focuses the first focusable element in the form slot when opened.
     *
     * @private
     */
    focusFirstElement() {
        const shadowRoot = this._host.shadowRoot;
        if (!shadowRoot) {
            return;
        }
        // Wait for next frame to ensure slot content is ready
        requestAnimationFrame(() => {
            const formSlot = shadowRoot.querySelector('slot[name="form"]');
            if (!formSlot) {
                return;
            }
            const firstFocusable = formSlot.assignedElements()
                .flatMap((el) => [
                el,
                ...Array.from(el.querySelectorAll('input, textarea, select, button, [tabindex="0"]'))
            ])
                .find((el) => el instanceof HTMLElement && !el.hasAttribute('disabled'));
            if (firstFocusable) {
                firstFocusable.focus();
            }
        });
    }
}
/**
 * Adds a FlowBoardComposerController to the host element.
 *
 * @param host - The host element.
 * @returns The controller instance.
 *
 * @public
 */
export function addFlowBoardComposerController(host) {
    return new FlowBoardComposerController(host);
}
//# sourceMappingURL=FlowBoardComposerController.js.map