/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../core/Accessor.js";import{createTask as i}from"../../core/asyncUtils.js";import{handlesGroup as o}from"../../core/handleUtils.js";import{abortMaybe as r}from"../../core/maybe.js";import{memoize as s}from"../../core/memoize.js";import{throwIfAborted as l,throwIfAbortError as a,ignoreAbortErrors as n}from"../../core/promiseUtils.js";import{watch as p,syncAndInitial as m}from"../../core/reactiveUtils.js";import{throttle as d}from"../../core/throttle.js";import{property as h,subclass as u}from"../../core/accessorSupport/decorators.js";import{hasGraphicFeatureExpressionInfo as f,getGraphicEffectiveElevationInfo as c}from"../../support/elevationInfoUtils.js";import{getConfig as _}from"./support/constants.js";import{isPolyline as P,isValidInputPath as v}from"./support/geometryUtils.js";import{generateProfiles as g}from"./support/profileUtils.js";const M=Symbol("line-change");let w=class extends t{constructor(e){super(e),this._updateTask=null,this._paramsPerProfile=new Map,this._getUpdateParametersMemoized=s((e,t,i)=>({stationary:e,visibleProfiles:t,generationParameters:i})),this._getGenerationParametersMemoized=s((e,t,i,o,r,s,l)=>P(t)&&v(t)&&null!=o?{view:e,geometry:t,elevationInfo:i,options:s,queue:o,cache:r,slicePlane:l}:null),this._getElevationInfoMemoized=s((e,t)=>({mode:e,offset:t})),this._getOptionsMemoized=s((e,t)=>{const{densificationMaxSamples:i,maxTotalSamples:o}=_();return{samplingDistance:e,densificationMaxSamples:Math.round(i/t),maxTotalSamples:o}}),this._updateThrottled=d(e=>this._update(e),_().updateThrottleMillis)}initialize(){const e=this.viewModel;this.addHandles([p(()=>({visible:e.visible,profiles:e.profiles.toArray()}),({visible:e,profiles:t})=>{this._abortUpdate(),this._paramsPerProfile.clear(),this.removeHandles(M),e&&0!==t.length&&this.addHandles([...t.map(e=>o([e.attach(this.viewModel),e.on("change",()=>{this._invalidateProfile(e)})])),p(()=>this._updateParameters,this._updateThrottled,m),this._updateThrottled],M)},m)])}destroy(){this._paramsPerProfile.clear()}get _updateParameters(){const{viewModel:e}=this,t=e.view;return this._getUpdateParametersMemoized(null==t||t.stationary,this.viewModel.visibleProfiles,this._generationParameters)}get _generationParameters(){const{view:e,input:t,queue:i,tileCache:o}=this.viewModel;return null!=e&&e.ready?this._getGenerationParametersMemoized(e,t?.geometry,this._elevationInfo,i,o,this._options,"3d"===e.type?e.slice.plane:null):null}get _elevationInfo(){const e=this.viewModel.input,t=e?f(e)?null:c(e):null;return null!=t?this._getElevationInfoMemoized(t.mode,t.offset):null}get _options(){const e=this.viewModel,t=e.visibleProfiles.length;let i=e.minDemResolution??_().defaultDemResolution;return i=parseFloat(i.toFixed(2)),this._getOptionsMemoized(i,t)}_update({stationary:e,visibleProfiles:t,generationParameters:o}){if(this._abortUpdate(),!e)return;if(null==o)return void this._clearResults();const r=this._updateTask=i(async e=>{this.viewModel.error=null;const i=t.filter(e=>!this._isProfileValid(e,o)),r=g({...o,providers:i},{signal:e});try{for await(const t of r){l(e),this._clearInvalidResults(o);for(let e=0;e<i.length;e++)i[e].result=t[e]}for(const e of i)this._paramsPerProfile.set(e,o)}catch(s){a(s),this._abortUpdate(),this.viewModel.error=s,i.forEach(e=>{e.result=null})}}),s=this.viewModel.view;s?.addUpdatingPromise(n(r.promise))}_abortUpdate(){this._updateTask=r(this._updateTask)}_isProfileValid(e,t){return this._paramsPerProfile.has(e)&&this._paramsPerProfile.get(e)===t}_invalidateProfile(e){this._paramsPerProfile.delete(e),this._updateThrottled(this._updateParameters)}_clearInvalidResults(e){for(const t of this.viewModel.profiles.items)this._isProfileValid(t,e)||(t.result=null,this._paramsPerProfile.delete(t))}_clearResults(){for(const e of this.viewModel.profiles.items)e.result=null,this._paramsPerProfile.delete(e)}};e([h({nonNullable:!0})],w.prototype,"viewModel",void 0),e([h()],w.prototype,"_updateParameters",null),e([h()],w.prototype,"_generationParameters",null),e([h()],w.prototype,"_elevationInfo",null),e([h()],w.prototype,"_options",null),w=e([u("esri.widgets.ElevationProfile.ElevationProfileController")],w);export{w as ElevationProfileController};
