/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../chunks/_rollupPluginBabelHelpers","../../../../chunks/tslib.es6","../../../../core/Accessor","../../../../core/Evented","../../../../core/Handles","../../../../core/maybe","../../../../core/reactiveUtils","../../../../core/accessorSupport/decorators/property","../../../../core/arrayUtils","../../../../core/has","../../../../core/accessorSupport/ensureType","../../../../core/accessorSupport/decorators/subclass","./ExtentSet","../../webgl-engine/lib/basicInterfaces","../../../support/Scheduler"],(function(e,t,i,r,s,n,a,o,h,c,p,d,l,u,g){"use strict";let y=function(t){function i(){var e;return(e=t.apply(this,arguments)||this).dirtyExtents=new l.ExtentSet,e.globalDirty=!1,e.averageExtentUpdateSize=0,e.dirtyGraphicsSet=new Set,e.handles=new s,e.updateElevation=!1,e.graphicsCoreOwner=null,e.graphicsCore=null,e.events=new r,e}e._inheritsLoose(i,t);var o=i.prototype;return o.setup=function(e,t,i,r){this.graphicsCoreOwner=e,this.queryGraphicUIDsInExtent=t,this.graphicsCore=i,this.elevationProvider=r;const s=this.graphicsCoreOwner.view.resourceController.scheduler;this.handles.add([r.on("elevation-change",(e=>this._elevationChanged(e))),a.watch((()=>this.graphicsCoreOwner.suspended),(()=>this._suspendedChange())),s.registerTask(g.TaskPriority.ELEVATION_ALIGNMENT,this)])},o.destroy=function(){this.dirtyGraphicsSet.clear(),this.handles.destroy(),this.handles=null,this.graphicsCoreOwner=null,this.graphicsCore=null,this.queryGraphicUIDsInExtent=null},o.clear=function(){this.dirtyGraphicsSet.clear(),this.notifyChange("updating")},o._suspendedChange=function(){!0===this.graphicsCoreOwner.suspended?this.updateElevation=!1:!1===this.graphicsCoreOwner.suspended&&this.updateElevation&&(this.globalDirty=!0,this.notifyChange("updating"))},o.elevationInfoChange=function(){this.globalDirty=!0,this.notifyChange("updating")},o.runTask=function(e){for(this.globalDirty&&(this._markAllGraphicsElevationDirty(),this.globalDirty=!1,e.madeProgress()),e.run((()=>this.dirtyExtents.merge(e)));this.running&&!e.done;)this._updateDirtyGraphics(e),this._updateDirtyExtents(e);this.graphicsCoreOwner.view.deconflictor.setDirty(),this.notifyChange("updating")},o._updateDirtyGraphics=function(e){const t=this.graphicsCoreOwner.view.renderCoordsHelper,i=this.graphicsCore.effectiveUpdatePolicy===u.UpdatePolicy.ASYNC;for(const r of this.dirtyGraphicsSet.keys()){const s=this.graphicsCore.getGraphics3DGraphicById(r);if(this.dirtyGraphicsSet.delete(r),n.isSome(s)&&(s.alignWithElevation(this.elevationProvider,t,i),e.madeProgress()),e.done)return}},o._updateDirtyExtents=function(e){for(;!this.dirtyExtents.empty&&!e.done;){const t=this.dirtyExtents.pop(),i=this.elevationProvider.spatialReference;this.events.emit("invalidate-elevation",{extent:t,spatialReference:i});const r=this.dirtyGraphicsSet.size;this.queryGraphicUIDsInExtent(t,i,(e=>{const t=this.graphicsCore.getGraphics3DGraphicById(e);n.isSome(t)&&t.needsElevationUpdates()&&this.dirtyGraphicsSet.add(e)})),this.averageExtentUpdateSize=.1*(this.dirtyGraphicsSet.size-r)+.9*this.averageExtentUpdateSize,e.madeProgress()}},o._markAllGraphicsElevationDirty=function(){this.dirtyExtents.clear(),this.dirtyGraphicsSet.clear(),this.graphicsCore.graphics3DGraphics.forEach(((e,t)=>this.dirtyGraphicsSet.add(t)))},o._elevationChanged=function(e){if("scene"===e.context&&(!this.graphicsCore.layer.elevationInfo||"relative-to-scene"!==this.graphicsCore.layer.elevationInfo.mode))return;const{extent:t,spatialReference:i}=e;if(this.graphicsCoreOwner.suspended){if(!this.updateElevation){const e=this.graphicsCore.computedExtent;e&&t[2]>e.xmin&&t[0]<e.xmax&&t[3]>e.ymin&&t[1]<e.ymax&&(this.updateElevation=!0)}this.events.emit("invalidate-elevation",{extent:t,spatialReference:i})}else t[0]===-1/0?this.globalDirty=!0:this.dirtyExtents.add(t),this.notifyChange("updating")},e._createClass(i,[{key:"updating",get:function(){return this.running}},{key:"running",get:function(){return this.dirtyGraphicsSet.size>0||this.dirtyExtents&&!this.dirtyExtents.empty||this.globalDirty}},{key:"updatingRemaining",get:function(){return this.dirtyGraphicsSet.size+this.dirtyExtents.size*this.averageExtentUpdateSize}}]),i}(i);t.__decorate([o.property({readOnly:!0})],y.prototype,"updating",null),t.__decorate([o.property({readOnly:!0})],y.prototype,"updatingRemaining",null),y=t.__decorate([d.subclass("esri.views.3d.layers.graphics.Graphics3DElevationAlignment")],y);return y}));
