import*as Common from"../common/common.js";import*as SDK from"../sdk/sdk.js";import{SourceFrameIssuesManager}from"./SourceFrameIssuesManager.js";let issuesManagerInstance=null;export class IssuesManager extends Common.ObjectWrapper.ObjectWrapper{constructor(){super(),this._eventListeners=new WeakMap,SDK.SDKModel.TargetManager.instance().observeModels(SDK.IssuesModel.IssuesModel,this),this._issues=new Map,this._filteredIssues=new Map,this._hasSeenTopFrameNavigated=!1,SDK.FrameManager.FrameManager.instance().addEventListener(SDK.FrameManager.Events.TopFrameNavigated,this._onTopFrameNavigated,this),SDK.FrameManager.FrameManager.instance().addEventListener(SDK.FrameManager.Events.FrameAddedToTarget,this._onFrameAddedToTarget,this),this._showThirdPartySettingsChangeListener=null,this._sourceFrameIssuesManager=new SourceFrameIssuesManager(this)}static instance({forceNew:e}={forceNew:!1}){return issuesManagerInstance&&!e||(issuesManagerInstance=new IssuesManager),issuesManagerInstance}reloadForAccurateInformationRequired(){return!this._hasSeenTopFrameNavigated}_onTopFrameNavigated(e){const{frame:s}=e.data,t=new Map;for(const[a,r]of this._issues.entries())r.isAssociatedWithRequestId(s.loaderId)&&t.set(a,r);this._issues=t,this._hasSeenTopFrameNavigated=!0,this._updateFilteredIssues()}_onFrameAddedToTarget(e){const{frame:s}=e.data;s.isTopFrame()&&this._updateFilteredIssues()}modelAdded(e){const s=e.addEventListener(SDK.IssuesModel.Events.IssueAdded,this._issueAdded,this);this._eventListeners.set(e,s)}modelRemoved(e){const s=this._eventListeners.get(e);s&&Common.EventTarget.EventTarget.removeEventListeners([s])}_issueAdded(e){const{issuesModel:s,issue:t}=e.data;if(!t.getDescription())return;const a=t.primaryKey();this._issues.has(a)||(this._issues.set(a,t),this._issueFilter(t)&&(this._filteredIssues.set(a,t),this.dispatchEventToListeners(Events.IssueAdded,{issuesModel:s,issue:t})),this.dispatchEventToListeners(Events.IssuesCountUpdated))}issues(){return this._filteredIssues.values()}numberOfIssues(){return this._filteredIssues.size}numberOfAllStoredIssues(){return this._issues.size}_issueFilter(e){if(!this._showThirdPartySettingsChangeListener){const e=SDK.Issue.getShowThirdPartyIssuesSetting();this._showThirdPartySettingsChangeListener=e.addChangeListener((()=>{this._updateFilteredIssues()}))}return SDK.Issue.getShowThirdPartyIssuesSetting().get()||!e.isCausedByThirdParty()}_updateFilteredIssues(){this._filteredIssues.clear();for(const[e,s]of this._issues)this._issueFilter(s)&&this._filteredIssues.set(e,s);this.dispatchEventToListeners(Events.FullUpdateRequired),this.dispatchEventToListeners(Events.IssuesCountUpdated)}}export var Events;!function(e){e.IssuesCountUpdated="IssuesCountUpdated",e.IssueAdded="IssueAdded",e.FullUpdateRequired="FullUpdateRequired"}(Events||(Events={})),globalThis.addIssueForTest=e=>{const s=SDK.SDKModel.TargetManager.instance().mainTarget()?.model(SDK.IssuesModel.IssuesModel);s?.issueAdded({issue:e})};