"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.syncPseudoLiveRuntime=exports.syncPseudoLivePlayhead=exports.resolvePseudoLiveTimeUpdateActions=exports.getPseudoLivePayload=exports.estimatePseudoLiveCurrentTime=exports.createPseudoLiveState=exports.PseudoLiveTimeUpdateAction=void 0;var _constant=require("../../utils/constant"),PseudoLiveTimeUpdateAction={SEEK:"seek",PLAYBACK_RATE:"playbackRate",FINISH:"finish"};exports.PseudoLiveTimeUpdateAction=PseudoLiveTimeUpdateAction;var createPseudoLiveState=function(){return{enabled:!1,pendingInitialSeek:!1,seekTime:0,currentLoopNumber:1,totalLoopNumber:1,offset:0,duration:0,localLoopNumber:1,currentLineIndex:0,playbackRate:1,finalEnded:!1,playheadTime:0,playheadUpdatedAtMs:0,playing:!1}};exports.createPseudoLiveState=createPseudoLiveState;var getPseudoLivePayload=function(e){var o,t,i,a=(null==e?void 0:e.Basic)||{},n=null==e||null===(o=e.VodLoopVideoConfig)||void 0===o||null===(t=o.LineConfigs)||void 0===t?void 0:t[0],r=null==n||null===(i=n.VodLoopVideos)||void 0===i?void 0:i[0];return a.Status===_constant.LIVE_STATUS.直播&&a.IsVodLoopVideoEnable&&n&&r?{lineConfig:n,loopVideo:r}:null};exports.getPseudoLivePayload=getPseudoLivePayload;var syncPseudoLiveRuntime=function(e,o){var t,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},a=getPseudoLivePayload(o);if(!a)return null;var n=e||createPseudoLiveState(),r=a.lineConfig,u=a.loopVideo,d=null!==(t=i.currentLineIndex)&&void 0!==t?t:0,s=Number((null==r?void 0:r.CurrentLoopNumber)||1),l=Number((null==r?void 0:r.TotalLoopNumber)||1),p=Number((null==u?void 0:u.Offset)||0),v=Number((null==u?void 0:u.Duration)||0),m=i.resetLocalLoop||!n.enabled||n.currentLineIndex!==d,L=n.currentLoopNumber!==s||n.totalLoopNumber!==l||n.offset!==p||n.duration!==v;return n.enabled=!0,n.currentLineIndex=d,n.seekTime=Number((null==u?void 0:u.SeekTime)||0),n.currentLoopNumber=s,n.totalLoopNumber=l,n.offset=p,n.duration=v,(m||L||i.pendingInitialSeek)&&(n.finalEnded=!1),m&&(n.localLoopNumber=n.currentLoopNumber),i.pendingInitialSeek&&(n.pendingInitialSeek=!0,n.playbackRate=1),n};exports.syncPseudoLiveRuntime=syncPseudoLiveRuntime;var estimatePseudoLiveCurrentTime=function(e){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:Date.now(),t=Number((null==e?void 0:e.playheadTime)||0),i=Number((null==e?void 0:e.playbackRate)||1),a=Number((null==e?void 0:e.playheadUpdatedAtMs)||0);return null==e||!e.playing||!a||!Number.isFinite(i)||i<=0?t:t+Math.max(0,o-a)/1e3*i};exports.estimatePseudoLiveCurrentTime=estimatePseudoLiveCurrentTime;var syncPseudoLivePlayhead=function(e,o){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(!e)return null;var i=t.nowMs||Date.now(),a=Number(o),n=void 0!==o&&Number.isFinite(a)?a:estimatePseudoLiveCurrentTime(e,i);e.playheadTime=n>=0?n:0,e.playheadUpdatedAtMs=i,"boolean"==typeof t.playing&&(e.playing=t.playing);var r=Number(t.playbackRate);return Number.isFinite(r)&&r>0&&(e.playbackRate=r),e};exports.syncPseudoLivePlayhead=syncPseudoLivePlayhead;var resolvePseudoLiveTimeUpdateActions=function(e,o){if(null==e||!e.enabled)return[];var t=[],i=Number(e.seekTime||0);if(e.localLoopNumber!==e.currentLoopNumber)return e.localLoopNumber=e.currentLoopNumber,[{type:PseudoLiveTimeUpdateAction.SEEK,time:i,reason:"loop_number_changed",targetTime:i}];var a=i-o;return a>=60?t.push({type:PseudoLiveTimeUpdateAction.SEEK,time:i,reason:"gap_gte_60",gapTime:a,targetTime:i}):a>10?t.push({type:PseudoLiveTimeUpdateAction.PLAYBACK_RATE,rate:1.2,reason:"gap_gt_10",gapTime:a,targetTime:i}):a<3&&t.push({type:PseudoLiveTimeUpdateAction.PLAYBACK_RATE,rate:1,reason:"gap_lt_3",gapTime:a,targetTime:i}),e.currentLoopNumber>=e.totalLoopNumber&&e.duration>0&&o>=e.duration&&t.push({type:PseudoLiveTimeUpdateAction.FINISH}),t};exports.resolvePseudoLiveTimeUpdateActions=resolvePseudoLiveTimeUpdateActions;