import{r as t,c as s,a as i,h as e}from"./p-wX3eJOEL.js";import{i as h}from"./p-CtINY9El.js";import{d as n,t as o}from"./p-DNHOWgWh.js";import{c as a}from"./p-BfP9ezJQ.js";const c=class{constructor(i){t(this,i),this.ifxOpen=s(this,"ifxOpen",7),this.ifxClose=s(this,"ifxClose",7),this.ifxDropdown=s(this,"ifxDropdown",7)}placement="bottom-start";defaultOpen=!1;internalIsOpen=!1;noAppendToBody=!1;ifxOpen;ifxClose;ifxDropdown;disabled;noCloseOnOutsideClick=!1;noCloseOnMenuClick=!1;get el(){return i(this)}trigger;menu;popperInstance;handleOutsideClick(t){const s=t.target;this.noCloseOnOutsideClick||this.el.contains(s)||this.menu.contains(s)||this.closeDropdown()}handleFocusOutside(t){const s=t.target;!this.internalIsOpen||this.el.contains(s)||this.menu.contains(s)||this.closeDropdown()}handleTriggerClick=()=>{this.internalIsOpen?this.closeDropdown():this.openDropdown()};handleTriggerKeyDown=t=>{"Tab"===t.key&&this.internalIsOpen&&!t.shiftKey&&(t.preventDefault(),this.focusFirstItem())};handleMenuClick=()=>{this.noCloseOnMenuClick||this.closeDropdown()};getItemFocusables(){return this.menu?Array.from(this.menu.querySelectorAll("ifx-dropdown-item")).filter((t=>!("true"===t.getAttribute("hide")||t.classList.contains("hide")))).map((t=>t.shadowRoot?.querySelector("a"))).filter((t=>!!t)):[]}handleMenuKeyDown=t=>{const s=this.getItemFocusables();if(!s.length)return;const i=s.indexOf(document.activeElement);switch(t.key){case"ArrowDown":t.preventDefault(),s[(i+1)%s.length].focus();break;case"ArrowUp":t.preventDefault(),s[(i-1+s.length)%s.length].focus();break;case"Home":t.preventDefault(),s[0].focus();break;case"End":t.preventDefault(),s[s.length-1].focus();break;case"Enter":case" ":case"Spacebar":t.preventDefault(),document.activeElement?.click();break;case"Escape":t.preventDefault(),this.closeDropdown(),this.trigger?.focus();break;case"Tab":t.shiftKey&&0===i&&(t.preventDefault(),this.closeDropdown(),this.trigger?.focus())}};componentWillLoad(){this.updateSlotContent(),this.watchHandlerIsOpen(this.defaultOpen,this.internalIsOpen)}async componentDidLoad(){if(!h(this.el)){const t=n();o("ifx-dropdown",await t)}}watchHandlerIsOpen(t,s){t!==s&&t!==this.internalIsOpen&&(t?this.openDropdown():this.closeDropdown())}watchHandlerDisabled(t){this.trigger&&(this.trigger.disabled=t)}watchHandlerSlot(){this.updateSlotContent()}focusFirstItem(){const[t]=this.getItemFocusables();t?.focus()}updateSlotContent(){const t=this.el.querySelector("ifx-dropdown-trigger-button, ifx-dropdown-trigger");t!==this.trigger&&(this.trigger&&(this.trigger.removeEventListener("click",this.handleTriggerClick),this.trigger.removeEventListener("keydown",this.handleTriggerKeyDown)),this.trigger=t,this.trigger&&(this.trigger.disabled=this.disabled,this.trigger.addEventListener("click",this.handleTriggerClick),this.trigger.addEventListener("keydown",this.handleTriggerKeyDown)));const s=this.el.querySelector("ifx-dropdown-menu");this.noAppendToBody?this.menu=s:(this.menu&&this.menu!==s&&(this.menu.removeEventListener("click",this.handleMenuClick),this.menu.removeEventListener("keydown",this.handleMenuKeyDown),this.menu.remove()),this.menu=s,this.menu&&!document.body.contains(this.menu)&&document.body.append(this.menu)),this.menu&&(this.menu.removeEventListener("click",this.handleMenuClick),this.menu.removeEventListener("keydown",this.handleMenuKeyDown),this.menu.addEventListener("click",this.handleMenuClick),this.menu.addEventListener("keydown",this.handleMenuKeyDown))}disconnectedCallback(){this.popperInstance&&(this.popperInstance.destroy(),this.popperInstance=null),this.menu&&this.menu.remove()}async isOpen(){return this.internalIsOpen}async closeDropdown(){this.internalIsOpen&&(this.internalIsOpen=!1,this.trigger.isOpen=!1,this.menu.isOpen=!1,this.ifxClose.emit()),this.popperInstance&&(this.popperInstance.destroy(),this.popperInstance=null)}async openDropdown(){this.internalIsOpen||this.disabled||(this.internalIsOpen=!0,this.trigger.isOpen=!0,this.menu.isOpen=!0,this.popperInstance=a(this.el,this.menu,{placement:this.placement,strategy:"fixed"}),this.ifxOpen.emit())}render(){return e("div",{key:"4835afaecc8b4ea2095ab573c6cb9b2ade14417f","aria-label":"dropdown menu",class:"dropdown"},e("slot",{key:"3f7d76fc61ed684e62c6d2cce0931f9b6233edae"}))}static get watchers(){return{defaultOpen:[{watchHandlerIsOpen:0}],disabled:[{watchHandlerDisabled:0}]}}};c.style=":host{display:inline-block}";export{c as ifx_dropdown}