/*!
 * KoliBri - The accessible HTML-Standard
 */
import{proxyCustomElement,HTMLElement,h,Host}from"@stencil/core/internal/client";import{a as alignFloatingElements}from"./align-floating-elements.js";import{c as clsx}from"./clsx.js";import{d as dispatchDomEvent,K as KolEvent}from"./events.js";import{w as watchValidator}from"./prop.validators.js";import{v as validateShow}from"./show.js";import{g as getDocument}from"./dev.utils.js";import{a as validateAlign}from"./align.js";const validatePopoverCallbacks=(e,t)=>{watchValidator(e,"_on",(e=>"object"==typeof e&&null!==e),new Set(["PopoverCallbacksPropType {Events.onClose}"]),t)},KolPopover=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.hidePopoverByEscape=e=>{"Escape"===e.key&&this.hidePopover(e)},this.hidePopoverByClickOutside=e=>{this.host&&!this.host.contains(e.target)&&this.hidePopover(e)},this.catchHostAndTriggerElement=e=>{e&&(this.host=e,this.triggerElement=e.previousElementSibling)},this.catchPopoverElement=e=>{this.popoverElement=e},this.catchArrowElement=e=>{this.arrowElement=e},this._align="top",this._on=void 0,this._show=!1,this.state={_align:"top",_on:{},_show:!1,_visible:!1}}async showPopover(){this.addListenersToBody(),this.triggerElement&&this.popoverElement&&(await alignFloatingElements({align:this._align,referenceElement:this.triggerElement,arrowElement:this.arrowElement,floatingElement:this.popoverElement}),this.state=Object.assign(Object.assign({},this.state),{_visible:!0}))}hidePopover(e){var t,o,s;this.state=Object.assign(Object.assign({},this.state),{_visible:!1}),this._show=!1,null===(t=this.triggerElement)||void 0===t||t.focus(),this.removeListenersToBody(),null===(s=null===(o=this.state._on)||void 0===o?void 0:o.onClose)||void 0===s||s.call(o,e),this.host&&dispatchDomEvent(this.host,KolEvent.close)}addListenersToBody(){var e;const t=getDocument().body;t.addEventListener("keyup",this.hidePopoverByEscape),t.addEventListener("click",this.hidePopoverByClickOutside),null===(e=document.scrollingElement)||void 0===e||e.addEventListener("scroll",(()=>{this.showPopover()}),{passive:!0})}removeListenersToBody(){var e;const t=getDocument().body;t.removeEventListener("keyup",this.hidePopoverByEscape),t.removeEventListener("click",this.hidePopoverByClickOutside),null===(e=document.scrollingElement)||void 0===e||e.removeEventListener("scroll",(()=>{this.showPopover()}))}render(){return h(Host,{key:"6e414db89865e4faced8091f9f4484077f903033",ref:this.catchHostAndTriggerElement,class:"kol-popover"},h("div",{key:"64a38574bf8ca2878f871846f9985eef4f0b3394",class:clsx("kol-popover__content",{"kol-popover__content--visible":this.state._visible}),ref:this.catchPopoverElement,hidden:!this.state._show},h("div",{key:"e2fe3722133cc1bc20be4a7ccfc45e0622442892",class:clsx("kol-popover__arrow",`kol-popover__arrow--${this.state._align}`),ref:this.catchArrowElement}),h("slot",{key:"9fba7bbfd2879e01da7c15f026227671bfb4c171"})))}validateAlign(e){validateAlign(this,e)}validateOn(e){validatePopoverCallbacks(this,e)}validateShow(e){validateShow(this,e),e&&this.showPopover()}componentWillLoad(){this.validateAlign(this._align),this.validateShow(this._show)}static get watchers(){return{_align:["validateAlign"],_on:["validateOn"],_show:["validateShow"]}}},[4,"kol-popover-wc",{_align:[1],_on:[16],_show:[1540],state:[32]},void 0,{_align:["validateAlign"],_on:["validateOn"],_show:["validateShow"]}]);function defineCustomElement$1(){if("undefined"==typeof customElements)return;["kol-popover-wc"].forEach((e=>{if("kol-popover-wc"===e)customElements.get(e)||customElements.define(e,KolPopover)}))}const KolPopoverWc=KolPopover,defineCustomElement=defineCustomElement$1;export{KolPopoverWc,defineCustomElement};