import*as Platform from"../platform/platform.js";import{Capability,SDKModel}from"./SDKModel.js";export class PerformanceMetricsModel extends SDKModel{constructor(e){super(e),this._agent=e.performanceAgent();const t=MetricMode;this._metricModes=new Map([["TaskDuration",t.CumulativeTime],["ScriptDuration",t.CumulativeTime],["LayoutDuration",t.CumulativeTime],["RecalcStyleDuration",t.CumulativeTime],["LayoutCount",t.CumulativeCount],["RecalcStyleCount",t.CumulativeCount]]),this._metricData=new Map}enable(){return this._agent.invoke_enable({})}disable(){return this._agent.invoke_disable()}async requestMetrics(){const e=await this._agent.invoke_getMetrics()||[],t=new Map,a=performance.now();for(const i of e.metrics){let e,l=this._metricData.get(i.name);switch(l||(l={lastValue:void 0,lastTimestamp:void 0},this._metricData.set(i.name,l)),this._metricModes.get(i.name)){case MetricMode.CumulativeTime:e=l.lastTimestamp&&l.lastValue?Platform.NumberUtilities.clamp(1e3*(i.value-l.lastValue)/(a-l.lastTimestamp),0,1):0,l.lastValue=i.value,l.lastTimestamp=a;break;case MetricMode.CumulativeCount:e=l.lastTimestamp&&l.lastValue?Math.max(0,1e3*(i.value-l.lastValue)/(a-l.lastTimestamp)):0,l.lastValue=i.value,l.lastTimestamp=a;break;default:e=i.value}t.set(i.name,e)}return{metrics:t,timestamp:a}}}const MetricMode={CumulativeTime:Symbol("CumulativeTime"),CumulativeCount:Symbol("CumulativeCount")};SDKModel.register(PerformanceMetricsModel,Capability.DOM,!1);