"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    AtomicTabPopover: ()=>AtomicTabPopover
});
const core_namespaceObject = require("@popperjs/core");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const ref_js_namespaceObject = require("lit/directives/ref.js");
const when_js_namespaceObject = require("lit/directives/when.js");
const external_button_cjs_namespaceObject = require("../button.cjs");
const binding_guard_cjs_namespaceObject = require("../../../decorators/binding-guard.cjs");
const bindings_cjs_namespaceObject = require("../../../decorators/bindings.cjs");
const error_guard_cjs_namespaceObject = require("../../../decorators/error-guard.cjs");
const with_tailwind_styles_cjs_namespaceObject = require("../../../decorators/with-tailwind-styles.cjs");
const arrow_bottom_rounded_cjs_namespaceObject = require("../../../images/arrow-bottom-rounded.cjs");
var arrow_bottom_rounded_cjs_default = /*#__PURE__*/ __webpack_require__.n(arrow_bottom_rounded_cjs_namespaceObject);
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 AtomicTabPopover extends external_lit_namespaceObject.LitElement {
    initialize() {
        this.addEventListener('keydown', this.handleKeyDown);
    }
    connectedCallback() {
        super.connectedCallback();
        this.addEventListener('focusout', this.handleFocusOut);
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.removeEventListener('keydown', this.handleKeyDown);
        this.removeEventListener('focusout', this.handleFocusOut);
        this.popperInstance?.destroy();
    }
    toggle() {
        this.isOpen = !this.isOpen;
    }
    setButtonVisibility(isVisible) {
        this.show = isVisible;
    }
    closePopoverOnFocusOut(event) {
        const popup = this.popupRef.value;
        if (!popup) return;
        const slot = popup.children[0];
        if (!slot) return;
        const assignedElements = slot.assignedElements();
        const isMovingToSlottedElement = assignedElements.some((element)=>element === event.relatedTarget || element.contains(event.relatedTarget));
        if (isMovingToSlottedElement) return;
        if (!popup.contains(event.relatedTarget)) this.closePopover();
    }
    getTabButtons() {
        const popup = this.popupRef.value;
        if (!popup) return [];
        const slot = popup.children[0];
        if (!slot) return [];
        return Array.from(slot.assignedElements()).map((el)=>el.querySelector('button')).filter((el)=>null !== el);
    }
    getCurrentTabIndex(elements) {
        const MAX_SHADOW_DEPTH = 3;
        let currentElement = document.activeElement;
        let depth = 0;
        while(currentElement?.shadowRoot && depth < MAX_SHADOW_DEPTH){
            currentElement = currentElement.shadowRoot.activeElement;
            depth++;
        }
        if (!(currentElement instanceof HTMLButtonElement)) return -1;
        return elements.indexOf(currentElement);
    }
    navigatePopover(key) {
        const tabButtons = this.getTabButtons();
        if (!tabButtons.length) return;
        const currentIndex = this.getCurrentTabIndex(tabButtons);
        const startIndex = currentIndex > -1 ? currentIndex : -1;
        let nextIndex;
        nextIndex = -1 === currentIndex ? 'ArrowDown' === key ? 0 : tabButtons.length - 1 : 'ArrowDown' === key ? (startIndex + 1) % tabButtons.length : (startIndex - 1 + tabButtons.length) % tabButtons.length;
        tabButtons[nextIndex]?.focus();
    }
    get slotElements() {
        return this.children;
    }
    get hasTabs() {
        const popup = this.popupRef.value;
        return popup ? !!popup.children.length : false;
    }
    closePopover() {
        this.isOpen = false;
    }
    initializePopper() {
        const button = this.buttonRef.value;
        const popup = this.popupRef.value;
        if (this.popperInstance || !button || !popup) return;
        this.popperInstance = (0, core_namespaceObject.createPopperLite)(button, popup, {
            placement: 'bottom-end',
            modifiers: [
                core_namespaceObject.preventOverflow
            ]
        });
        popup.classList.add('popover-nested');
    }
    updated() {
        this.initializePopper();
        this.popperInstance?.forceUpdate();
    }
    renderDropdownButton() {
        const label = this.bindings?.i18n.t('more');
        const ariaLabel = this.bindings?.i18n.t('tab-popover', {
            label
        });
        const buttonClasses = 'relative pb-1 mt-1 group mr-6 font-semibold flex items-center';
        return (0, external_button_cjs_namespaceObject.renderButton)({
            props: {
                ref: this.buttonRef,
                style: 'text-transparent',
                onClick: ()=>this.toggle(),
                part: 'popover-button',
                ariaExpanded: this.isOpen ? 'true' : 'false',
                ariaLabel,
                ariaControls: this.popoverId,
                class: buttonClasses
            }
        })((0, external_lit_namespaceObject.html)`
      <span
        title=${label}
        part="value-label"
        class="group-hover:text-primary group-focus:text-primary mr-1.5 truncate"
      >
        ${label}
      </span>
      <atomic-icon
        part="arrow-icon"
        class="group-hover:text-primary group-focus:text-primary ml-auto w-2 ${this.isOpen ? 'rotate-180' : ''}"
        .icon=${arrow_bottom_rounded_cjs_default()}
      ></atomic-icon>
    `);
    }
    renderBackdrop() {
        return (0, external_lit_namespaceObject.html)`<div
      part="backdrop"
      class="fixed top-0 right-0 bottom-0 left-0 z-9998 cursor-pointer bg-transparent"
      @click=${()=>this.toggle()}
    ></div>`;
    }
    renderPopover() {
        return (0, external_lit_namespaceObject.html)`
      <div class="relative ${this.isOpen ? 'z-9999' : ''}">
        ${this.renderDropdownButton()}
        <ul
          id=${this.popoverId}
          ${(0, ref_js_namespaceObject.ref)(this.popupRef)}
          part="overflow-tabs"
          class="bg-background border-neutral absolute rounded-lg border py-2 shadow-lg ${this.isOpen ? 'flex' : 'hidden'}"
        >
          <slot></slot>
        </ul>
      </div>
    `;
    }
    render() {
        this.classList.toggle('visibility-hidden', !this.show);
        this.ariaHidden = String(!this.show);
        return (0, external_lit_namespaceObject.html)`
      ${this.renderPopover()} ${(0, when_js_namespaceObject.when)(this.isOpen, ()=>this.renderBackdrop())}
    `;
    }
    constructor(...args){
        super(...args), this.show = false, this.isOpen = false, this.buttonRef = (0, ref_js_namespaceObject.createRef)(), this.popupRef = (0, ref_js_namespaceObject.createRef)(), this.popoverId = 'atomic-tab-popover', this.handleKeyDown = (e)=>{
            if (!this.isOpen) return;
            if ('Escape' === e.key) this.toggle();
            else if ('ArrowDown' === e.key || 'ArrowUp' === e.key) {
                e.preventDefault();
                this.navigatePopover(e.key);
            }
        }, this.handleFocusOut = (event)=>{
            this.closePopoverOnFocusOut(event);
        };
    }
}
AtomicTabPopover.styles = (0, external_lit_namespaceObject.css)`
    :host {
      position: absolute;
    }

    :host(.visibility-hidden) {
      visibility: hidden;
    }

    [part='overflow-tabs'] {
      flex-direction: column;
      max-width: 150px;

      button {
        text-align: start;
      }
    }
  `;
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicTabPopover.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicTabPopover.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicTabPopover.prototype, "show", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicTabPopover.prototype, "isOpen", void 0);
_ts_decorate([
    (0, binding_guard_cjs_namespaceObject.bindingGuard)(),
    (0, error_guard_cjs_namespaceObject.errorGuard)(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicTabPopover.prototype, "render", null);
AtomicTabPopover = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-tab-popover'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles
], AtomicTabPopover);
exports.AtomicTabPopover = __webpack_exports__.AtomicTabPopover;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicTabPopover"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
