/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{__decorate as e}from"tslib";import{EventedAccessor as t}from"../../../core/Evented.js";import{throwIfAbortError as s}from"../../../core/promiseUtils.js";import{property as r,subclass as i}from"../../../core/accessorSupport/decorators.js";import n from"../../../geometry/SpatialReference.js";import{equals as a}from"../../../geometry/support/spatialReferenceUtils.js";import{ElevationQuery as o}from"../layers/graphics/ElevationQuery.js";import{ElevationRange as l}from"./ElevationRange.js";let c=class extends t{get spatialReference(){return this.view.basemapTerrain?.spatialReference??this.view.spatialReference}constructor(e){super(e),this._providers=[new Array,new Array,new Array],this._lastResult=null,this._cacheEnabled=!1}reset(){this._cachedQuery?.destroy({completeTasks:!0}),this._cachedQuery=this._lastResult=null}enableCache(e){e||(this._lastResult=null),this._cacheEnabled=e}getElevation(e,t,s,r,i){const n=this._cacheEnabled&&this._lastResult;if(n&&e===n.x&&t===n.y&&s===n.z&&a(r,n.spatialReference)&&i===n.queryContext)return n.result;let o=this._updateElevation(null,1,e,t,s,r,i);return"im"!==i&&null==o&&(o=this._updateElevation(null,0,e,t,s,r,i)),"scene"===i&&(o=this._updateElevation(o,2,e,t,s,r,i)),this._cacheEnabled&&(this._lastResult=new u(e,t,s,r,i,o)),o}getSphereElevationRange(e,t,s){let r=null;const i=i=>{for(const n of this._providers[i])if(n.getSphereElevationRange){const i=n.getSphereElevationRange(e,t,s);null!=i&&(r??=new l,r.expandElevationRangeValues(i.minElevation,i.maxElevation))}};return"im"!==s&&i(0),i(1),"scene"===s&&i(2),r}getRootElevationRange(){return this._providers.reduce((e,t)=>t.reduce((e,t)=>{const s=t.getRootElevationRange?.();return null!=s&&(e??=new l,e.expandElevationRangeValues(s.minElevation,s.maxElevation)),e},e),null)}async queryElevation(e,t,r,i,n,a=null,o=0){const l=this._getElevationQuery(i);try{const s=await l.queryElevation(e,t,a,o);return"scene"===n?this._updateElevation(s,2,e,t,r,i,n):s}catch(c){return s(c),this.getElevation(e,t,r,i,n)}}register(e,t){this.addHandles(t.on("elevation-change",e=>this.emit("elevation-change",e)),t),this._providers[e].push(t),this.reset()}unregister(e){this.removeHandles(e);for(const t of this._providers){const s=t.indexOf(e);s>-1&&t.splice(s,1)}this.reset()}_getElevationQuery(e=this.view.spatialReference){const t=this._cachedQuery;if(null!=t&&a(e,t.spatialReference))return t;t?.destroy({completeTasks:!0});const{wkid:s,wkt:r,wkt2:i,latestWkid:l}=e,c=new o(this.view.resourceController.scheduler,new n({wkid:s,wkt:r,wkt2:i,latestWkid:l}),()=>this.view.map?.ground,{maximumAutoTileRequests:4});return this._cachedQuery=c,c}_updateElevation(e,t,s,r,i,n,a){return this._providers[t].reduce((e,t)=>{const o=t.getElevation(s,r,i,n,a);return null==o?e:Math.max(o,e??o)},e)}};e([r({constructOnly:!0})],c.prototype,"view",void 0),e([r()],c.prototype,"spatialReference",null),c=e([i("esri.views.3d.support.CombinedElevationProvider")],c);class u{constructor(e,t,s,r,i,n){this.x=e,this.y=t,this.z=s,this.spatialReference=r,this.queryContext=i,this.result=n}}export{c as CombinedElevationProvider};
