import{SimpleNotification}from"./notify.mjs";import{ElementBuilder}from"../base/builder.mjs";import{isEmpty,stripHTML}from"../base/helpers.mjs";const E=new ElementBuilder;class TooltipHelper{static offsetX=10;static offsetY=10;static activationDelay=150;static deactivationDelay=150;static containerClassName="tooltip-container";constructor(){this.tooltipContainer=E.div(),this.tooltipContainer.class(this.constructor.containerClassName),window.addEventListener("mousemove",(t=>this.onMouseMove(t)),!0),window.addEventListener("mouseout",(t=>this.onMouseOut(t)),!0),window.addEventListener("touch",(t=>this.onTouch(t)),!0),window.addEventListener("contextmenu",(t=>this.onContextmenu(t)),!0),document.body.appendChild(this.tooltipContainer.render())}onMouseOut(t){null===t.toElement&&this.deactivate()}onMouseMove(t){let i=t.target;for(;!i.hasAttribute("data-tooltip")&&(i=i.parentElement,!isEmpty(i)););isEmpty(i)?this.deactivate():(this.setTooltip(i.getAttribute("data-tooltip")),this.x=t.clientX,this.y=t.clientY,this.positionElement())}onTouch(t){let i=t.target;for(;!i.hasAttribute("data-tooltip")&&(i=i.parentElement,!isEmpty(i)););isEmpty(i)?this.deactivate():(this.setTooltip(i.getAttribute("data-tooltip")),this.x=t.touches[0].clientX,this.y=t.touches[0].clientY,this.positionElement())}onContextmenu(t){if(!t.ctrlKey||!navigator.clipboard)return;let i=t.target;for(;!i.hasAttribute("data-tooltip")&&(i=i.parentElement,!isEmpty(i)););isEmpty(i)?this.deactivate():(navigator.clipboard.writeText(stripHTML(i.getAttribute("data-tooltip"))),SimpleNotification.notify("Copied to Clipboard",1e3),t.preventDefault(),t.stopPropagation())}setTooltip(t){-1!==t.indexOf("{")&&-1!==t.indexOf("}")?this.tooltipContainer.addClass("json"):this.tooltipContainer.removeClass("json"),navigator.clipboard&&(t+="<br /><em class='note'>Ctrl+Right-Click to Copy Text</em>"),this.tooltipContainer.content(t),this.tooltipContainer.addClass("active")}positionElement(){this.x>window.innerWidth/2?(this.tooltipContainer.css("left",null),this.tooltipContainer.css("right",window.innerWidth-this.x+this.constructor.offsetX)):(this.tooltipContainer.css("right",null),this.tooltipContainer.css("left",this.x+this.constructor.offsetX)),this.y>window.innerHeight/2?(this.tooltipContainer.css("top",null),this.tooltipContainer.css("bottom",window.innerHeight-this.y+this.constructor.offsetY)):(this.tooltipContainer.css("bottom",null),this.tooltipContainer.css("top",this.y+this.constructor.offsetY))}deactivate(){this.tooltipContainer.removeClass("active")}}export{TooltipHelper};
