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