import{menu_styles_default as e}from"./chunk.Z75LPS3C.js";import{SlMenuItem as t}from"./chunk.NMJWOOZI.js";import{ShoelaceElement as n}from"./chunk.Z7XDKKOD.js";import{__decorateClass as r}from"./chunk.6M63UXML.js";import{html as s}from"../../../../lit/index.js";import{query as o}from"../../../../lit/decorators.js";var l=class extends n{connectedCallback(){super.connectedCallback(),this.setAttribute("role","menu")}handleClick(e){if(!(e.target instanceof t))return;const n=e.target;"checkbox"===n.type&&(n.checked=!n.checked),this.emit("sl-select",{detail:{item:n}})}handleKeyDown(e){if("Enter"===e.key||" "===e.key){const t=this.getCurrentItem();e.preventDefault(),e.stopPropagation(),null==t||t.click()}else if(["ArrowDown","ArrowUp","Home","End"].includes(e.key)){const t=this.getAllItems(),n=this.getCurrentItem();let r=n?t.indexOf(n):0;t.length>0&&(e.preventDefault(),e.stopPropagation(),"ArrowDown"===e.key?r++:"ArrowUp"===e.key?r--:"Home"===e.key?r=0:"End"===e.key&&(r=t.length-1),r<0&&(r=t.length-1),r>t.length-1&&(r=0),this.setCurrentItem(t[r]),t[r].focus())}}handleMouseDown(e){const t=e.target;this.isMenuItem(t)&&this.setCurrentItem(t)}handleSlotChange(){const e=this.getAllItems();e.length>0&&this.setCurrentItem(e[0])}isMenuItem(e){var t;return"sl-menu-item"===e.tagName.toLowerCase()||["menuitem","menuitemcheckbox","menuitemradio"].includes(null!=(t=e.getAttribute("role"))?t:"")}getAllItems(){return[...this.defaultSlot.assignedElements({flatten:!0})].filter((e=>!(e.inert||!this.isMenuItem(e))))}getCurrentItem(){return this.getAllItems().find((e=>"0"===e.getAttribute("tabindex")))}setCurrentItem(e){this.getAllItems().forEach((t=>{t.setAttribute("tabindex",t===e?"0":"-1")}))}render(){return s`
      <slot
        @slotchange=${this.handleSlotChange}
        @click=${this.handleClick}
        @keydown=${this.handleKeyDown}
        @mousedown=${this.handleMouseDown}
      ></slot>
    `}};l.styles=e,r([o("slot")],l.prototype,"defaultSlot",2);export{l as SlMenu};