"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__, {
    AtomicFocusTrap: ()=>AtomicFocusTrap
});
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const boolean_converter_cjs_namespaceObject = require("../../../converters/boolean-converter.cjs");
const watch_cjs_namespaceObject = require("../../../decorators/watch.cjs");
const light_dom_cjs_namespaceObject = require("../../../mixins/light-dom.cjs");
const accessibility_utils_cjs_namespaceObject = require("../../../utils/accessibility-utils.cjs");
const utils_cjs_namespaceObject = require("../../../utils/utils.cjs");
function _ts_decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc);
    else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
    if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(k, v);
}
class AtomicFocusTrap extends (0, light_dom_cjs_namespaceObject.LightDomMixin)(external_lit_namespaceObject.LitElement) {
    connectedCallback() {
        super.connectedCallback();
        document.addEventListener('focusin', this.onFocusChanged);
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.showAll();
        document.removeEventListener('focusin', this.onFocusChanged);
    }
    hide(element) {
        if (element.hasAttribute('aria-hidden') || element.hasAttribute('aria-live') || 'atomic-aria-live' === element.tagName.toLowerCase()) return;
        element.setAttribute('aria-hidden', 'true');
        this.hiddenElements.push(element);
    }
    showAll() {
        let el = this.hiddenElements.pop();
        while(el){
            el.removeAttribute('aria-hidden');
            el = this.hiddenElements.pop();
        }
    }
    hideSiblingsRecursively(element) {
        const parent = (0, utils_cjs_namespaceObject.getParent)(element);
        if (null === parent) return;
        Array.from(parent.children).forEach((sibling)=>{
            if (sibling === element) return;
            if (sibling.assignedSlot && (0, utils_cjs_namespaceObject.isAncestorOf)(this, sibling.assignedSlot)) return;
            this.hide(sibling);
        });
        if (parent !== this.scope) this.hideSiblingsRecursively(parent);
    }
    showSelf() {
        this.parentToHide.removeAttribute('aria-hidden');
        this.parentToHide.removeAttribute('tabindex');
    }
    hideSelf() {
        if (this.shouldHideSelf) {
            this.parentToHide.setAttribute('aria-hidden', 'true');
            this.parentToHide.setAttribute('tabindex', '-1');
        }
    }
    async onDeactivated(isInitialLoad) {
        this.showAll();
        if (!isInitialLoad) {
            await (0, utils_cjs_namespaceObject.defer)();
            this.source?.focus();
        }
        this.hideSelf();
    }
    async onActivated(isInitialLoad) {
        this.showSelf();
        if (!isInitialLoad) {
            await (0, utils_cjs_namespaceObject.defer)();
            (0, accessibility_utils_cjs_namespaceObject.getFirstFocusableDescendant)(this)?.focus();
        }
        this.hideSiblingsRecursively(this);
    }
    async handleActiveChange(_prev, next) {
        const isInitialLoad = next === _prev;
        if (next) await this.onActivated(isInitialLoad);
        else await this.onDeactivated(isInitialLoad);
    }
    get parentToHide() {
        return this.container ?? this;
    }
    constructor(...args){
        super(...args), this.active = false, this.shouldHideSelf = true, this.scope = document.body, this.hiddenElements = [], this.onFocusChanged = (e)=>{
            const elementIsPartOfHost = (focusedElement)=>(0, utils_cjs_namespaceObject.isAncestorOf)(this, focusedElement);
            const elementIsPartOfScope = (focusedElement)=>(0, utils_cjs_namespaceObject.isAncestorOf)(this.scope, focusedElement);
            if (!e.target || !this.active) return;
            const focusedElement = (0, utils_cjs_namespaceObject.getFocusedElement)();
            if (focusedElement && (elementIsPartOfHost(focusedElement) || !elementIsPartOfScope(focusedElement))) return;
            (0, accessibility_utils_cjs_namespaceObject.getFirstFocusableDescendant)(this)?.focus();
        };
    }
}
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        converter: boolean_converter_cjs_namespaceObject.booleanConverter
    })
], AtomicFocusTrap.prototype, "active", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof HTMLElement ? Object : HTMLElement)
], AtomicFocusTrap.prototype, "source", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof HTMLElement ? Object : HTMLElement)
], AtomicFocusTrap.prototype, "container", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        attribute: 'should-hide-self',
        converter: boolean_converter_cjs_namespaceObject.booleanConverter
    })
], AtomicFocusTrap.prototype, "shouldHideSelf", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof Element ? Object : Element)
], AtomicFocusTrap.prototype, "scope", void 0);
_ts_decorate([
    (0, watch_cjs_namespaceObject.watch)('active', {
        waitUntilFirstUpdate: true
    }),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [
        Boolean,
        Boolean
    ]),
    _ts_metadata("design:returntype", Promise)
], AtomicFocusTrap.prototype, "handleActiveChange", null);
AtomicFocusTrap = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-focus-trap')
], AtomicFocusTrap);
exports.AtomicFocusTrap = __webpack_exports__.AtomicFocusTrap;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicFocusTrap"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
