import{r as m,d as g,a as E,G as v,h as l,H as u,w as f}from"./index-DCi8lcS0.js";import{a as _,b}from"./align-floating-elements-6A9FkBi8-xyinCPK6.js";import{v as y}from"./align-zs9k2ruM-Z9QWZaht.js";import{v as w}from"./show-Bv---Je2-4xgeHX5J.js";import{c as d}from"./clsx-eK3rPvPS-B-dksMZM.js";const P=(t,e)=>{f(t,"_on",o=>typeof o=="object"&&o!==null,new Set(["PopoverCallbacksPropType {Events.onClose}"]),e)},C=class{constructor(t){m(this,t),this.alignPopover=async()=>{this.popoverElement&&(this.triggerElement&&await _({align:this._align,referenceElement:this.triggerElement,arrowElement:this.arrowElement,floatingElement:this.popoverElement}),this.popoverElement.style.removeProperty("visibility"))},this.handleBeforeToggle=e=>{e.newState==="open"&&this.popoverElement&&(this.popoverElement.style.visibility="hidden")},this.handleToggle=e=>{var o,n,r,a,s,h;const p=e,i=p.newState==="open";if(this.state=Object.assign(Object.assign({},this.state),{_show:i,_visible:i}),i)(o=this.cleanupAutoUpdate)===null||o===void 0||o.call(this),this.triggerElement&&this.popoverElement&&(this.cleanupAutoUpdate=b(this.triggerElement,this.popoverElement,()=>{this.alignPopover()})),this.addListenersToBody(),this.alignPopover();else{(n=this.cleanupAutoUpdate)===null||n===void 0||n.call(this),this.cleanupAutoUpdate=void 0,this.removeListenersFromBody();const c=(r=this.lastCloseEvent)!==null&&r!==void 0?r:p;this.lastCloseEvent=void 0,(a=this.triggerElement)===null||a===void 0||a.focus(),(h=(s=this.state._on)===null||s===void 0?void 0:s.onClose)===null||h===void 0||h.call(s,c),this.host&&g(this.host,E.close)}this._show!==i&&(this._show=i)},this.catchHostAndTriggerElement=e=>{e&&(this.host=e,this.triggerElement=e.previousElementSibling)},this.catchPopoverElement=e=>{this.popoverElement&&(this.popoverElement.removeEventListener("beforetoggle",this.handleBeforeToggle),this.popoverElement.removeEventListener("toggle",this.handleToggle)),this.popoverElement=e,this.popoverElement&&(this.popoverElement.addEventListener("beforetoggle",this.handleBeforeToggle),this.popoverElement.addEventListener("toggle",this.handleToggle),this.syncPopoverVisibility())},this.catchArrowElement=e=>{this.arrowElement=e},this.requestHide=e=>{var o;this.lastCloseEvent=e,(o=this.popoverElement)===null||o===void 0||o.hidePopover()},this.handleEscape=e=>{e.key==="Escape"&&this.requestHide(e)},this.hidePopoverByClickOutside=e=>{this.host&&!this.host.contains(e.target)&&this.requestHide(e)},this.handleScroll=()=>{this.alignPopover()},this._align="top",this._show=!1,this.state={_align:"top",_on:{},_show:!1,_visible:!1}}addListenersToBody(){var t;const e=v(),o=e.body;o.addEventListener("keyup",this.handleEscape,{capture:!0}),o.addEventListener("click",this.hidePopoverByClickOutside,{capture:!0}),(t=e.scrollingElement)===null||t===void 0||t.addEventListener("scroll",this.handleScroll,{passive:!0})}removeListenersFromBody(){var t;const e=v(),o=e.body;o.removeEventListener("keyup",this.handleEscape,{capture:!0}),o.removeEventListener("click",this.hidePopoverByClickOutside,{capture:!0}),(t=e.scrollingElement)===null||t===void 0||t.removeEventListener("scroll",this.handleScroll)}syncPopoverVisibility(){if(!this.popoverElement)return;const t=()=>{if(!this.popoverElement)return;const e=this.popoverElement.matches(":popover-open");this._show?e||this.popoverElement.showPopover():e&&this.popoverElement.hidePopover()};this.popoverElement.isConnected?t():requestAnimationFrame(()=>{t()})}render(){return l(u,{key:"4bd2902259ca45002759ad451dffa053066d980d",ref:this.catchHostAndTriggerElement,class:"kol-popover"},l("div",{key:"afc3a81d0eb739c5fb17c7fced21c42ef6014dee",class:d("kol-popover__content",{"kol-popover__content--visible":this.state._visible}),ref:this.catchPopoverElement,popover:"auto"},l("div",{key:"01daedd84147cd6c8046661ec332864fb6a92008",class:d("kol-popover__arrow",`kol-popover__arrow--${this.state._align}`),ref:this.catchArrowElement}),l("slot",{key:"cbd9a60997ba870b166ce92b0c18871d5e99ca92"})))}validateAlign(t){y(this,t),this.alignPopover()}validateOn(t){P(this,t)}validateShow(t){if(w(this,t),this.popoverElement){const e=this.popoverElement.matches(":popover-open");if(t){if(!e)try{this.popoverElement.showPopover()}catch{}}else if(e)try{this.popoverElement.hidePopover()}catch{}}}componentWillLoad(){this.validateAlign(this._align),this.validateShow(this._show)}disconnectedCallback(){var t,e,o;(t=this.popoverElement)===null||t===void 0||t.removeEventListener("beforetoggle",this.handleBeforeToggle),(e=this.popoverElement)===null||e===void 0||e.removeEventListener("toggle",this.handleToggle),(o=this.cleanupAutoUpdate)===null||o===void 0||o.call(this),this.cleanupAutoUpdate=void 0,this.removeListenersFromBody()}static get watchers(){return{_align:["validateAlign"],_on:["validateOn"],_show:["validateShow"]}}};export{C as kol_popover_wc};
//# sourceMappingURL=kol-popover-wc.entry-DXzOhrjT.js.map
