/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../chunks/_rollupPluginBabelHelpers","../../../../chunks/tslib.es6","../../../../core/Accessor","../../../../core/Evented","../../../../core/Handles","../../../../core/Logger","../../../../core/maybe","../../../../core/unitUtils","../../../../core/accessorSupport/decorators/property","../../../../core/arrayUtils","../../../../core/has","../../../../core/accessorSupport/ensureType","../../../../core/accessorSupport/decorators/subclass","../../../../chunks/vec3","../../../../chunks/vec3f64","../../../../geometry/support/aaBoundingBox","../../../../geometry/support/aaBoundingRect","../../../../symbols/support/unitConversionUtils","../../webgl-engine/lib/Intersector","../../webgl-engine/lib/IntersectorInterfaces"],(function(e,t,r,o,n,a,i,s,l,c,d,p,u,h,v,y,m,g,f,b,_){"use strict";const x=i.getLogger("esri.views.3d.layers.support.StageLayerElevationProvider"),E=1;e.StageLayerElevationProvider=function(e){function r(t){var r;return(r=e.call(this,t)||this).elevationOffset=0,r._layerHandes=new a,r}t._inheritsLoose(r,e);var o=r.prototype;return o.initialize=function(){this.renderCoordsHelper=this.view.renderCoordsHelper,this.intersectLayers=[this.stageLayer],this.intersector=b.newIntersector(this.view.state.viewingMode),this.intersector.options.store=_.StoreResults.MIN;const e=this._computeLayerExtent(this.stageLayer);this.zmin=e[2],this.zmax=e[5];const t=this.stageLayer.events;this._layerHandes.add([t.on("layerObjectAdded",(e=>this._objectChanged(e.object))),t.on("layerObjectRemoved",(e=>this._objectChanged(e.object))),t.on("objectGeometryAdded",(e=>this._objectChanged(e.object))),t.on("objectGeometryRemoved",(e=>this._objectChanged(e.object))),t.on("vertexAttrsUpdated",(e=>this._objectChanged(e.object))),t.on("objectTransformation",(e=>this._objectChanged(e)))])},o.dispose=function(){this._layerHandes.destroy()},o.elevationInfoChanged=function(){const e=null!=this.layer?this.layer.elevationInfo:null;if(null!=e&&"on-the-ground"!==e.mode){const t=l.getMetersPerVerticalUnitForSR(this.layer.spatialReference),r=f.getMetersPerUnit(e.unit);this.elevationOffset=s.unwrapOr(e.offset,0)*r/t}else this.elevationOffset=0},o.getElevation=function(e,t,r,o){if(C[0]=e,C[1]=t,C[2]=r,!this.renderCoordsHelper.toRenderCoords(C,o,C))return x.error("could not project point for elevation alignment"),null;const n=this.elevationOffset,a=this.zmin+n,i=this.zmax+n;this.renderCoordsHelper.setAltitude(R,i,C),this.renderCoordsHelper.setAltitude(B,a,C);const s=e=>e.metadata&&e.metadata.isElevationSource;return this.intersector.reset(R,B,null),this.intersector.intersect(this.intersectLayers,null,E,null,s),this.intersector.results.min.getIntersectionPoint(C)?this.renderCoordsHelper.getAltitude(C):null},o._objectChanged=function(e){var t;if(null==(t=e.metadata)||!t.isElevationSource||!this.spatialReference)return;m.empty(S),e.metadata.lastValidElevationBB.isEmpty()||this._expandExtent(e.metadata.lastValidElevationBB.min,e.metadata.lastValidElevationBB.max,S);const r=e.boundingVolumeWorldSpace.min,o=e.boundingVolumeWorldSpace.max;this._expandExtent(r,o,S),m.toRect(S,j),this.zmin=Math.min(this.zmin,S[2]),this.zmax=Math.max(this.zmax,S[5]),L.extent=j,L.spatialReference=this.spatialReference,this.emit("elevation-change",L),v.copy(e.metadata.lastValidElevationBB.min,r),v.copy(e.metadata.lastValidElevationBB.max,o)},o._computeLayerExtent=function(e){return m.empty(S),e.objects.forAll((e=>this._expandExtent(e.boundingVolumeWorldSpace.min,e.boundingVolumeWorldSpace.max,S))),S},o._expandExtent=function(e,t,r){for(let o=0;o<8;++o)C[0]=1&o?e[0]:t[0],C[1]=2&o?e[1]:t[1],C[2]=4&o?e[2]:t[2],this.renderCoordsHelper.fromRenderCoords(C,C,this.spatialReference),m.expandWithVec3(r,C);return r},r}(n.EventedMixin(o)),r.__decorate([c.property({constructOnly:!0})],e.StageLayerElevationProvider.prototype,"layer",void 0),r.__decorate([c.property({constructOnly:!0})],e.StageLayerElevationProvider.prototype,"stageLayer",void 0),r.__decorate([c.property({constructOnly:!0})],e.StageLayerElevationProvider.prototype,"view",void 0),r.__decorate([c.property({readOnly:!0,aliasOf:"view.spatialReference"})],e.StageLayerElevationProvider.prototype,"spatialReference",void 0),e.StageLayerElevationProvider=r.__decorate([h.subclass("esri.views.3d.layers.support.StageLayerElevationProvider")],e.StageLayerElevationProvider);const S=m.empty(),j=g.empty(),L={spatialReference:null,extent:j,context:"scene"},C=y.create(),R=y.create(),B=y.create();Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
