import{DOMWatcher}from"../base/watcher.mjs";import{ElementBuilder}from"../base/builder.mjs";const trackingConfiguration={name:"Tooltip tracker",childList:!0,subtree:!0,attributes:!0,debug:!1,containerClassName:"tooltip-container"},E=new ElementBuilder,offsetConfiguration={x:10,y:10},activationDelay=150,checkInterval=100;let enableTimer,checkIntervalTimer;class TooltipHelper extends DOMWatcher{constructor(t){void 0===t&&(t=trackingConfiguration),t.initialize=!1,super(t),this.filterFunction=this.tooltipFilter,this.initializeFunction=this.tooltipInitialize,this.updateFunction=this.tooltipUpdate,this.tooltipContainer=E.div().class(t.containerClassName||trackingConfiguration.containerClassName),document.body.appendChild(this.tooltipContainer.render()),this.initialize()}tooltipUpdate(t){t==this.tooltipNode&&this.tooltipContainer.content(t.getAttribute("data-tooltip"))}tooltipFilter(t){return t instanceof HTMLElement&&t.hasAttribute("data-tooltip")}tooltipInitialize(t){if(t.hasAttribute("data-tooltip-initialized"))return;let e=this,i=function(t,i){window.requestAnimationFrame((()=>{let o=e.tooltipContainer,n=window.innerWidth,a=window.innerHeight;t>n/2?(o.css("left",null),o.css("right",n-t+offsetConfiguration.x)):(o.css("right",null),o.css("left",t+offsetConfiguration.x)),i>a/2?(o.css("top",null),o.css("bottom",a-i+offsetConfiguration.y)):(o.css("bottom",null),o.css("top",i+offsetConfiguration.y))}))},o=function(){e.tooltipContainer.addClass("active"),checkIntervalTimer=setInterval((()=>{e.watchedNode.contains(t)||n()}),100)},n=function(){e.tooltipNode=null,e.tooltipContainer.removeClass("active"),clearInterval(checkIntervalTimer)},a=function(t){i(t.clientX,t.clientY)},r=function(t){clearTimeout(enableTimer),n(),window.removeEventListener("touchstart",r)};t.addEventListener("mouseenter",(function(n){e.tooltipNode=t,e.tooltipContainer.content(t.getAttribute("data-tooltip")),i(n.clientX,n.clientY),enableTimer=setTimeout(o,150),t.addEventListener("mousemove",a)})),t.addEventListener("mouseleave",(function(e){clearTimeout(enableTimer),n(),t.removeEventListener("mousemove",a)})),t.addEventListener("touchstart",(function(n){n.stopPropagation(),n.preventDefault(),e.tooltipNode=t,e.tooltipContainer.content(t.getAttribute("data-tooltip")),i(n.touches[0].clientX,n.touches[0].clientY),enableTimer=setTimeout(o,150),window.addEventListener("touchstart",r)})),t.setAttribute("data-tooltip-initialized",(new Date).getTime())}}export{TooltipHelper};
