/*
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","../../../../../core/Error","../../../../../core/Logger","../../../../../core/maybe","../../../../../geometry/support/aaBoundingRect","../../../../../geometry/support/boundsUtils","../../../../../layers/graphics/data/spatialQuerySupport","../../../../../layers/graphics/data/timeSupport","../../../../../layers/graphics/data/utils","../../../../../rest/support/Query","../FeatureStore2D","./whereUtils"],(function(t,e,i,r,s,n,o,a,h,l,u,c){"use strict";const d=i.getLogger("esri.views.2d.layers.features.controllers.FeatureFilter"),y=1,f=2;let _=function(){function i(t){this._geometryBounds=s.create(),this._idToVisibility=new Map,this._serviceInfo=t}var _=i.prototype;return _.check=function(t){return this._applyFilter(t)},_.clear=function(){const t=this._resetAllHiddenIds();return this.update(),{show:t,hide:[]}},_.invalidate=function(){this._idToVisibility.forEach(((t,e)=>{this._idToVisibility.set(e,0)}))},_.setKnownIds=function(t){for(const e of t)this._idToVisibility.set(e,y)},_.setTrue=function(t){const e=[],i=[],r=new Set(t);return this._idToVisibility.forEach(((t,s)=>{const n=!!(this._idToVisibility.get(s)&y),o=r.has(s);!n&&o?e.push(s):n&&!o&&i.push(s),this._idToVisibility.set(s,o?y|f:0)})),{show:e,hide:i}},_.createQuery=function(){const{geometry:t,spatialRel:e,where:i,timeExtent:r,objectIds:s}=this;return l.fromJSON({geometry:t,spatialRel:e,where:i,timeExtent:r,objectIds:s})},_.update=function(){var e=t._asyncToGenerator((function*(t,e){this._hash=JSON.stringify(t);const i=yield h.normalizeQueryLike(t,null,e);yield Promise.all([this._setGeometryFilter(i),this._setIdFilter(i),this._setAttributeFilter(i),this._setTimeFilter(i)])}));function i(t,i){return e.apply(this,arguments)}return i}(),_._setAttributeFilter=function(){var e=t._asyncToGenerator((function*(t){if(!t||!t.where)return this._clause=null,void(this.where=null);this._clause=yield c.createWhereClause(t.where,this._serviceInfo.fieldsIndex),this.where=t.where}));function i(t){return e.apply(this,arguments)}return i}(),_._setIdFilter=function(t){this._idsToShow=t&&t.objectIds&&new Set(t.objectIds),this._idsToHide=t&&t.hiddenIds&&new Set(t.hiddenIds),this.objectIds=t&&t.objectIds},_._setGeometryFilter=function(){var e=t._asyncToGenerator((function*(t){if(!t||!t.geometry)return this._spatialQueryOperator=null,this.geometry=null,void(this.spatialRel=null);const e=t.geometry,i=t.spatialRel||"esriSpatialRelIntersects",r=yield o.getSpatialQueryOperator(i,e,this._serviceInfo.geometryType,this._serviceInfo.hasZ,this._serviceInfo.hasM);n.getBoundsXY(this._geometryBounds,e),this._spatialQueryOperator=r,this.geometry=e,this.spatialRel=i}));function i(t){return e.apply(this,arguments)}return i}(),_._setTimeFilter=function(t){if(this.timeExtent=this._timeOperator=null,t&&t.timeExtent)if(this._serviceInfo.timeInfo)this.timeExtent=t.timeExtent,this._timeOperator=a.getTimeOperator(this._serviceInfo.timeInfo,t.timeExtent,u.featureAdapter);else{const i=new e("feature-layer-view:time-filter-not-available","Unable to apply time filter, as layer doesn't have time metadata.",t.timeExtent);d.error(i)}},_._applyFilter=function(t){return this._filterByGeometry(t)&&this._filterById(t)&&this._filterByTime(t)&&this._filterByExpression(t)},_._filterByExpression=function(t){return!this.where||this._clause(t)},_._filterById=function(t){return(!this._idsToHide||!this._idsToHide.size||!this._idsToHide.has(t.getObjectId()))&&(!this._idsToShow||!this._idsToShow.size||this._idsToShow.has(t.getObjectId()))},_._filterByGeometry=function(t){if(!this.geometry)return!0;const e=t.readHydratedGeometry();return!!e&&this._spatialQueryOperator(e)},_._filterByTime=function(t){return!!r.isNone(this._timeOperator)||this._timeOperator(t)},_._resetAllHiddenIds=function(){const t=[];return this._idToVisibility.forEach(((e,i)=>{e&y||(this._idToVisibility.set(i,y),t.push(i))})),t},t._createClass(i,[{key:"hash",get:function(){return this._hash}}]),i}();return _}));
