import{html as e,LitElement as t,render as i}from"../../../lit/index.js";import{unsafeHTML as o}from"../../../lit/directives/unsafe-html.js";import"./simple-popover-manager.js";class SimplePopoverSelection extends t{constructor(){super(),this.opened=!1,this.disabled=!1,this.event="click"}openedToggle(e){this.disabled||(this.opened=!this.opened)}openedChanged(t){if(t){let t=globalThis.SimplePopoverManager.requestAvailability();i(globalThis.document.createElement("div"),t);let l,r=globalThis.document.createElement("div"),n=this.querySelectorAll('[slot="options"]');for(var s in 1===n.length&&n[0].children&&1===n[0].children.length&&"SLOT"===n[0].children[0].tagName&&(n=n[0].children[0].assignedNodes({flatten:!0})),n)if(n[s].cloneNode&&n[s].tagName){let e=n[s].cloneNode(!0);e.removeAttribute("slot"),r.appendChild(e)}if(this.querySelector('[slot="style"]')){let t=this.querySelector('[slot="style"]').cloneNode(!0),i=globalThis.document.createElement("style");i.innerHTML=t.innerHTML,l=e`${o(r.innerHTML)}${o(i.outerHTML)}`}else l=e`${o(r.innerHTML)}`;let a=globalThis.document.createElement("div");a.setAttribute("slot","body"),t.appendChild(a),i(l,a),setTimeout((()=>{let e=globalThis.SimplePopoverManager.requestAvailability().querySelectorAll("*");for(var t in e)e[t].addEventListener&&e[t].addEventListener("click",this.itemSelect.bind(this));globalThis.SimplePopoverManager.requestAvailability().querySelector("[data-simple-popover-selection-active]")?globalThis.SimplePopoverManager.requestAvailability().querySelector("[data-simple-popover-selection-active]").focus():globalThis.SimplePopoverManager.requestAvailability()&&globalThis.SimplePopoverManager.requestAvailability().querySelector(":first-child")&&globalThis.SimplePopoverManager.requestAvailability().querySelector(":first-child").focus()}),0)}globalThis.SimplePopoverManager.requestAvailability().setPopover(this,this.querySelector('[slot="button"]'),t)}managerReset(){this.opened=!1}updated(e){e.forEach(((e,t)=>{"opened"==t&&this.shadowRoot&&void 0!==e&&(this.openedChanged(this[t]),this.dispatchEvent(new CustomEvent(`${t}-changed`,{detail:this})))}))}itemSelect(e){this.dispatchEvent(new CustomEvent("simple-popover-selection-changed",{bubbles:!0,detail:e.target})),this.opened=!1,setTimeout((()=>{globalThis.SimplePopoverManager.requestAvailability().setPopover(this,this.querySelector('[slot="button"]'),!1),this.querySelector('[slot="button"]').focus()}),0)}static get properties(){return{opened:{type:Boolean,reflect:!0},disabled:{type:Boolean,reflect:!0},event:{type:String}}}firstUpdated(){"click"===this.event?this.addEventListener("click",this.openedToggle.bind(this)):"hover"===this.event&&(this.addEventListener("mouseenter",this.openPopover.bind(this)),this.addEventListener("focusin",this.openPopover.bind(this)),this.addEventListener("focusout",this.closePopover.bind(this)),this.addEventListener("mouseleave",this.closePopover.bind(this)),this.addEventListener("mouseout",this.closePopover.bind(this)))}openPopover(){this.disabled||setTimeout((()=>{this.opened=!0}),0)}closePopover(){setTimeout((()=>{this.opened=!1}),0)}render(){return e`<slot name="button"></slot>`}}customElements.define("simple-popover-selection",SimplePopoverSelection);export{SimplePopoverSelection};