/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.25/esri/copyright.txt for details. */ import{_ as t}from"../../chunks/tslib.es6.js";import e from"../../TimeExtent.js";import i from"../../TimeInterval.js";import n from"../../core/Collection.js";import s from"../../core/Evented.js";import{HandleOwnerMixin as r}from"../../core/HandleOwner.js";import o from"../../core/Logger.js";import{isSome as l,isNone as a,unwrap as m,applySome as u}from"../../core/maybe.js";import{after as p,isAbortError as d}from"../../core/promiseUtils.js";import{watch as h,whenOnce as c,initial as f,syncAndInitial as g}from"../../core/reactiveUtils.js";import{offsetDate as w}from"../../core/timeUtils.js";import{property as T}from"../../core/accessorSupport/decorators/property.js";import"../../core/arrayUtils.js";import{ensureType as v}from"../../core/accessorSupport/ensureType.js";import{subclass as x}from"../../core/accessorSupport/decorators/subclass.js";import E from"../../webdoc/Widgets.js";import y from"../../webdoc/widgets/TimeSlider.js";const _=1e4;function b(t){return l(t)&&void 0!==t.count}function C(t){return l(t)&&void 0!==t.interval}function S(t){return l(t)&&void 0!==t.dates}function j(t){return"esri.WebMap"===t.declaredClass}let A=class extends(r(s.EventedAccessor)){constructor(t){super(t),this._animationController=null,this._isViewTimeExtentInitialized=!1,this._timerId=null,this.actions=new n,this.fullTimeExtent=null,this.loop=!1,this.mode="time-window",this.stops={count:10},this.timeExtent=null,this.view=null}initialize(){this.handles.add([h((()=>this.effectiveStops),(()=>{a(this.timeExtent)&&(this.timeExtent=this._getDefaultTimeExtent())}),f),h((()=>this.view),((t,e)=>{this._unregisterWidget(e),this._registerWidget(t)}),g),h((()=>this.timeExtent),(t=>{if(a(this.view))return;const e=this.view.timeExtent;(a(t)||t.isAllTime)&&(a(e)||e.isAllTime)||l(t)&&l(e)&&t.equals(e)||(this.view.timeExtent=m(t))}),f),h((()=>u(this.view,(t=>t.timeExtent))),(t=>{this._isViewTimeExtentInitialized?(a(t)||t.isAllTime)&&(a(this.timeExtent)||this.timeExtent.isAllTime)||l(t)&&l(this.timeExtent)&&t.equals(this.timeExtent)||(this.timeExtent=t):this._isViewTimeExtentInitialized=!0}))])}destroy(){l(this._timerId)&&(clearInterval(this._timerId),this._timerId=null),this._unregisterWidget(this.view),this.view=null,l(this._animationController)&&(this._animationController.abort(),this._animationController=null)}get effectiveStops(){const{fullTimeExtent:t,stops:e}=this;if(S(e)){const{dates:i}=e;if(null==i||0===i.length)return null;const n=i.sort(((t,e)=>t.getTime()-e.getTime()));if(a(t))return n;const{start:s,end:r}=t;if(a(s)||a(r))return n;return n.filter((t=>!(t.getTime()r.getTime())))}if(b(e)){const i=e.timeExtent??m(t);return this._divideTimeExtentByCount(i,e.count)}if(C(e)){const i=e.timeExtent??m(t);return this._divideTimeExtentByInterval(i,e.interval)}return[]}set playRate(t){t<=0||t>36e5||("playing"===this.state&&this._startAnimation(),this._set("playRate",t))}get state(){return a(this.fullTimeExtent)?"disabled":l(this._animationController)?"playing":"ready"}get timeExtentValues(){const{mode:t,timeExtent:e}=this;if(a(e)||e.isAllTime||e.isEmpty)return null;const{start:i,end:n}=e;switch(t){case"cumulative-from-end":case"instant":return l(i)?[i.getTime()]:null;case"cumulative-from-start":return l(n)?[n.getTime()]:null;case"time-window":return l(i)&&l(n)?[i.getTime(),n.getTime()]:null}}static async getPropertiesFromWebMap(t,e){if(a(t)||!j(t))return null;await t.load({signal:e});const i=t?.widgets?.timeSlider;if(!i)return null;const{getFullTimeExtentFromWebDocument:n,getModeFromTimeSlider:s,getStopsFromTimeSlider:r,getTimeExtentFromTimeSlider:o}=await import("./utils.js"),l=await n(t,e),m=i.loop,u=s(i);return{fullTimeExtent:l,loop:m,mode:u,playRate:i.stopDelay??2e3,stops:r(i),timeExtent:o(i,u)}}next(){this._step({forward:!0,allowRestart:!1})}play(){this._startAnimation()}previous(){this._step({forward:!1,allowRestart:!1})}stop(){this._stopAnimation()}triggerAction(t){this.emit("trigger-action",{action:t})}updateWebDocument(t){if(j(t)){const e=new y({currentTimeExtent:this.timeExtent,fullTimeExtent:this.fullTimeExtent,loop:this.loop,numStops:b(this.stops)?this.stops.count:null,numThumbs:"time-window"===this.mode?2:1,stopDelay:this.playRate,stopInterval:C(this.stops)?this.stops.interval:null,stops:S(this.stops)?this.stops.dates:null});t.widgets?t.widgets.timeSlider=e:t.widgets=new E({timeSlider:e})}}valuesToTimeExtent(t){if(a(t))return null;const i=t.sort(((t,e)=>t-e)).map((t=>new Date(t))),n=i.length>0?i[0]:null,s=i.length>1?i[1]:null;switch(this.mode){case"time-window":return new e({start:n,end:s});case"instant":return new e({start:n,end:n});case"cumulative-from-start":return new e({start:null,end:n});case"cumulative-from-end":return new e({start:n,end:null});default:return e.allTime}}async _animate(){try{const t=this.view,e=u(this._animationController,(t=>t.signal));await Promise.all([p(this.playRate,null,e),l(t)&&c((()=>!1===t.updating),e)])}catch(t){return d(t)||o.getLogger(this.declaredClass).error(t),void(this._animationController=null)}this._step({forward:!0,allowRestart:!1}),a(this._animationController)||this._animate()}_divideTimeExtentByCount(t,e=10){if(!t||!e)return[];const{start:i,end:n}=t;if(a(i)||a(n))return[];if(e>_)return this._divideTimeExtentByCount(t);const s=[],r=i.getTime(),o=n.getTime()-r;for(let l=0;l<=e;l++)s.push(new Date(r+l/e*o));return s}_divideTimeExtentByInterval(t,e,i=_){if(!t||!e)return[];const{start:n,end:s}=t;if(a(n)||a(s))return[];const r=s.getTime()-n.getTime(),o=e.toMilliseconds();if(o<=0||r/o>i)return this._divideTimeExtentByCount(t);const l=[],{value:m,unit:u}=e;let p=n;for(;p.getTime()<=s.getTime();)l.push(new Date(p.getTime())),p=w(p,m,u);return l}_getDefaultTimeExtent(){const{effectiveStops:t,mode:i}=this;if(a(t)||!i)return null;switch(i){case"time-window":return t.length>1?new e({start:t[0],end:t[1]}):null;case"cumulative-from-start":return t.length>0?new e({start:null,end:t[0]}):null;case"cumulative-from-end":return t.length>0?new e({start:t[0],end:null}):null;case"instant":return t.length>0?new e({start:t[0],end:t[0]}):null;default:return null}}_registerWidget(t){if(l(t)){t.persistableViewModels.includes(this)||t.persistableViewModels.add(this)}}_startAnimation(){this._stopAnimation(),this._animationController=new AbortController,this._step({forward:!0,allowRestart:!0}),this._animate()}_step(t){const{forward:e,allowRestart:i}=t,{effectiveStops:n}=this;if(a(this.timeExtentValues)||a(n))return;const s=n.map((t=>t.getTime())),r=this.timeExtentValues.map((t=>{const e=s.indexOf(t);if(-1!==e)return e;const i=s.reduce(((e,i)=>Math.abs(i-t)t+(e?1:-1))),l=o.some((t=>t<0||t>s.length-1)),{loop:m,state:u}=this;if(l)if(m||i){const t=Math.min(...r),i=Math.max(...r),n=(e?r.map((e=>e-t)):r.map((t=>t+(s.length-1-i)))).map((t=>s[t]));this.timeExtent=this.valuesToTimeExtent(n)}else"playing"===u&&this.stop();else{const t=o.map((t=>s[t]));this.timeExtent=this.valuesToTimeExtent(t)}}_stopAnimation(){l(this._animationController)&&(this._animationController.abort(),this._animationController=null)}_unregisterWidget(t){l(t)&&t.persistableViewModels.remove(this)}};t([T()],A.prototype,"_animationController",void 0),t([T({type:n})],A.prototype,"actions",void 0),t([T({readOnly:!0})],A.prototype,"effectiveStops",null),t([T({type:e})],A.prototype,"fullTimeExtent",void 0),t([T({nonNullable:!0})],A.prototype,"loop",void 0),t([T({nonNullable:!0})],A.prototype,"mode",void 0),t([T({nonNullable:!0,value:1e3})],A.prototype,"playRate",null),t([T({readOnly:!0})],A.prototype,"state",null),t([T({cast:t=>a(t)?null:(C(t)&&(t.interval=v(i,t.interval)),(C(t)||b(t))&&(t.timeExtent=v(e,t.timeExtent)),t)})],A.prototype,"stops",void 0),t([T({type:e})],A.prototype,"timeExtent",void 0),t([T({readOnly:!0})],A.prototype,"timeExtentValues",null),t([T()],A.prototype,"view",void 0),t([T()],A.prototype,"next",null),t([T()],A.prototype,"play",null),t([T()],A.prototype,"previous",null),t([T()],A.prototype,"stop",null),t([T()],A.prototype,"updateWebDocument",null),A=t([x("esri.widgets.TimeSlider.TimeSliderViewModel")],A);const I=A;export{I as default};