import*as Common from"../common/common.js";import*as Root from"../root/root.js";import*as SDK from"../sdk/sdk.js";let contrastCheckTriggerInstance=null;export class ContrastCheckTrigger extends Common.ObjectWrapper.ObjectWrapper{constructor(){super(),this.pageLoadListeners=new WeakMap,this.frameAddedListeners=new WeakMap,SDK.SDKModel.TargetManager.instance().observeModels(SDK.ResourceTreeModel.ResourceTreeModel,this)}static instance({forceNew:e}={forceNew:!1}){return contrastCheckTriggerInstance&&!e||(contrastCheckTriggerInstance=new ContrastCheckTrigger),contrastCheckTriggerInstance}async modelAdded(e){this.pageLoadListeners.set(e,e.addEventListener(SDK.ResourceTreeModel.Events.Load,this.pageLoaded,this)),this.frameAddedListeners.set(e,e.addEventListener(SDK.ResourceTreeModel.Events.FrameAdded,this.frameAdded,this))}modelRemoved(e){const t=this.pageLoadListeners.get(e);t&&Common.EventTarget.EventTarget.removeEventListeners([t]);const r=this.frameAddedListeners.get(e);r&&Common.EventTarget.EventTarget.removeEventListeners([r])}checkContrast(e){Root.Runtime.experiments.isEnabled("contrastIssues")&&e.target().auditsAgent().invoke_checkContrast()}pageLoaded(e){const{resourceTreeModel:t}=e.data;this.checkContrast(t)}async frameAdded(e){if(!Root.Runtime.experiments.isEnabled("contrastIssues"))return;const t=e.data;if(!t.isMainFrame())return;const r=await t.resourceTreeModel().target().runtimeAgent().invoke_evaluate({expression:"document.readyState",returnByValue:!0});r.result&&"complete"===r.result.value&&this.checkContrast(t.resourceTreeModel())}}