"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    AriaLiveRegionController: ()=>AriaLiveRegionController,
    FocusTargetController: ()=>FocusTargetController,
    getFirstFocusableDescendant: ()=>getFirstFocusableDescendant
});
const external_event_utils_cjs_namespaceObject = require("./event-utils.cjs");
const external_utils_cjs_namespaceObject = require("./utils.cjs");
class AriaLiveRegionController {
    getAriaLiveElement() {
        const event = (0, external_event_utils_cjs_namespaceObject.buildCustomEvent)('atomic/accessibility/findAriaLive', {});
        document.dispatchEvent(event);
        const { element } = event.detail;
        return element;
    }
    dispatchMessage(message) {
        this.getAriaLiveElement()?.updateMessage(this.regionName, message, this.assertive);
    }
    set message(msg) {
        this.dispatchMessage(msg);
    }
    hostUpdate() {
        this.getAriaLiveElement()?.registerRegion(this.regionName, this.assertive);
    }
    constructor(host, regionName, assertive = false){
        this.host = host;
        this.regionName = regionName;
        this.assertive = assertive;
        this.host.addController(this);
    }
}
class FocusTargetController {
    registerFocusCallback(callback) {
        this.publicOnFocusCallbacks.push(callback);
    }
    clearFocusCallbacks() {
        this.internalOnFocusCallback?.();
        while(this.publicOnFocusCallbacks.length)this.publicOnFocusCallbacks.pop()?.();
    }
    async setTarget(el) {
        if (!el) return;
        this.element = el;
        if (this.doFocusOnNextTarget) {
            this.doFocusOnNextTarget = false;
            await this.focus();
        }
    }
    async focus() {
        await (0, external_utils_cjs_namespaceObject.defer)();
        this.element?.focus();
        this.clearFocusCallbacks();
    }
    focusAfterSearch() {
        this.lastSearchId = this.bindings.store.getUniqueIDFromEngine(this.bindings.engine);
        this.doFocusAfterSearch = true;
        return new Promise((resolve)=>{
            this.internalOnFocusCallback = resolve;
        });
    }
    focusOnNextTarget() {
        this.doFocusOnNextTarget = true;
        return new Promise((resolve)=>{
            this.internalOnFocusCallback = resolve;
        });
    }
    disableForCurrentSearch() {
        if (this.bindings.store.getUniqueIDFromEngine(this.bindings.engine) !== this.lastSearchId) this.doFocusAfterSearch = false;
    }
    async hostUpdated() {
        if (this.doFocusAfterSearch && this.bindings.store.getUniqueIDFromEngine(this.bindings.engine) !== this.lastSearchId) {
            this.doFocusAfterSearch = false;
            if (this.element) {
                const el = this.element;
                await (0, external_utils_cjs_namespaceObject.defer)().then(()=>{
                    el.focus();
                    this.clearFocusCallbacks();
                });
            }
        }
    }
    constructor(host, bindings){
        this.host = host;
        this.publicOnFocusCallbacks = [];
        this.doFocusAfterSearch = false;
        this.doFocusOnNextTarget = false;
        this.bindings = bindings;
        this.host.addController(this);
    }
}
function isFocusable(element) {
    if ('-1' === element.getAttribute('tabindex')) return false;
    if (element.hasAttribute('tabindex')) return true;
    if ('true' === element.getAttribute('contentEditable')) return true;
    switch(element.tagName){
        case 'A':
        case 'AREA':
            return element.hasAttribute('href');
        case 'INPUT':
        case 'SELECT':
        case 'TEXTAREA':
        case 'BUTTON':
            return !element.hasAttribute('disabled');
        case 'IFRAME':
            return true;
        default:
            return false;
    }
}
function* getFocusableDescendants(element) {
    if (isFocusable(element)) yield element;
    let children = Array.from(element.children);
    if (element instanceof HTMLSlotElement) children = element.assignedElements();
    else if (element.shadowRoot) children.push(...Array.from(element.shadowRoot.children));
    for (const child of children)yield* getFocusableDescendants(child);
}
function getFirstFocusableDescendant(element) {
    return getFocusableDescendants(element).next().value ?? null;
}
exports.AriaLiveRegionController = __webpack_exports__.AriaLiveRegionController;
exports.FocusTargetController = __webpack_exports__.FocusTargetController;
exports.getFirstFocusableDescendant = __webpack_exports__.getFirstFocusableDescendant;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AriaLiveRegionController",
    "FocusTargetController",
    "getFirstFocusableDescendant"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
