import{__decorate as e}from"tslib";import{html as t}from"lit";import{state as i,property as s,query as n,customElement as r}from"lit/decorators.js";import{N as o}from"../internal/nile-element.esm.js";import{s as l}from"./nile-side-bar-action-menu.css.esm.js";import{H as a}from"../internal/slot.esm.js";import"../internal/accessibility/a11y.state.enum.esm.js";import"../internal/accessibility/a11y.property.enum.esm.js";import"../internal/accessibility/role.enum.esm.js";let m=class extends o{constructor(){super(...arguments),this.searchValue="",this.searchWidth=0,this.searchEnabled=!1,this.customSearch=!1,this.showNoResults=!1,this.allowSpaceKey=!1,this.noResultsMessage="No results found",this.hasSlotController=new a(this,"menu__footer","menu__header")}connectedCallback(){super.connectedCallback(),this.setAttribute("role","menu")}handleClick(e){const t=e.target.closest("nile-menu-item");t?.hasSubMenu||t&&!t.disabled&&("checkbox"===t.type&&(t.checked=!t.checked),this.emit("nile-select",{value:t.value}))}handleKeyDown(e){if("Enter"===e.key){const t=this.getCurrentItem();e.preventDefault(),t?.click()}if(this.allowSpaceKey||" "!==e.key||e.preventDefault(),["ArrowDown","ArrowUp","Home","End"].includes(e.key)){const t=this.getAllItems(),i=this.getCurrentItem();let s=i?t.indexOf(i):0;t.length>0&&(e.preventDefault(),"ArrowDown"===e.key?s++:"ArrowUp"===e.key?s--:"Home"===e.key?s=0:"End"===e.key&&(s=t.length-1),s<0&&(s=t.length-1),s>t.length-1&&(s=0),this.setCurrentItem(t[s]),t[s].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){return"nile-menu-item"===e.tagName.toLowerCase()||["menuitem","menuitemcheckbox","menuitemradio"].includes(e.getAttribute("role")??"")}getAllItems(){return[...this.defaultSlot.assignedElements({flatten:!0})].filter((e=>!!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")}))}handleSearchChange(e){let t=this.getAllItems(),i=e.target.value;if(this.customSearch)return void this.emit("nile-search-value",{value:i});let s=0;t.map((t=>{this.searchWidth=Math.max(this.searchWidth,t.offsetWidth),t.innerText.toLowerCase().includes(e.target.value.toLowerCase())?(t.style.display="block",s++):t.style.display="none"})),this.showNoResults=0===s}render(){return t`
      <slot name="menu__pre-header" part="menu__pre-header" @click=${e=>{e.stopPropagation()}}></slot>
      ${this.searchEnabled?t`<div class="search__wrapper" part="search__wrapper">
            <nile-input
              size="medium"
              .value=${this.searchValue}
              placeholder="Search..."
              @nile-input=${this.handleSearchChange}
              part="menu__input"
              clearable
            >
              <nile-icon
                name="var(--nile-icon-search, var(--ng-icon-search-lg))"
                method="var(--nile-svg-method-fill, var(--ng-svg-method-stroke))"
                slot="suffix"
              ></nile-icon>
            </nile-input>
          </div>`:t``}
      <div part="menu__items-wrapper" class="menu__items-wrapper">
        ${this.showNoResults?t` <nile-menu-item>${this.noResultsMessage}</nile-menu-item> `:""}

        <slot
          name="menu__header"
          part="menu__header"
          @click=${e=>{e.stopPropagation()}}
        ></slot>
        <slot
          class="menu__item-wrapper"
          @slotchange=${this.handleSlotChange}
          @click=${this.handleClick}
          @keydown=${this.handleKeyDown}
          @mousedown=${this.handleMouseDown}
        ></slot>
        </div>
        ${this.hasSlotController.test("menu__footer")?t` <slot
              part="menu__footer"
              name="menu__footer"
              class="menu__footer"
            ></slot>`:null}

    `}};m.styles=l,e([i()],m.prototype,"searchValue",void 0),e([i()],m.prototype,"searchWidth",void 0),e([s({type:Boolean,reflect:!0})],m.prototype,"searchEnabled",void 0),e([s({type:Boolean,reflect:!0})],m.prototype,"customSearch",void 0),e([s({type:Boolean})],m.prototype,"showNoResults",void 0),e([s({type:Boolean})],m.prototype,"allowSpaceKey",void 0),e([s({type:String})],m.prototype,"noResultsMessage",void 0),e([n("slot:not([name])")],m.prototype,"defaultSlot",void 0),m=e([r("nile-side-bar-action-menu")],m);export{m as N};
