import*as Platform from"../platform/platform.js";import{TracingModel}from"./TracingModel.js";export class FilmStripModel{constructor(e,t){this._frames=[],this._zeroTime=0,this._spanTime=0,this.reset(e,t)}reset(e,t){this._zeroTime=t||e.minimumRecordTime(),this._spanTime=e.maximumRecordTime()-this._zeroTime,this._frames=[];const s=TracingModel.browserMainThread(e);if(!s)return;const r=s.events();for(let a=0;a<r.length;++a){const e=r[a];if(!(e.startTime<this._zeroTime)&&e.hasCategory(_category))if(e.name===TraceEvents.CaptureFrame){e.args.data&&this._frames.push(Frame._fromEvent(this,e,this._frames.length))}else e.name===TraceEvents.Screenshot&&this._frames.push(Frame._fromSnapshot(this,e,this._frames.length))}}frames(){return this._frames}zeroTime(){return this._zeroTime}spanTime(){return this._spanTime}frameByTimestamp(e){const t=Platform.ArrayUtilities.upperBound(this._frames,e,((e,t)=>e-t.timestamp))-1;return t>=0?this._frames[t]:null}}const _category="disabled-by-default-devtools.screenshot",TraceEvents={CaptureFrame:"CaptureFrame",Screenshot:"Screenshot"};export class Frame{constructor(e,t,s){this._model=e,this.timestamp=t,this.index=s,this._imageData=null,this._snapshot=null}static _fromEvent(e,t,s){const r=new Frame(e,t.startTime,s);return r._imageData=t.args.data,r}static _fromSnapshot(e,t,s){const r=new Frame(e,t.startTime,s);return r._snapshot=t,r}model(){return this._model}imageDataPromise(){return this._imageData||!this._snapshot?Promise.resolve(this._imageData):this._snapshot.objectPromise()}}