import*as Common from"../common/common.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";export class ComputedStyleModel extends Common.ObjectWrapper.ObjectWrapper{constructor(){super(),this._node=UI.Context.Context.instance().flavor(SDK.DOMModel.DOMNode),this._cssModel=null,this._eventListeners=[],UI.Context.Context.instance().addFlavorChangeListener(SDK.DOMModel.DOMNode,this._onNodeChanged,this)}node(){return this._node}cssModel(){return this._cssModel&&this._cssModel.isEnabled()?this._cssModel:null}_onNodeChanged(e){this._node=e.data,this._updateModel(this._node?this._node.domModel().cssModel():null),this._onComputedStyleChanged(null)}_updateModel(e){if(this._cssModel===e)return;Common.EventTarget.EventTarget.removeEventListeners(this._eventListeners),this._cssModel=e;const t=e?e.domModel():null,s=e?e.target().model(SDK.ResourceTreeModel.ResourceTreeModel):null;e&&t&&s&&(this._eventListeners=[e.addEventListener(SDK.CSSModel.Events.StyleSheetAdded,this._onComputedStyleChanged,this),e.addEventListener(SDK.CSSModel.Events.StyleSheetRemoved,this._onComputedStyleChanged,this),e.addEventListener(SDK.CSSModel.Events.StyleSheetChanged,this._onComputedStyleChanged,this),e.addEventListener(SDK.CSSModel.Events.FontsUpdated,this._onComputedStyleChanged,this),e.addEventListener(SDK.CSSModel.Events.MediaQueryResultChanged,this._onComputedStyleChanged,this),e.addEventListener(SDK.CSSModel.Events.PseudoStateForced,this._onComputedStyleChanged,this),e.addEventListener(SDK.CSSModel.Events.ModelWasEnabled,this._onComputedStyleChanged,this),t.addEventListener(SDK.DOMModel.Events.DOMMutated,this._onDOMModelChanged,this),s.addEventListener(SDK.ResourceTreeModel.Events.FrameResized,this._onFrameResized,this)])}_onComputedStyleChanged(e){delete this._computedStylePromise,this.dispatchEventToListeners(Events.ComputedStyleChanged,e?e.data:null)}_onDOMModelChanged(e){const t=e.data;this._node&&(this._node===t||t.parentNode===this._node.parentNode||t.isAncestor(this._node))&&this._onComputedStyleChanged(null)}_onFrameResized(e){this._frameResizedTimer&&clearTimeout(this._frameResizedTimer),this._frameResizedTimer=setTimeout(function(){this._onComputedStyleChanged(null),delete this._frameResizedTimer}.bind(this),100)}_elementNode(){const e=this.node();return e?e.enclosingElementOrSelf():null}async fetchComputedStyle(){const e=this._elementNode(),t=this.cssModel();if(!e||!t)return null;const s=e.id;return s?(this._computedStylePromise||(this._computedStylePromise=t.computedStylePromise(s).then(function(e,t){return e===this._elementNode()&&t?new ComputedStyle(e,t):null}.bind(this,e))),this._computedStylePromise):null}}export const Events={ComputedStyleChanged:Symbol("ComputedStyleChanged")};export class ComputedStyle{constructor(e,t){this.node=e,this.computedStyle=t}}