import*as BrowserSDK from"../browser_sdk/browser_sdk.js";import*as Common from"../common/common.js";import*as SDK from"../sdk/sdk.js";export class AggregatedIssue extends SDK.Issue.Issue{constructor(e){super(e),this.affectedCookies=new Map,this.affectedRequests=new Map,this.heavyAdIssueDetails=new Map,this.blockedByResponseDetails=new Map,this.corsIssues=new Set,this.cspIssues=new Set,this.lowContrastIssues=new Set,this.mixedContentIssues=new Set,this.sharedArrayBufferIssues=new Set,this.trustedWebActivityIssues=new Set,this.representative=null,this.aggregatedIssuesCount=0}primaryKey(){throw new Error("This should never be called")}getBlockedByResponseDetails(){return this.blockedByResponseDetails.values()}cookies(){return Array.from(this.affectedCookies.values()).map((e=>e.cookie))}cookiesWithRequestIndicator(){return this.affectedCookies.values()}heavyAds(){return this.heavyAdIssueDetails.values()}getMixedContentIssues(){return this.mixedContentIssues.values()}getTrustedWebActivityIssues(){return this.trustedWebActivityIssues}getCorsIssues(){return this.corsIssues}getCspIssues(){return this.cspIssues}getLowContrastIssues(){return this.lowContrastIssues}requests(){return this.affectedRequests.values()}getSharedArrayBufferIssues(){return this.sharedArrayBufferIssues}getDescription(){return this.representative?this.representative.getDescription():null}getCategory(){return this.representative?this.representative.getCategory():SDK.Issue.IssueCategory.Other}getAggregatedIssuesCount(){return this.aggregatedIssuesCount}keyForCookie(e){const{domain:s,path:t,name:r}=e;return`${s};${t};${r}`}addInstance(e){this.aggregatedIssuesCount++,this.representative||(this.representative=e);let s=!1;for(const t of e.requests())s=!0,this.affectedRequests.has(t.requestId)||this.affectedRequests.set(t.requestId,t);for(const t of e.cookies()){const e=this.keyForCookie(t);this.affectedCookies.has(e)||this.affectedCookies.set(e,{cookie:t,hasRequest:s})}e instanceof SDK.MixedContentIssue.MixedContentIssue&&this.mixedContentIssues.add(e);for(const t of e.heavyAds()){const e=JSON.stringify(t);this.heavyAdIssueDetails.set(e,t)}for(const t of e.getBlockedByResponseDetails()){const e=JSON.stringify(t,["parentFrame","blockedFrame","requestId","frameId","reason","request"]);this.blockedByResponseDetails.set(e,t)}e instanceof SDK.TrustedWebActivityIssue.TrustedWebActivityIssue&&this.trustedWebActivityIssues.add(e),e instanceof SDK.ContentSecurityPolicyIssue.ContentSecurityPolicyIssue&&this.cspIssues.add(e),e instanceof SDK.SharedArrayBufferIssue.SharedArrayBufferIssue&&this.sharedArrayBufferIssues.add(e),e instanceof SDK.LowTextContrastIssue.LowTextContrastIssue&&this.lowContrastIssues.add(e),e instanceof SDK.CorsIssue.CorsIssue&&this.corsIssues.add(e)}}export class IssueAggregator extends Common.ObjectWrapper.ObjectWrapper{constructor(e){super(),this.aggregatedIssuesByCode=new Map,this.issuesManager=e,this.issuesManager.addEventListener(BrowserSDK.IssuesManager.Events.IssueAdded,this.onIssueAdded,this),this.issuesManager.addEventListener(BrowserSDK.IssuesManager.Events.FullUpdateRequired,this.onFullUpdateRequired,this);for(const s of this.issuesManager.issues())this.aggregateIssue(s)}onIssueAdded(e){const{issue:s}=e.data;this.aggregateIssue(s)}onFullUpdateRequired(){this.aggregatedIssuesByCode.clear();for(const e of this.issuesManager.issues())this.aggregateIssue(e);this.dispatchEventToListeners("FullUpdateRequired")}aggregateIssue(e){let s=this.aggregatedIssuesByCode.get(e.code());return s||(s=new AggregatedIssue(e.code()),this.aggregatedIssuesByCode.set(e.code(),s)),s.addInstance(e),this.dispatchEventToListeners("AggregatedIssueUpdated",s),s}aggregatedIssues(){return this.aggregatedIssuesByCode.values()}numberOfAggregatedIssues(){return this.aggregatedIssuesByCode.size}}