/*!
 * KoliBri - The accessible HTML-Standard
 */
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const index=require("./index-4e35d174.js"),alignFloatingElements=require("./align-floating-elements-fdc6eb39.js"),prop_validators=require("./prop.validators-37d21dea.js"),dev_utils=require("./dev.utils-58fde089.js"),align=require("./align-f4f3dfb6.js"),show=require("./show-f204ce41.js");require("./reuse-8cfefa9b.js"),require("./index-bf4a6924.js");const validatePopoverCallbacks=(e,t)=>{prop_validators.watchValidator(e,"_on",(e=>"object"==typeof e&&null!==e),new Set(["PopoverCallbacksPropType {Events.onClose}"]),t)},KolPopover=class{constructor(e){index.registerInstance(this,e),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.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,i,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===(i=this.state._on)||void 0===i?void 0:i.onClose)||void 0===s||s.call(i,e)}addListenersToBody(){var e;const t=dev_utils.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=dev_utils.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 index.h(index.Host,{key:"3c22e4b27c9b89a69dc3086eca4770f6ddebfe65",ref:this.catchHostAndTriggerElement,class:"kol-popover"},index.h("div",{key:"86d31be301d912b47e3dcb00896ccf750143e833",class:{popover:!0,show:this.state._visible},ref:this.catchPopoverElement,hidden:!this.state._show},index.h("div",{key:"a677d09531661bb23f851c08c73a985dc1ad3669",class:`arrow ${this.state._align}`,ref:this.catchArrowElement}),index.h("slot",{key:"7f64335d0bc5e99f1900912e197680693d75ff51"})))}validateAlign(e){align.validateAlign(this,e)}validateOn(e){validatePopoverCallbacks(this,e)}validateShow(e){show.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"]}}};exports.kol_popover_wc=KolPopover;