import*as Common from"../common/common.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";import{ElementsTreeElement}from"./ElementsTreeElement.js";import{ElementsTreeOutline}from"./ElementsTreeOutline.js";export class ElementsTreeElementHighlighter{constructor(e){this._throttler=new Common.Throttler.Throttler(100),this._treeOutline=e,this._treeOutline.addEventListener(UI.TreeOutline.Events.ElementExpanded,this._clearState,this),this._treeOutline.addEventListener(UI.TreeOutline.Events.ElementCollapsed,this._clearState,this),this._treeOutline.addEventListener(ElementsTreeOutline.Events.SelectedNodeChanged,this._clearState,this),SDK.SDKModel.TargetManager.instance().addModelListener(SDK.OverlayModel.OverlayModel,SDK.OverlayModel.Events.HighlightNodeRequested,this._highlightNode,this),SDK.SDKModel.TargetManager.instance().addModelListener(SDK.OverlayModel.OverlayModel,SDK.OverlayModel.Events.InspectModeWillBeToggled,this._clearState,this),this._currentHighlightedElement=null,this._alreadyExpandedParentElement=null,this._pendingHighlightNode=null,this._isModifyingTreeOutline=!1}_highlightNode(e){if(!Common.Settings.Settings.instance().moduleSetting("highlightNodeOnHoverInOverlay").get())return;const t=e.data;this._throttler.schedule((async()=>{this._highlightNodeInternal(this._pendingHighlightNode),this._pendingHighlightNode=null})),this._pendingHighlightNode=this._treeOutline===ElementsTreeOutline.forDOMModel(t.domModel())?t:null}_highlightNodeInternal(e){this._isModifyingTreeOutline=!0;let t=null;if(this._currentHighlightedElement){let e=this._currentHighlightedElement;for(;e&&e!==this._alreadyExpandedParentElement;){e.expanded&&e.collapse();const t=e.parent;e=t instanceof ElementsTreeElement?t:null}}if(this._currentHighlightedElement=null,this._alreadyExpandedParentElement=null,e){let n=e;const l=this._treeOutline.treeElementByNode,i=e=>{const t=l.get(e);return!t||!t.expanded};for(;n&&i(n);)n=n.parentNode;this._alreadyExpandedParentElement=n?l.get(n):this._treeOutline.rootElement(),t=this._treeOutline.createTreeElementFor(e)}this._currentHighlightedElement=t,this._treeOutline.setHoverEffect(t),t&&t.reveal(!0),this._isModifyingTreeOutline=!1}_clearState(){this._isModifyingTreeOutline||(this._currentHighlightedElement=null,this._alreadyExpandedParentElement=null,this._pendingHighlightNode=null)}}