/*
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/Handles","../../../../core/Logger","../../../../core/maybe","../../../../core/ObservableChangesType","../../../../core/promiseUtils","../../../../core/reactiveUtils","../../../../core/accessorSupport/decorators/property","../../../../core/arrayUtils","../../../../core/has","../../../../core/accessorSupport/ensureType","../../../../core/accessorSupport/decorators/subclass","../../../../layers/support/FeatureFilter","../../../../layers/support/floorFilterUtils","./enums","./QueryEngine","../../../support/Scheduler"],(function(i,e,t,r,s,l,n,o,a,h,c,u,y,_,d,p,g,F,b,V){"use strict";const f=l.getLogger("esri.views.3d.layers.graphics.Graphics3DFilterVisibility");i.Graphics3DFilterVisibility=function(i){function t(...e){var t;return(t=i.call(this,...e)||this)._dirty=!1,t._querying=!1,t._handles=new s,t}e._inheritsLoose(t,i);var r=t.prototype;return r.setup=function(i,e){this._layerView=i,this._core=e,this._objectIdField=i.layer.objectIdField,this._queryEngine=new b.QueryEngine({layerView:this._layerView,priority:V.TaskPriority.FILTER_VISIBILITY});const t=this._layerView.view.resourceController.scheduler;this._handles.add(t.registerTask(V.TaskPriority.FILTER_VISIBILITY,this)),this._handles.add(h.on((()=>{var i;return null==(i=e.owner)?void 0:i.loadedGraphics}),"after-changes",(i=>this._graphicsChanged(i)),{onListenerAdd:()=>this.dirty=!0})),this.filterChanged()},r.destroy=function(){this._handles.destroy(),this._handles=null,this.clear(),this._layerView=null,this._core=null},r.clear=function(){this._queryEngine.clear()&&(this._core.modifyGraphics3DGraphicVisibilities((i=>i.clearVisibilityFlag(F.VisibilityFlag.FILTER))),this._queryResult=null,this._querying=!1),this.dirty=!1},r._graphicsChanged=function(i){this._queryEngine&&0==(i.type&o.ObservableChangesType.ADD)||(this.dirty=!0)},r.updateVisibility=function(i){this.active&&(i.hasVisibilityFlag(F.VisibilityFlag.FILTER,F.VisibilityGroup.GRAPHIC)||i.setVisibilityFlag(F.VisibilityFlag.FILTER,!1,F.VisibilityGroup.GRAPHIC),this.dirty=!0)},r.filterChanged=function(){const i=this._recomputeFilter();i!==this._featureFilter&&(this._featureFilter=i,this.dirty=!0);const e="layerFilter"in this._layerView?this._layerView.layerFilter:null;e!==this._sceneFilter&&(this._sceneFilter=e,this.dirty=!0)},r._recomputeFilter=function(){const i="filter"in this._layerView?this._layerView.filter:null,e="timeExtent"in this._layerView?this._layerView.timeExtent:null,t=g.getFloorFilterClause(this._layerView);if(n.isNone(e)&&n.isNone(t))return i;const r=n.isSome(i)?i.clone():new p;if(n.isSome(e)&&(r.timeExtent=n.isSome(r.timeExtent)?r.timeExtent.intersection(e):e),n.isSome(t)){const i=null==r.where||""===r.where;r.where=i?t:`(${r.where}) AND (${t})`}return r},r.runTask=function(i){if(!this.active)return void this.clear();!this._dirty||this._querying||i.done||(this._querying=!0,this.dirty=!1,this._queryEngine.executeQueryForIdSet(this._featureFilter,this._sceneFilter).then((i=>{this._queryResult=i,this._querying=!1})).catch((i=>{if(!a.isAbortError(i)){f.warn("FeatureFilter query failed: "+i,{error:i});const e=new Set;this._core.graphics3DGraphics.forEach((i=>e.add(this._getFeatureId(i.graphic)))),this._queryResult=e,this._querying=!1}})),i.madeProgress());const e=this._queryResult;n.isSome(e)&&!i.done&&(this._core.modifyGraphics3DGraphicVisibilities((t=>{if(i.done)return!1;const r=e.has(this._getFeatureId(t.graphic));return!!t.setVisibilityFlag(F.VisibilityFlag.FILTER,r,F.VisibilityGroup.GRAPHIC)&&(i.madeProgress(),!0)})),i.done||(this._queryResult=null))},r._getFeatureId=function(i){return null==i.objectId?i.attributes[this._objectIdField]:i.objectId},e._createClass(t,[{key:"updating",get:function(){return this._dirty||this._querying||n.isSome(this._queryResult)}},{key:"active",get:function(){return(this._featureFilter||this._sceneFilter)&&this._core.graphics3DGraphics.size>0}},{key:"running",get:function(){return this._dirty&&!this._querying||n.isSome(this._queryResult)}},{key:"dirty",set:function(i){this._dirty=i}}]),t}(r),t.__decorate([c.property({readOnly:!0})],i.Graphics3DFilterVisibility.prototype,"updating",null),t.__decorate([c.property({readOnly:!0})],i.Graphics3DFilterVisibility.prototype,"running",null),t.__decorate([c.property()],i.Graphics3DFilterVisibility.prototype,"_dirty",void 0),t.__decorate([c.property()],i.Graphics3DFilterVisibility.prototype,"_querying",void 0),t.__decorate([c.property()],i.Graphics3DFilterVisibility.prototype,"_queryResult",void 0),i.Graphics3DFilterVisibility=t.__decorate([d.subclass("esri.views.3d.layers.graphics.Graphics3DFilterVisibility")],i.Graphics3DFilterVisibility),Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
