/**
 * @license
 * Copyright 2025 ReverseCraft
 * SPDX-License-Identifier: Apache-2.0
 */
import{FakeIssuesManager as e}from"../utils/devtools-utils.js";import{logger as s}from"../utils/logger.js";import{DevTools as t}from"../third-party/index.js";import{enableNetworkInitiatorTracking as r,clearNetworkInitiators as i}from"../utils/cdp.js";export const stableIdSymbol=Symbol("stableIdSymbol");export class PageCollector{#e=new Set;#s;#t=new WeakMap;#r=3;storage=new WeakMap;constructor(e,s){this.#e.add(e),this.#s=s}async init(e){for(const s of e)this.addPage(s);for(const e of this.#e)e.on("targetcreated",this.#i),e.on("targetdestroyed",this.#a)}addBrowser(e){this.#e.has(e)||(this.#e.add(e),e.on("targetcreated",this.#i),e.on("targetdestroyed",this.#a))}dispose(){for(const e of this.#e)e.off("targetcreated",this.#i),e.off("targetdestroyed",this.#a)}#i=async e=>{try{const s=await e.page();if(!s)return;this.addPage(s)}catch(e){s("Error getting a page for a target onTargetCreated",e)}};#a=async e=>{try{const s=await e.page();if(!s)return;this.cleanupPageDestroyed(s)}catch(e){s("Error getting a page for a target onTargetDestroyed",e)}};addPage(e){this.#o(e)}#o(e){if(this.storage.has(e))return;const s=function(){let e=1;return()=>(e===Number.MAX_SAFE_INTEGER&&(e=0),e++)}();this.storage.set(e,[[]]);const t=this.#s(t=>{const r=t;r[stableIdSymbol]=s();(this.storage.get(e)??[[]])[0].push(r)});t.framenavigated=s=>{s===e.mainFrame()&&this.splitAfterNavigation(e)};for(const[s,r]of Object.entries(t))e.on(s,r);this.#t.set(e,t)}splitAfterNavigation(e){const s=this.storage.get(e);s&&(s.unshift([]),s.splice(this.#r))}cleanupPageDestroyed(e){const s=this.#t.get(e);if(s)for(const[t,r]of Object.entries(s))e.off(t,r);this.storage.delete(e)}getData(e,s){const t=this.storage.get(e);if(!t)return[];if(!s)return t[0];const r=[];for(let e=this.#r;e>=0;e--)t[e]&&r.push(...t[e]);return r}getIdForResource(e){return e[stableIdSymbol]??-1}getById(e,s){if(!this.storage.get(e))throw new Error("No requests found for selected page");const t=this.find(e,e=>e[stableIdSymbol]===s);if(t)return t;throw new Error("Request not found for selected page")}find(e,s){const t=this.storage.get(e);if(t)for(const e of t){const t=e.find(s);if(t)return t}}}export class ConsoleCollector extends PageCollector{#n=new WeakMap;addPage(e){if(super.addPage(e),!this.#n.has(e)){const s=new a(e);this.#n.set(e,s),s.subscribe()}}cleanupPageDestroyed(e){super.cleanupPageDestroyed(e),this.#n.get(e)?.unsubscribe(),this.#n.delete(e)}}class a{#g=new e;#d=new t.IssueAggregator(this.#g);#u=new Set;#c=new Set;#h;#l;constructor(e){this.#h=e,this.#l=this.#h._client()}#f(){this.#g=new e,this.#d&&this.#d.removeEventListener("AggregatedIssueUpdated",this.#p),this.#d=new t.IssueAggregator(this.#g),this.#d.addEventListener("AggregatedIssueUpdated",this.#p)}async subscribe(){this.#f(),this.#h.on("framenavigated",this.#b),this.#l.on("Audits.issueAdded",this.#m);try{await this.#l.send("Audits.enable")}catch(e){s("Error subscribing to issues",e)}}unsubscribe(){this.#u.clear(),this.#c.clear(),this.#h.off("framenavigated",this.#b),this.#l.off("Audits.issueAdded",this.#m),this.#d&&this.#d.removeEventListener("AggregatedIssueUpdated",this.#p),this.#l.send("Audits.disable").catch(()=>{})}#p=e=>{this.#c.has(e.data)||(this.#c.add(e.data),this.#h.emit("issue",e.data))};#b=e=>{e===e.page().mainFrame()&&(this.#u.clear(),this.#c.clear(),this.#f())};#m=e=>{try{const r=e.issue,i=t.createIssuesFromProtocolIssue(null,r)[0];if(!i)return void s("No issue mapping for for the issue: ",r.code);const a=i.primaryKey();if(this.#u.has(a))return;this.#u.add(a),this.#g.dispatchEventToListeners("IssueAdded",{issue:i,issuesModel:null})}catch(e){s("Error creating a new issue",e)}}}export class NetworkCollector extends PageCollector{constructor(e,s=e=>({request:s=>{e(s)}})){super(e,s)}addPage(e){super.addPage(e),r(e).catch(e=>{s("Error enabling network initiator tracking",e)})}splitAfterNavigation(e){i(e);const s=this.storage.get(e)??[];if(!s)return;const t=s[0],r=t.findLastIndex(s=>s.frame()===e.mainFrame()&&s.isNavigationRequest());if(-1!==r){let e=r;const i=t[r].redirectChain();if(i.length>0){const s=i[0],r=t.findIndex(e=>e===s);-1!==r&&r<e&&(e=r)}const a=t.splice(e);s.unshift(a)}else s.unshift([])}}