import{RuntimeModel}from"./RuntimeModel.js";import{Capability,SDKModel}from"./SDKModel.js";export class HeapProfilerModel extends SDKModel{constructor(e){super(e),e.registerHeapProfilerDispatcher(new HeapProfilerDispatcher(this)),this._enabled=!1,this._heapProfilerAgent=e.heapProfilerAgent(),this._memoryAgent=e.memoryAgent(),this._runtimeModel=e.model(RuntimeModel),this._samplingProfilerDepth=0}debuggerModel(){return this._runtimeModel.debuggerModel()}runtimeModel(){return this._runtimeModel}async enable(){this._enabled||(this._enabled=!0,await this._heapProfilerAgent.invoke_enable())}async startSampling(e){if(this._samplingProfilerDepth++)return!1;const t=await this._heapProfilerAgent.invoke_startSampling({samplingInterval:e||16384});return Boolean(t.getError())}async stopSampling(){if(!this._samplingProfilerDepth)throw new Error("Sampling profiler is not running.");if(--this._samplingProfilerDepth)return this.getSamplingProfile();const e=await this._heapProfilerAgent.invoke_stopSampling();return e.getError()?null:e.profile}async getSamplingProfile(){const e=await this._heapProfilerAgent.invoke_getSamplingProfile();return e.getError()?null:e.profile}async collectGarbage(){const e=await this._heapProfilerAgent.invoke_collectGarbage();return Boolean(e.getError())}async snapshotObjectIdForObjectId(e){const t=await this._heapProfilerAgent.invoke_getHeapObjectId({objectId:e});return t.getError()?null:t.heapSnapshotObjectId}async objectForSnapshotObjectId(e,t){const r=await this._heapProfilerAgent.invoke_getObjectByHeapObjectId({objectId:e,objectGroup:t});return r.getError()?null:this._runtimeModel.createRemoteObject(r.result)}async addInspectedHeapObject(e){const t=await this._heapProfilerAgent.invoke_addInspectedHeapObject({heapObjectId:e});return Boolean(t.getError())}async takeHeapSnapshot(e,t){await this._heapProfilerAgent.invoke_takeHeapSnapshot({reportProgress:e,treatGlobalObjectsAsRoots:t})}async startTrackingHeapObjects(e){const t=await this._heapProfilerAgent.invoke_startTrackingHeapObjects({trackAllocations:e});return Boolean(t.getError())}async stopTrackingHeapObjects(e){const t=await this._heapProfilerAgent.invoke_stopTrackingHeapObjects({reportProgress:e});return Boolean(t.getError())}heapStatsUpdate(e){this.dispatchEventToListeners(Events.HeapStatsUpdate,e)}lastSeenObjectId(e,t){this.dispatchEventToListeners(Events.LastSeenObjectId,{lastSeenObjectId:e,timestamp:t})}addHeapSnapshotChunk(e){this.dispatchEventToListeners(Events.AddHeapSnapshotChunk,e)}reportHeapSnapshotProgress(e,t,r){this.dispatchEventToListeners(Events.ReportHeapSnapshotProgress,{done:e,total:t,finished:r})}resetProfiles(){this.dispatchEventToListeners(Events.ResetProfiles,this)}}export const Events={HeapStatsUpdate:Symbol("HeapStatsUpdate"),LastSeenObjectId:Symbol("LastSeenObjectId"),AddHeapSnapshotChunk:Symbol("AddHeapSnapshotChunk"),ReportHeapSnapshotProgress:Symbol("ReportHeapSnapshotProgress"),ResetProfiles:Symbol("ResetProfiles")};export let NativeProfilerCallFrame;export let CommonHeapProfileNode;export let CommonHeapProfile;class HeapProfilerDispatcher{constructor(e){this._heapProfilerModel=e}heapStatsUpdate({statsUpdate:e}){this._heapProfilerModel.heapStatsUpdate(e)}lastSeenObjectId({lastSeenObjectId:e,timestamp:t}){this._heapProfilerModel.lastSeenObjectId(e,t)}addHeapSnapshotChunk({chunk:e}){this._heapProfilerModel.addHeapSnapshotChunk(e)}reportHeapSnapshotProgress({done:e,total:t,finished:r}){this._heapProfilerModel.reportHeapSnapshotProgress(e,t,r)}resetProfiles(){this._heapProfilerModel.resetProfiles()}}let NodeForConstruction;SDKModel.register(HeapProfilerModel,Capability.JS,!1);