import*as i18n from"../i18n/i18n.js";import*as Root from"../root/root.js";import{DebuggerModel,Location}from"./DebuggerModel.js";import{Capability,SDKModel}from"./SDKModel.js";const UIStrings={profileD:"Profile {PH1}"},str_=i18n.i18n.registerUIStrings("sdk/CPUProfilerModel.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class CPUProfilerModel extends SDKModel{constructor(e){super(e),this._isRecording=!1,this._nextAnonymousConsoleProfileNumber=1,this._anonymousConsoleProfileIdToTitle=new Map,this._profilerAgent=e.profilerAgent(),this._preciseCoverageDeltaUpdateCallback=null,e.registerProfilerDispatcher(this),this._profilerAgent.invoke_enable(),this._debuggerModel=e.model(DebuggerModel)}runtimeModel(){return this._debuggerModel.runtimeModel()}debuggerModel(){return this._debuggerModel}consoleProfileStarted({id:e,location:o,title:i}){i||(i=i18nString(UIStrings.profileD,{PH1:this._nextAnonymousConsoleProfileNumber++}),this._anonymousConsoleProfileIdToTitle.set(e,i)),this._dispatchProfileEvent(Events.ConsoleProfileStarted,e,o,i)}consoleProfileFinished({id:e,location:o,profile:i,title:t}){t||(t=this._anonymousConsoleProfileIdToTitle.get(e),this._anonymousConsoleProfileIdToTitle.delete(e)),Root.Runtime.Runtime.instance().loadModulePromise("profiler").then((()=>{this._dispatchProfileEvent(Events.ConsoleProfileFinished,e,o,t,i)}))}_dispatchProfileEvent(e,o,i,t,r){const s=Location.fromPayload(this._debuggerModel,i),l={id:this.target().id()+"."+o,scriptLocation:s,cpuProfile:r,title:t,cpuProfilerModel:this};this.dispatchEventToListeners(e,l)}isRecordingProfile(){return this._isRecording}startRecording(){this._isRecording=!0;return this._profilerAgent.invoke_setSamplingInterval({interval:100}),this._profilerAgent.invoke_start()}stopRecording(){return this._isRecording=!1,this._profilerAgent.invoke_stop().then((e=>e.profile||null))}startPreciseCoverage(e,o){this._preciseCoverageDeltaUpdateCallback=o;return this._profilerAgent.invoke_startPreciseCoverage({callCount:!1,detailed:e,allowTriggeredUpdates:!0})}async takePreciseCoverage(){const e=await this._profilerAgent.invoke_takePreciseCoverage();return{timestamp:e&&e.timestamp||0,coverage:e&&e.result||[]}}stopPreciseCoverage(){return this._preciseCoverageDeltaUpdateCallback=null,this._profilerAgent.invoke_stopPreciseCoverage()}preciseCoverageDeltaUpdate({timestamp:e,occassion:o,result:i}){this._preciseCoverageDeltaUpdateCallback&&this._preciseCoverageDeltaUpdateCallback(e,o,i)}}export const Events={ConsoleProfileStarted:Symbol("ConsoleProfileStarted"),ConsoleProfileFinished:Symbol("ConsoleProfileFinished")};SDKModel.register(CPUProfilerModel,Capability.JS,!0);export let EventData;