/*!
 * KoliBri - The accessible HTML-Standard
 */
import{r as i,h as t,H as s}from"./index-C_yiBYg2.js";import{a as o,b as e}from"./align-floating-elements-7Np6z5EU.js";import{w as h,t as l}from"./common-Dd0zJQbD.js";import{v as n}from"./align-D-ifSKu8.js";import{v as r}from"./show-DdPK3aM7.js";import{c as a}from"./clsx-eK3rPvPS.js";import{d,K as c}from"./events-9-skLO0f.js";const v=(e,t)=>{h(e,"_on",e=>"object"==typeof e&&null!==e,new Set(["PopoverCallbacksPropType {Events.onClose}"]),t)},f=class{constructor(t){i(this,t),this.alignPopover=async()=>{this.popoverElement&&(this.triggerElement&&await o({align:this._align,referenceElement:this.triggerElement,arrowElement:this.arrowElement,floatingElement:this.popoverElement}),this.popoverElement.style.removeProperty("visibility"))},this.handleBeforeToggle=e=>{"open"===e.newState&&this.popoverElement&&(this.popoverElement.style.visibility="hidden")},this.handleToggle=t=>{var o,i,s,l,n,r;const h=t,a="open"===h.newState;if(this.state=Object.assign(Object.assign({},this.state),{_show:a,_visible:a}),a)null===(o=this.cleanupAutoUpdate)||void 0===o||o.call(this),this.triggerElement&&this.popoverElement&&(this.cleanupAutoUpdate=e(this.triggerElement,this.popoverElement,()=>{this.alignPopover()})),this.addListenersToBody(),this.alignPopover();else{null===(i=this.cleanupAutoUpdate)||void 0===i||i.call(this),this.cleanupAutoUpdate=void 0,this.removeListenersFromBody();const e=null!==(s=this.lastCloseEvent)&&void 0!==s?s:h;this.lastCloseEvent=void 0,null===(l=this.triggerElement)||void 0===l||l.focus(),null===(r=null===(n=this.state._on)||void 0===n?void 0:n.onClose)||void 0===r||r.call(n,e),this.host&&d(this.host,c.close)}this._show!==a&&(this._show=a)},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 t;this.lastCloseEvent=e,null===(t=this.popoverElement)||void 0===t||t.hidePopover()},this.handleEscape=e=>{"Escape"===e.key&&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 e;const t=l(),o=t.body;o.addEventListener("keyup",this.handleEscape,{capture:!0}),o.addEventListener("click",this.hidePopoverByClickOutside,{capture:!0}),null===(e=t.scrollingElement)||void 0===e||e.addEventListener("scroll",this.handleScroll,{passive:!0})}removeListenersFromBody(){var e;const t=l(),o=t.body;o.removeEventListener("keyup",this.handleEscape,{capture:!0}),o.removeEventListener("click",this.hidePopoverByClickOutside,{capture:!0}),null===(e=t.scrollingElement)||void 0===e||e.removeEventListener("scroll",this.handleScroll)}syncPopoverVisibility(){if(!this.popoverElement)return;const e=()=>{if(!this.popoverElement)return;const e=this.popoverElement.matches(":popover-open");this._show?e||this.popoverElement.showPopover():e&&this.popoverElement.hidePopover()};this.popoverElement.isConnected?e():requestAnimationFrame(()=>{e()})}render(){return t(s,{key:"4bd2902259ca45002759ad451dffa053066d980d",ref:this.catchHostAndTriggerElement,class:"kol-popover"},t("div",{key:"afc3a81d0eb739c5fb17c7fced21c42ef6014dee",class:a("kol-popover__content",{"kol-popover__content--visible":this.state._visible}),ref:this.catchPopoverElement,popover:"auto"},t("div",{key:"01daedd84147cd6c8046661ec332864fb6a92008",class:a("kol-popover__arrow",`kol-popover__arrow--${this.state._align}`),ref:this.catchArrowElement}),t("slot",{key:"cbd9a60997ba870b166ce92b0c18871d5e99ca92"})))}validateAlign(e){n(this,e),this.alignPopover()}validateOn(e){v(this,e)}validateShow(e){if(r(this,e),this.popoverElement){const t=this.popoverElement.matches(":popover-open");if(e){if(!t)try{this.popoverElement.showPopover()}catch(e){}}else if(t)try{this.popoverElement.hidePopover()}catch(e){}}}componentWillLoad(){this.validateAlign(this._align),this.validateShow(this._show)}disconnectedCallback(){var e,t,o;null===(e=this.popoverElement)||void 0===e||e.removeEventListener("beforetoggle",this.handleBeforeToggle),null===(t=this.popoverElement)||void 0===t||t.removeEventListener("toggle",this.handleToggle),null===(o=this.cleanupAutoUpdate)||void 0===o||o.call(this),this.cleanupAutoUpdate=void 0,this.removeListenersFromBody()}static get watchers(){return{_align:["validateAlign"],_on:["validateOn"],_show:["validateShow"]}}};export{f as kol_popover_wc};