import*as i18n from"../i18n/i18n.js";import{ColdColorScheme,HotColorScheme,TickingFlameChart}from"./TickingFlameChart.js";const NO_NORMALIZED_TIMESTAMP=-1.5,UIStrings={playbackStatus:"Playback Status",bufferingStatus:"Buffering Status"},str_=i18n.i18n.registerUIStrings("media/EventTimelineView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class PlayerEventsTimeline extends TickingFlameChart{constructor(){super(),this._normalizedTimestamp=-1.5,this.addGroup(i18nString(UIStrings.playbackStatus),2),this.addGroup(i18nString(UIStrings.bufferingStatus),2),this._playbackStatusLastEvent=null,this._audioBufferingStateEvent=null,this._videoBufferingStateEvent=null}_ensureNoPreviousPlaybackEvent(e){null!==this._playbackStatusLastEvent&&(this._playbackStatusLastEvent.endTime=e,this._playbackStatusLastEvent=null)}_onPlaybackEvent(e,t){switch(e.event){case"kPlay":this.canTick=!0,this._ensureNoPreviousPlaybackEvent(t),this._playbackStatusLastEvent=this.startEvent({level:0,startTime:t,name:"Play"});break;case"kPause":this._ensureNoPreviousPlaybackEvent(t),this._playbackStatusLastEvent=this.startEvent({level:0,startTime:t,name:"Pause",color:HotColorScheme[1]});break;case"kWebMediaPlayerDestroyed":this.canTick=!1,this._ensureNoPreviousPlaybackEvent(t),this.addMarker({level:1,startTime:t,name:"Destroyed",color:HotColorScheme[4]});break;case"kSuspended":this.canTick=!1,this._ensureNoPreviousPlaybackEvent(t),this._playbackStatusLastEvent=this.startEvent({level:1,startTime:t,name:"Suspended",color:HotColorScheme[3]});break;case"kEnded":this._ensureNoPreviousPlaybackEvent(t),this._playbackStatusLastEvent=this.startEvent({level:1,startTime:t,name:"Ended",color:HotColorScheme[2]});break;default:throw`_onPlaybackEvent cant handle ${e.event}`}}_bufferedEnough(e){return"BUFFERING_HAVE_ENOUGH"===e.state}_onBufferingStatus(e,t){let a=null,n=null;if("kBufferingStateChanged"!==e.event)throw`_onPlaybackEvent cant handle ${e.event}`;a=e.value.audio_buffering_state,n=e.value.video_buffering_state,a&&(null!==this._audioBufferingStateEvent&&(this._audioBufferingStateEvent.endTime=t,this._audioBufferingStateEvent=null),this._bufferedEnough(a)||(this._audioBufferingStateEvent=this.startEvent({level:3,startTime:t,name:"Audio Buffering",color:ColdColorScheme[1]}))),n&&(null!==this._videoBufferingStateEvent&&(this._videoBufferingStateEvent.endTime=t,this._videoBufferingStateEvent=null),this._bufferedEnough(n)||(this._videoBufferingStateEvent=this.startEvent({level:2,startTime:t,name:"Video Buffering",color:ColdColorScheme[0]})))}onEvent(e){-1.5===this._normalizedTimestamp&&(this._normalizedTimestamp=Number(e.timestamp));const t=1e3*(Number(e.timestamp)-this._normalizedTimestamp);switch(e.event){case"kPlay":case"kPause":case"kWebMediaPlayerDestroyed":case"kSuspended":case"kEnded":return this._onPlaybackEvent(e,t);case"kBufferingStateChanged":return this._onBufferingStatus(e,t)}}}