/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../../../core/CircularArray.js";import{EventEmitter as t}from"../../../../core/Evented.js";import has from"../../../../core/has.js";import{createDisjointTimerQuery as n}from"../../../webgl/capabilities/DisjointTimerQuery.js";const s=!!has("esri-2d-profiler");class i{constructor(i,o){if(this._events=new t,this._entries=new Map,this._timings=new e(10),this._currentContainer=null,!s)return;this._ext=n(i.gl,{}),this._debugOutput=o;const r=i.gl;if(!this.enableCommandLogging)return;let a;for(a in r)if("function"==typeof r[a]){const e=r[a],t=a.includes("draw");r[a]=(...n)=>(this._events.emit("command",{container:this._currentContainer,method:a,args:n,isDrawCommand:t}),e.apply(r,n))}}get enableCommandLogging(){return"object"==typeof s&&s.commandLogging}get enableTimeLogging(){return"object"==typeof s&&s.timeLogging}get lastTime(){return this._timings.peekLast()}recordContainerStart(e){s&&(this._currentContainer=e)}recordContainerEnd(){s&&(this._currentContainer=null)}recordStart(e){if(s&&null!=this._ext){if(this._entries.has(e)){const t=this._entries.get(e),n=this._ext.resultAvailable(t.query),s=this._ext.disjoint();if(n&&!s){const n=this._ext.getResult(t.query)/1e6;let s=0;if(null!=this._timings.enqueue(n)&&this.enableTimeLogging){const e=this._timings.entries,t=e.length;let n=0;for(const s of e)n+=s;s=n/t}const i=n.toFixed(2),o=s?s.toFixed(2):"--";this.enableCommandLogging?(this.enableTimeLogging?console.groupCollapsed(`Frame report for ${e}, ${i} ms (${o} last 10 avg)\n              ${t.commandsLen} Commands (${t.drawCommands} draw)`):console.groupCollapsed(`Frame report for ${e}\n              ${t.commandsLen} Commands (${t.drawCommands} draw)`),console.log("Commands: ",t.commands),console.groupEnd()):this.enableTimeLogging&&console.log(`Frame report for ${e}, ${i} ms (${o} last 10 avg)`),this.enableTimeLogging&&(this._debugOutput.innerHTML=`${i} (${o})`)}for(const e of t.handles)e.remove();this._ext.deleteQuery(t.query),this._entries.delete(e)}const t={name:e,query:this._ext.createQuery(),commands:[],commandsLen:0,drawCommands:0,handles:[]};this.enableCommandLogging&&t.handles.push(this._events.on("command",e=>{t.commandsLen++,t.commands.push(e),e.isDrawCommand&&t.drawCommands++})),this._ext.beginTimeElapsed(t.query),this._entries.set(e,t)}}recordEnd(e){s&&null!=this._ext&&this._entries.has(e)&&this._ext.endTimeElapsed()}}export{i as Profiler};
