/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.24/esri/copyright.txt for details. */ import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../core/Accessor.js";import o from"../../core/Handles.js";import{handlesGroup as s}from"../../core/handleUtils.js";import{isNone as r,isSome as i,applySome as a,unwrapOr as l,abortMaybe as n}from"../../core/maybe.js";import{memoize as p}from"../../core/memoize.js";import{createTask as d,throwIfAborted as h,throwIfAbortError as m,ignoreAbortErrors as c}from"../../core/promiseUtils.js";import{watch as f,syncAndInitial as u}from"../../core/reactiveUtils.js";import{throttle as _}from"../../core/throttle.js";import{property as P}from"../../core/accessorSupport/decorators/property.js";import"../../core/arrayUtils.js";import"../../core/has.js";import"../../core/accessorSupport/ensureType.js";import{subclass as v}from"../../core/accessorSupport/decorators/subclass.js";import{hasGraphicFeatureExpressionInfo as g,getGraphicEffectiveElevationInfo as y}from"../../support/elevationInfoUtils.js";import{DENSIFICATION_MAX_SAMPLES as M,MAX_TOTAL_SAMPLES as w,DEFAULT_DEM_RESOLUTION as j,UPDATE_THROTTLE_MILLIS as U}from"./support/constants.js";import{isPolyline as b,isValidInputPath as T}from"./support/geometryUtils.js";import{generateProfiles as z}from"./support/profileUtils.js";const I="line-change";let k=class extends t{constructor(e){super(e),this._handles=new o,this._updateTask=null,this._paramsPerProfile=new Map,this._getUpdateParametersMemoized=p(((e,t,o)=>({stationary:e,visibleProfiles:t,generationParameters:o}))),this._getGenerationParametersMemoized=p(((e,t,o,s,i,a,l)=>b(t)&&T(t)&&!r(s)?{view:e,geometry:t,elevationInfo:o,options:a,queue:s,cache:i,slicePlane:l}:null)),this._getElevationInfoMemoized=p(((e,t)=>({mode:e,offset:t}))),this._getOptionsMemoized=p(((e,t,o)=>({geodesicDistanceThreshold:e,samplingDistance:t,densificationMaxSamples:Math.round(M/o),maxTotalSamples:w}))),this._updateThrottled=_((e=>c(this._update(e))),U)}initialize(){const e=this.viewModel;this._handles.add([f((()=>e.profiles.toArray()),(e=>{this._abortUpdate(),this._paramsPerProfile.clear(),this._handles.remove(I),this._handles.add(e.map((e=>s([e.attach(this.viewModel),e.on("change",(()=>{this._invalidateProfile(e)}))]))),I)}),u),this._updateThrottled,f((()=>this._updateParameters),this._updateThrottled,u)])}destroy(){this._handles.destroy(),this._paramsPerProfile.clear()}get _updateParameters(){const{viewModel:e}=this,t=e.view;return this._getUpdateParametersMemoized(!i(t)||t.stationary,this.viewModel.visibleProfiles,this._generationParameters)}get _generationParameters(){const{view:e,input:t,queue:o,tileCache:s}=this.viewModel;return r(e)||!e.ready?null:this._getGenerationParametersMemoized(e,a(t,(e=>e.geometry)),this._elevationInfo,o,s,this._options,"3d"===e.type?e.slicePlane:null)}get _elevationInfo(){const e=a(this.viewModel.input,(e=>g(e)?null:y(e)));return i(e)?this._getElevationInfoMemoized(e.mode,e.offset):null}get _options(){const e=this.viewModel,t=e.visibleProfiles.length;let o=l(e.minDemResolution,j);return o=parseFloat(o.toFixed(2)),this._getOptionsMemoized(e.geodesicDistanceThreshold,o,t)}async _update({stationary:e,visibleProfiles:t,generationParameters:o}){this._abortUpdate(),e&&(r(o)?this._clearResults():this._updateTask=d((async e=>{this.viewModel.error=null;const s=t.filter((e=>!this._isProfileValid(e,o))),r=z({...o,providers:s},{signal:e});try{for await(const t of r){h(e),this._clearInvalidResults(o);for(let e=0;e{e.result=null}))}})))}_abortUpdate(){this._updateTask=n(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([P({nonNullable:!0})],k.prototype,"viewModel",void 0),e([P()],k.prototype,"_updateParameters",null),e([P()],k.prototype,"_generationParameters",null),e([P()],k.prototype,"_elevationInfo",null),e([P()],k.prototype,"_options",null),k=e([v("esri.widgets.ElevationProfile.ElevationProfileController")],k);export{k as ElevationProfileController};