import{Capability,SDKModel}from"./SDKModel.js";export class TracingManager extends SDKModel{constructor(e){super(e),this._tracingAgent=e.tracingAgent(),e.registerTracingDispatcher(new TracingDispatcher(this)),this._activeClient=null,this._eventBufferSize=0,this._eventsRetrieved=0}_bufferUsage(e,t,i){this._eventBufferSize=void 0===t?null:t,this._activeClient&&this._activeClient.tracingBufferUsage(e||i||0)}_eventsCollected(e){this._activeClient&&(this._activeClient.traceEventsCollected(e),this._eventsRetrieved+=e.length,this._eventBufferSize?(this._eventsRetrieved>this._eventBufferSize&&(this._eventsRetrieved=this._eventBufferSize),this._activeClient.eventsRetrievalProgress(this._eventsRetrieved/this._eventBufferSize)):this._activeClient.eventsRetrievalProgress(0))}_tracingComplete(){this._eventBufferSize=0,this._eventsRetrieved=0,this._activeClient&&(this._activeClient.tracingComplete(),this._activeClient=null),this._finishing=!1}async start(e,t,i){if(this._activeClient)throw new Error("Tracing is already started");this._activeClient=e;const r={bufferUsageReportingInterval:500,categories:t,options:i,transferMode:Protocol.Tracing.StartRequestTransferMode.ReportEvents},n=await this._tracingAgent.invoke_start(r);return n.getError()&&(this._activeClient=null),n}stop(){if(!this._activeClient)throw new Error("Tracing is not started");if(this._finishing)throw new Error("Tracing is already being stopped");this._finishing=!0,this._tracingAgent.invoke_end()}}export class TracingManagerClient{traceEventsCollected(e){}tracingComplete(){}tracingBufferUsage(e){}eventsRetrievalProgress(e){}}class TracingDispatcher{constructor(e){this._tracingManager=e}bufferUsage({value:e,eventCount:t,percentFull:i}){this._tracingManager._bufferUsage(e,t,i)}dataCollected({value:e}){this._tracingManager._eventsCollected(e)}tracingComplete(){this._tracingManager._tracingComplete()}}SDKModel.register(TracingManager,Capability.Tracing,!1);export let EventPayload;