// #region Imports
import { ControllerBase } from './Abstracts/ControllerBase';
// #endregion
/**
 * FocusTrapController class to handle focus trapping for Lit components.
 * Restricts focus within the specified Lit component for accessibility support.
 *
 * @public
 */
export class FocusTrapController extends ControllerBase {
    // #region Fields
    _focusableElements;
    _firstFocusableElement;
    _lastFocusableElement;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `FocusTrapController` class.
     *
     * @public
     */
    constructor(host) {
        super(host);
        this._focusableElements = [];
        this._firstFocusableElement = null;
        this._lastFocusableElement = null;
    }
    // #endregion
    // #region Methods
    /**
     * @public
     * @override
     */
    hostConnected() {
        super.hostConnected();
        this.activate();
    }
    /**
     * @public
     * @override
     */
    hostDisconnected() {
        super.hostDisconnected();
        this.deactivate();
    }
    // #endregion
    /**
     * Activates focus trapping within the component.
     *
     * @public
     */
    activate() {
        this._focusableElements = this.getFocusableElements();
        if (this._focusableElements.length > 0) {
            this._firstFocusableElement = this._focusableElements[0];
            this._lastFocusableElement = this._focusableElements[this._focusableElements.length - 1];
            this.host.addEventListener('keydown', this.handleKeyDown);
        }
        this._firstFocusableElement?.focus();
    }
    /**
     * Deactivates focus trapping within the component.
     *
     * @public
     */
    deactivate() {
        this.host.removeEventListener('keydown', this.handleKeyDown);
    }
    /**
     * Finds and returns all focusable elements within the host.
     *
     * @returns {HTMLElement[]} Array of focusable elements.
     */
    getFocusableElements() {
        const focusableSelectors = [
            'a[href]', 'button', 'input', 'textarea', 'select', '[tabindex]:not([tabindex="-1"])'
        ];
        return Array.from(this.host.shadowRoot?.querySelectorAll(focusableSelectors.join(',')) ?? []);
    }
    /**
     * Handles keydown events to trap focus within the component.
     *
     * @param {KeyboardEvent} event - The keyboard event to handle.
     */
    handleKeyDown = (event) => {
        if (event.key === 'Tab') {
            if (event.shiftKey) {
                // Shift + Tab: Loop focus backwards
                if (document.activeElement === this._firstFocusableElement) {
                    event.preventDefault();
                    this._lastFocusableElement?.focus();
                }
            }
            else if (document.activeElement === this._lastFocusableElement) {
                event.preventDefault();
                this._firstFocusableElement?.focus();
            }
        }
    };
}
//# sourceMappingURL=FocusTrapController.js.map