/**
 * @license
 * Copyright 2025 ReverseCraft
 * SPDX-License-Identifier: Apache-2.0
 */
import{clearScriptCache as t}from"./smart-breakpoint-utils.js";const e=new WeakMap;export async function getCdpSession(t){let n=e.get(t);return n||(n=await t.createCDPSession(),e.set(t,n)),n}export async function disposeCdpSession(n){const r=e.get(n);r&&(t(r),r.detach().catch(()=>{}),e.delete(n))}export function hasCdpSession(t){return e.has(t)}export function formatStackTrace(t,e=""){const n=[];t.description&&n.push(`${e}${t.description}`);for(const r of t.callFrames){const t=r.functionName||"(anonymous)",o=`${r.url}:${r.lineNumber+1}:${r.columnNumber}`;n.push(`${e}  at ${t} (${o})`)}return t.parent&&(n.push(`${e}--- async ---`),n.push(...formatStackTrace(t.parent,e))),n}export function formatInitiator(t){const e=[];if(e.push(`Type: ${t.type}`),t.url){const n=void 0!==t.lineNumber?`${t.url}:${t.lineNumber+1}${void 0!==t.columnNumber?`:${t.columnNumber}`:""}`:t.url;e.push(`URL: ${n}`)}return t.stack&&(e.push(""),e.push("Call Stack:"),e.push(...formatStackTrace(t.stack,"  "))),e}const n=new WeakMap,r=new WeakSet;export async function enableNetworkInitiatorTracking(t){if(r.has(t))return;const e=await getCdpSession(t);n.set(t,new Map),e.on("Network.requestWillBeSent",e=>{if(e.initiator){const r=n.get(t);r&&r.set(e.requestId,e.initiator)}}),await e.send("Network.enable"),r.add(t)}export function getNetworkInitiator(t,e){const r=n.get(t);return r?.get(e)}export function clearNetworkInitiators(t){const e=n.get(t);e&&e.clear()}export function isNetworkTrackingEnabled(t){return r.has(t)}