import{GlassPane,MarginBehavior,SizeBehavior}from"./GlassPane.js";export class PopoverHelper{constructor(e,o){this._disableOnClick=!1,this._hasPadding=!1,this._getRequest=o,this._scheduledRequest=null,this._hidePopoverCallback=null,this._container=e,this._showTimeout=0,this._hideTimeout=0,this._hidePopoverTimer=null,this._showPopoverTimer=null,this._boundMouseDown=this._mouseDown.bind(this),this._boundMouseMove=this._mouseMove.bind(this),this._boundMouseOut=this._mouseOut.bind(this),this._container.addEventListener("mousedown",this._boundMouseDown,!1),this._container.addEventListener("mousemove",this._boundMouseMove,!1),this._container.addEventListener("mouseout",this._boundMouseOut,!1),this.setTimeout(1e3)}setTimeout(e,o){this._showTimeout=e,this._hideTimeout="number"===typeof o?o:e/2}setHasPadding(e){this._hasPadding=e}setDisableOnClick(e){this._disableOnClick=e}_eventInScheduledContent(e){const o=e;return!!this._scheduledRequest&&this._scheduledRequest.box.contains(o.clientX,o.clientY)}_mouseDown(e){this._disableOnClick?this.hidePopover():this._eventInScheduledContent(e)||(this._startHidePopoverTimer(0),this._stopShowPopoverTimer(),this._startShowPopoverTimer(e,0))}_mouseMove(e){const o=e;this._eventInScheduledContent(o)||(this._startHidePopoverTimer(this._hideTimeout),this._stopShowPopoverTimer(),o.which&&this._disableOnClick||this._startShowPopoverTimer(o,this.isPopoverVisible()?.6*this._showTimeout:this._showTimeout))}_popoverMouseMove(e){this._stopHidePopoverTimer()}_popoverMouseOut(e,o){const i=o;if(!e.isShowing())return;const t=i.relatedTarget;t&&!t.isSelfOrDescendant(e.contentElement)&&this._startHidePopoverTimer(this._hideTimeout)}_mouseOut(e){this.isPopoverVisible()&&(this._eventInScheduledContent(e)||this._startHidePopoverTimer(this._hideTimeout))}_startHidePopoverTimer(e){this._hidePopoverCallback&&!this._hidePopoverTimer&&(this._hidePopoverTimer=window.setTimeout((()=>{this._hidePopover(),this._hidePopoverTimer=null}),e))}_startShowPopoverTimer(e,o){this._scheduledRequest=this._getRequest.call(null,e),this._scheduledRequest&&(this._showPopoverTimer=window.setTimeout((()=>{this._showPopoverTimer=null,this._stopHidePopoverTimer(),this._hidePopover();const o=e.target.ownerDocument;this._showPopover(o)}),o))}_stopShowPopoverTimer(){this._showPopoverTimer&&(clearTimeout(this._showPopoverTimer),this._showPopoverTimer=null)}isPopoverVisible(){return Boolean(this._hidePopoverCallback)}hidePopover(){this._stopShowPopoverTimer(),this._hidePopover()}_hidePopover(){this._hidePopoverCallback&&(this._hidePopoverCallback.call(null),this._hidePopoverCallback=null)}_showPopover(e){const o=new GlassPane;o.registerRequiredCSS("ui/popover.css",{enableLegacyPatching:!1}),o.setSizeBehavior(SizeBehavior.MeasureContent),o.setMarginBehavior(MarginBehavior.Arrow);const i=this._scheduledRequest;i&&i.show.call(null,o).then((t=>{t&&(this._scheduledRequest===i?(popoverHelperInstance&&(console.error("One popover is already visible"),popoverHelperInstance.hidePopover()),popoverHelperInstance=this,o.contentElement.classList.toggle("has-padding",this._hasPadding),o.contentElement.addEventListener("mousemove",this._popoverMouseMove.bind(this),!0),o.contentElement.addEventListener("mouseout",this._popoverMouseOut.bind(this,o),!0),o.setContentAnchorBox(i.box),o.show(e),this._hidePopoverCallback=()=>{i.hide&&i.hide.call(null),o.hide(),popoverHelperInstance=null}):i.hide&&i.hide.call(null))}))}_stopHidePopoverTimer(){this._hidePopoverTimer&&(clearTimeout(this._hidePopoverTimer),this._hidePopoverTimer=null,this._stopShowPopoverTimer())}dispose(){this._container.removeEventListener("mousedown",this._boundMouseDown,!1),this._container.removeEventListener("mousemove",this._boundMouseMove,!1),this._container.removeEventListener("mouseout",this._boundMouseOut,!1)}}let popoverHelperInstance=null;export let PopoverRequest;