import*as Platform from"../platform/platform.js";import{GlassPane}from"./GlassPane.js";import{ShortcutRegistry}from"./ShortcutRegistry.js";import{createShadowRootWithCoreStyles}from"./utils/create-shadow-root-with-core-styles.js";import{Events as ZoomManagerEvents,ZoomManager}from"./ZoomManager.js";export class Tooltip{constructor(t){this.element=t.body.createChild("div"),this._shadowRoot=createShadowRootWithCoreStyles(this.element,{cssFile:"ui/tooltip.css",enableLegacyPatching:!0,delegatesFocus:void 0}),this._tooltipElement=this._shadowRoot.createChild("div","tooltip"),t.addEventListener("mousemove",this._mouseMove.bind(this),!0),t.addEventListener("mousedown",this._hide.bind(this,!0),!0),t.addEventListener("mouseleave",this._hide.bind(this,!1),!0),t.addEventListener("keydown",this._hide.bind(this,!0),!0),t[_symbol]=this,ZoomManager.instance().addEventListener(ZoomManagerEvents.ZoomChanged,this._reset,this),t.defaultView&&t.defaultView.addEventListener("resize",this._reset.bind(this),!1)}static installHandler(t){new Tooltip(t)}static install(t,e,o,i){if(!e)return void delete t[_symbol];let n;t[_symbol]={content:e,actionId:o,options:i||{}},t.addEventListener("focus",(e=>{const o=t.ownerDocument[_symbol];o&&(n=window.setTimeout((()=>{t.matches(":focus-visible")&&o._show(t,e),n=null}),Timing.OpeningDelay))})),t.addEventListener("blur",(()=>{n&&(clearTimeout(n),n=null);const e=t.ownerDocument[_symbol];e&&e._hide()}))}static getContent(t){const e=t[_symbol];return e?e.content:""}static addNativeOverrideContainer(t){_nativeOverrideContainer.push(t)}_mouseMove(t){const e=t,o=e.composedPath();if(o&&0===e.buttons&&(0!==e.movementX||0!==e.movementY)){this._anchorElement&&-1===o.indexOf(this._anchorElement)&&this._hide(!1);for(const t of o){if(t===this._anchorElement)return;if(t instanceof HTMLElement&&null!==t.offsetParent&&Tooltip.getContent(t))return void this._show(t,e)}}}_reposition(t,e){this._tooltipElement.positionAt(0,0);const o=GlassPane.container(t.ownerDocument).boxInWindow(this.element.window()),i=this._anchorElement.boxInWindow(this.element.window()),n=this._tooltipElement&&(this._tooltipElement.textContent||"").match("\\s");this._tooltipElement.classList.toggle("tooltip-breakword",!n),this._tooltipElement.style.maxWidth=o.width-4+"px",this._tooltipElement.style.maxHeight="";const s=this._tooltipElement.offsetWidth,l=this._tooltipElement.offsetHeight,h=this._anchorTooltipAtElement()||void 0===e.x;let a,r=h?i.x:e.x+10;if(r=Platform.NumberUtilities.clamp(r,o.x+2,o.x+o.width-s-2),h){a=i.y+2+i.height+l<o.y+o.height?i.y+i.height+2:i.y-l-2}else a=e.y+10+l<o.y+o.height?e.y+10:e.y-l-1;this._tooltipElement.positionAt(r,a)}_anchorTooltipAtElement(){if(!this._anchorElement)throw new Error("No _anchorElement set");const t=this._anchorElement[_symbol];return void 0!==t.options.anchorTooltipAtElement?t.options.anchorTooltipAtElement:"BUTTON"===this._anchorElement.nodeName||"LABEL"===this._anchorElement.nodeName}_show(t,e){const o=t[_symbol];if(this._anchorElement=t,this._tooltipElement.removeChildren(),this._shouldUseNativeTooltips())return void Tooltip.install(this._anchorElement,o.content);if("string"===typeof o.content?this._tooltipElement.setTextContentTruncatedIfNeeded(o.content):this._tooltipElement.appendChild(o.content),o.actionId){const t=ShortcutRegistry.instance().shortcutsForAction(o.actionId);for(const e of t){this._tooltipElement.createChild("div","tooltip-shortcut").textContent=e.title()}}const i=Date.now(),n=void 0!==this._tooltipLastClosed&&i-this._tooltipLastClosed<Timing.InstantThreshold;this._tooltipElement.classList.toggle("instant",n),this._tooltipLastOpened=n?i:i+Timing.OpeningDelay,this._reposition(t,e),this._tooltipElement.classList.add("shown")}_shouldUseNativeTooltips(){for(const t of _nativeOverrideContainer)if(this._anchorElement&&this._anchorElement.isSelfOrDescendant(t))return!0;return!1}_hide(t){delete this._anchorElement,this._tooltipElement.classList.remove("shown"),this._tooltipLastOpened&&Date.now()>this._tooltipLastOpened&&(this._tooltipLastClosed=Date.now()),t&&delete this._tooltipLastClosed}_reset(){this._hide(!0),this._tooltipElement.positionAt(0,0),this._tooltipElement.style.maxWidth="0",this._tooltipElement.style.maxHeight="0"}}export let TooltipOptions;const Timing={InstantThreshold:300,OpeningDelay:600},_symbol=Symbol("Tooltip");export const TooltipSymbol=_symbol;const _nativeOverrideContainer=[];