/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{Milliseconds as t}from"../../../../../core/time.js";import{getRandomValue as e}from"../grouping.js";function r(e){return t(e.frameDurations.reduce((t,e)=>t+e,0))}function i(t){const{width:e,height:r}=t,i=t.frameDurations.reverse(),a=e=>{const r=t.frameDurations.length-1-e;return t.getFrame(r)};return{frameCount:t.frameCount,duration:t.duration,frameDurations:i,getFrame:a,width:e,height:r}}function a(e,r){const{width:i,height:a,getFrame:n}=e,s=r/e.duration,o=e.frameDurations.map(e=>t(e*s));return{frameCount:e.frameCount,duration:e.duration,frameDurations:o,getFrame:n,width:i,height:a}}function n(e,r){const{width:i,height:a,getFrame:n}=e,s=e.frameDurations.slice(),o=s.shift();return s.unshift(t(o+r)),{frameCount:e.frameCount,duration:e.duration+r,frameDurations:s,getFrame:n,width:i,height:a}}function s(e,r){const{width:i,height:a,getFrame:n}=e,s=e.frameDurations.slice(),o=s.pop();return s.push(t(o+r)),{frameCount:e.frameCount,duration:e.duration+r,frameDurations:s,getFrame:n,width:i,height:a}}class o{constructor(e,r,i,a){this._animation=e,this._repeatType=i,this._onFrameData=a,this._direction=1,this._currentFrame=0,this._lastUpdated=t(0),this.timeInFrame=0,this.timeToFrame=this._animation.frameDurations[this._currentFrame];let n=0;for(;r>n;)n+=this.timeToFrame,this.nextFrame();const s=this._animation.getFrame(this._currentFrame);this._onFrameData(s)}nextFrame(){if(this._currentFrame+=this._direction,this._direction>0){if(this._currentFrame===this._animation.frameDurations.length)switch(this._repeatType){case"None":this._currentFrame-=this._direction;break;case"Loop":this._currentFrame=0;break;case"Oscillate":this._currentFrame-=this._direction,this._direction=-1}}else if(-1===this._currentFrame)switch(this._repeatType){case"None":this._currentFrame-=this._direction;break;case"Loop":this._currentFrame=this._animation.frameDurations.length-1;break;case"Oscillate":this._currentFrame-=this._direction,this._direction=1}this.timeToFrame=this._animation.frameDurations[this._currentFrame],this.timeInFrame=0;const t=this._animation.getFrame(this._currentFrame);this._onFrameData(t)}update(e){const r=t(e-this._lastUpdated);this._lastUpdated=e,this._advance(r)}_advance(t){this.timeInFrame+=t,this.timeInFrame>this.timeToFrame&&this.nextFrame()}}function m(e,r){e.animationsEnabled&&r?.update(t(e.time))}function h(m,h,u,c){let d,{repeatType:f}=h;if(null==f&&(f="Loop"),!0===h.reverseAnimation&&(m=i(m)),null!=h.duration&&(m=a(m,t(1e3*h.duration))),null!=h.repeatDelay){const e=1e3*h.repeatDelay;"Loop"===f?m=s(m,t(e)):"Oscillate"===f&&(m=n(s(m,t(e/2)),t(e/2)))}if(null!=h.startTimeOffset)d=t(1e3*h.startTimeOffset);else if(null!=h.randomizeStartTime){const i=82749913,a=null!=h.randomizeStartSeed?h.randomizeStartSeed:i,n=e(u,a);d=t(n*r(m))}else d=t(0);return new o(m,d,f,c)}export{o as Player,a as adjustDuration,s as appendDelay,h as createPlayer,r as getDuration,n as prependDelay,i as reverse,m as updateIfNeeded};
