/* 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{HandleOwner as e}from"../../core/HandleOwner.js";import{applySome as i,isNone as n,isSome as s,unwrapOr as a}from"../../core/maybe.js";import{when as l,initial as o,watch as r,on as h}from"../../core/reactiveUtils.js";import{schedule as g}from"../../core/scheduling.js";import{truncateLocalTime as d,resetUTCDate as p,offsetDate as u}from"../../core/timeUtils.js";import{property as c}from"../../core/accessorSupport/decorators/property.js";import"../../core/arrayUtils.js";import"../../core/accessorSupport/ensureType.js";import{subclass as y}from"../../core/accessorSupport/decorators/subclass.js";import m from"../../views/SceneView.js";import _ from"../../views/3d/environment/SunLighting.js";import f from"../../views/3d/environment/VirtualLighting.js";import{positionToTimezoneInfo as w}from"../../views/3d/support/earthUtils.js";import{dateTimeToSliderPos as D,sliderPosToDateTime as S,getSeasonFromDate as v,getNorthernHemisphereSeason as T,getSeasonDate as U,Hemisphere as b,calculatePlaySpeed as L}from"./support/daylightUtils.js";import{SliderWithDropdownViewModel as P}from"./support/SliderWithDropdownViewModel.js";import E from"../support/DatePickerViewModel.js";import{formatSliderLabel as O,getSunriseAndSunsetTimes as C}from"../support/timeWidgetUtils.js";let M=class extends e{constructor(t){super(t),this.view=null,this.datePickerViewModel=new E,this.timeSliderViewModel=new P({min:0,max:1439,values:[0],labelFormatFunction:O,inputFormatFunction:O}),this.lightingUpdateInterval=200,this._oldLighting=null,this.playSpeedMultiplier=1,this._cachedLightingDateUTC=new Date(0),this._cachedDisplayUTCOffset=0,this._firstInteraction=!0,this._lastLightingUpdate=0,this._nextLightingUpdate=null}initialize(){this.handles.add([l((()=>this.view),(t=>t.when((()=>this._updateLighting(null)))),o),r((()=>i(this._lighting,(t=>"virtual"!==t.type?t.date:null))),(t=>this._scheduleUpdateLighting(t))),h((()=>this._lighting),"timezone-will-change",(t=>this._timezoneWillChange(t)),{onListenerAdd:()=>this._timezoneWillChange(null)}),r((()=>!0===i(this.view,(t=>t.stationary))),(()=>{(this.dayPlaying||this.yearPlaying)&&this._updateSunriseAndSunset()}),o),r((()=>{const t=this.timeSliderViewModel;return{vm:t,state:t.state,sliderPosition:this.timeSliderPosition}}),(({vm:t,state:e,sliderPosition:i})=>{"ready"===e&&t.setValue(0,i)})),r((()=>this.timeSliderViewModel.currentItem?.utcOffset),(t=>this.utcOffset=t)),r((()=>this.timeSliderViewModel.isDropdownOpen),(()=>this.stopPlaying())),r((()=>this.timeSliderViewModel.values),(t=>this.timeSliderPosition=t[0])),r((()=>this.datePickerViewModel.value),(t=>{this.dayPlaying=!1,this.localDate=t})),r((()=>this.localDate),(t=>{this.datePickerViewModel.value.valueOf()!==t.getTime()&&this.datePickerViewModel.set("value",t)}))])}destroy(){this._nextLightingUpdate&&(clearTimeout(this._nextLightingUpdate),this._nextLightingUpdate=null),this.view=null}get isSupported(){return n(this.view)||"3d"===this.view.type}get utcOffset(){return this._cachedDisplayUTCOffset}set utcOffset(t){t!==this.utcOffset&&s(this._lighting)&&"virtual"!==this._lighting.type&&(this._lighting.displayUTCOffset=t,this._updateLighting(null))}get localDate(){return d(this._lightingDateDisplay)}set localDate(t){t.getTime()!==this.localDate.getTime()&&(this._lightingDateDisplay=p(this._lightingDateDisplay,t))}get timeSliderPosition(){return D(this._lightingDateDisplay)}set timeSliderPosition(t){Math.abs(t-this.timeSliderPosition)>1/60&&(this._lightingDateDisplay=S(this._lightingDateDisplay,t),this.stopPlaying(),this._enableDirectShadowsIfFirstInteraction())}_timezoneFromCamera(t,e){if(n(e)||!t.cameraTrackingEnabled)return 0;const i=w([e.longitude,e.latitude]);return n(i)?0:Math.round(i.hours+i.minutes/60+i.seconds/3600)}get directShadowsEnabled(){const t=i(this._lighting,(t=>t.directShadowsEnabled));return a(t,!1)}set directShadowsEnabled(t){i(this._lighting,(e=>{e.directShadowsEnabled=t}))}get sunLightingEnabled(){return"sun"===this._lightingType}set sunLightingEnabled(t){const e=this._environment;if(t===this._get("sunLightingEnabled")||n(e))return;const i=e.lighting,a=this._oldLighting;this._oldLighting=i;const l={directShadowsEnabled:i.directShadowsEnabled,ambientOcclusionEnabled:i.ambientOcclusionEnabled,waterReflectionEnabled:i.waterReflectionEnabled,cameraTrackingEnabled:i.cameraTrackingEnabled},o=t?"sun":"virtual";let r;r=s(a)&&a.type===o?a:t?new _:new f,r.set(l),e.lighting=r,t||(this.timeSliderViewModel.isDropdownOpen=!1)}set playingState(t){this.playingState!==t&&this.sunLightingEnabled&&(this._set("playingState",t),"none"!==t&&(this._updateSunriseAndSunset(),this._lastTime=Date.now(),this._play(),this._enableDirectShadowsIfFirstInteraction()))}get dayPlaying(){return"day"===this.playingState}set dayPlaying(t){t?this.playingState="day":this.dayPlaying&&(this.playingState="none")}get yearPlaying(){return"year"===this.playingState}set yearPlaying(t){t?this.playingState="year":this.yearPlaying&&(this.playingState="none")}get currentSeason(){return v(this.localDate,this._currentHemisphere)}set currentSeason(t){this.stopPlaying();const e=T(t,this._currentHemisphere);this.localDate=U(this.localDate,e,b.NORTHERN)}get _currentHemisphere(){const t=i(this.view,(t=>t.camera?.position));return n(t)||t.latitude>=0?b.NORTHERN:b.SOUTHERN}get _environment(){return i(this.view,(t=>t.environment))}get _lighting(){return i(this._environment,(t=>t.lighting))}get _lightingType(){return i(this._lighting,(t=>t.type))}get _lightingDateDisplay(){return u(this._cachedLightingDateUTC,this._cachedDisplayUTCOffset,"hours")}set _lightingDateDisplay(t){const e=this._lighting;if(n(e)||!this.sunLightingEnabled||"virtual"===e.type)return;const i=u(t,-this._cachedDisplayUTCOffset,"hours");i.getTime()!==e.date.getTime()&&(e.date=i,this._updateLighting(null))}stopPlaying(){this.playingState="none"}toggleDayPlaying(){this.dayPlaying=!this.dayPlaying}toggleYearPlaying(){this.yearPlaying=!this.yearPlaying}toggleSunLightingEnabled(){this.stopPlaying(),this.sunLightingEnabled=!this.sunLightingEnabled}toggleDirectShadowsEnabled(){this.stopPlaying(),this.directShadowsEnabled=!this.directShadowsEnabled}_enableDirectShadowsIfFirstInteraction(){this._firstInteraction&&(this._firstInteraction=!1,this.directShadowsEnabled=!0)}_updateLighting(t){const e=Date.now();this._lastLightingUpdate=e;const{view:i}=this,s=this._lighting;if(n(i)||n(s)||"virtual"===s.type)return;const a=t||s.date,l=s.displayUTCOffset,o=null!==l?l:this._timezoneFromCamera(s,i.camera?.position);this._cachedLightingDateUTC.getTime()!==a.getTime()&&(this._cachedLightingDateUTC=new Date(a.getTime())),this._cachedDisplayUTCOffset!==o&&(this._cachedDisplayUTCOffset=o)}_timezoneWillChange(t){const e=this._lighting;if(n(e)||"virtual"===e.type||!e.cameraTrackingEnabled)return;let i;if(t)i=t.timezoneOffset;else{if(null!=e.displayUTCOffset)return;i=_.calculateTimezoneOffset(e.positionTimezoneInfo)}e.displayUTCOffset=i,this._scheduleUpdateLighting(null)}_scheduleUpdateLighting(t){if(this._nextLightingUpdate||n(t))return;const e=Date.now()-this._lastLightingUpdate,i=this.lightingUpdateInterval-e;i<=0?g((()=>this._updateLighting(t))):this._nextLightingUpdate=setTimeout((()=>{this._updateLighting(null),this._nextLightingUpdate=null}),i)}_play(){const t=this._lighting;if(!n(t)&&this.sunLightingEnabled&&"virtual"!==t.type&&(this.dayPlaying||this.yearPlaying)){const e=Date.now()-this._lastTime;if(this.dayPlaying){this._lastTime=Date.now();const i=L(this._sunrise,this._sunset,t.date)*this.playSpeedMultiplier/100*e;if(i>0){let e=new Date(t.date.getTime()+i);if(((e.getUTCHours()+t.displayUTCOffset)%24+24)%24<((t.date.getUTCHours()+t.displayUTCOffset)%24+24)%24){const n=864e5;e=new Date(t.date.getTime()+i-n)}t.date=e}}else{if(e>1e3){this._lastTime=Date.now();const e=(t.date.getUTCMonth()+1)%12,i=new Date(t.date.getTime());i.setUTCMonth(e),t.date=i}}requestAnimationFrame((()=>this._play()))}}_updateSunriseAndSunset(){const t=this._lighting;if(n(t)||"virtual"===t.type||!this.sunLightingEnabled)return;const e=i(this.view,(t=>t.camera?.position));if(n(e))return;const s=C(t.date,e.latitude,e.longitude,t.displayUTCOffset);this._sunrise=new Date(s.sunrise),this._sunset=new Date(s.sunset)}};t([c({type:m})],M.prototype,"view",void 0),t([c({type:E,nonNullable:!0})],M.prototype,"datePickerViewModel",void 0),t([c({type:P,nonNullable:!0})],M.prototype,"timeSliderViewModel",void 0),t([c()],M.prototype,"isSupported",null),t([c()],M.prototype,"lightingUpdateInterval",void 0),t([c()],M.prototype,"utcOffset",null),t([c()],M.prototype,"localDate",null),t([c()],M.prototype,"timeSliderPosition",null),t([c()],M.prototype,"directShadowsEnabled",null),t([c()],M.prototype,"sunLightingEnabled",null),t([c({type:["none","day","year"],value:"none"})],M.prototype,"playingState",null),t([c()],M.prototype,"dayPlaying",null),t([c()],M.prototype,"yearPlaying",null),t([c()],M.prototype,"playSpeedMultiplier",void 0),t([c()],M.prototype,"currentSeason",null),t([c()],M.prototype,"_lastTime",void 0),t([c()],M.prototype,"_sunrise",void 0),t([c()],M.prototype,"_sunset",void 0),t([c()],M.prototype,"_cachedLightingDateUTC",void 0),t([c()],M.prototype,"_cachedDisplayUTCOffset",void 0),t([c()],M.prototype,"_firstInteraction",void 0),t([c()],M.prototype,"_currentHemisphere",null),t([c()],M.prototype,"_environment",null),t([c()],M.prototype,"_lighting",null),t([c()],M.prototype,"_lightingType",null),t([c()],M.prototype,"_lightingDateDisplay",null),M=t([y("esri.widgets.Daylight.DaylightViewModel")],M);const j=M;export{j as default};