/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{neverAbortedSignal as e}from"../../arcadeEnvironment.js";import{throwIfCancelled as t}from"../support/errorsupport.js";import{FeatureSet as s}from"../support/FeatureSet.js";import{defaultMaxRecords as r}from"../support/shared.js";import{combine as l}from"../support/sqlUtils.js";import a from"../../../core/sql/WhereClause.js";class i extends s{constructor(e){super(),this.declaredClass="esri.arcade.featureset.actions.AttributeFilter",this._maxProcessing=1e3,this._parent=e.parentfeatureset,e.whereclause instanceof a?(this._whereClause=e.whereclause,this._whereClauseFunction=null):(this._whereClauseFunction=e.whereclause,this._whereClause=null)}_initialiseFeatureSet(){this.fields=this._parent.fields.slice(),this.geometryType=this._parent.geometryType,this.objectIdField=this._parent.objectIdField,this.globalIdField=this._parent.globalIdField,this.spatialReference=this._parent.spatialReference,this.hasM=this._parent.hasM,this.hasZ=this._parent.hasZ,this.typeIdField=this._parent.typeIdField,this.types=this._parent.types,this.subtypeField=this._parent.subtypeField,this.subtypes=this._parent.subtypes}async _queryAll(){return(await this.query({abortSignal:e})).features}async query(e){let s=e.where;null!==this._whereClauseFunction||(null!=s?null!==this._whereClause&&(s=l(this._whereClause,s)):s=this._whereClause),await this._ensureLoaded();const r=await this._parent.query({...e,where:s});return t(e.abortSignal),r.filterApplied&&null==this._whereClauseFunction?r:{...r,features:this._applyFilter(r.features,e.abortSignal)}}async*_applyFilter(e,s){for await(const r of e)if(t(s),null!=this._whereClause){const e=r.filter(e=>this._whereClause.testFeature(e));e.length>0&&(yield e)}else if(null!=this._whereClauseFunction){const e=[];for(const t of r)await this._whereClauseFunction(t)&&e.push(t);e.length>0&&(yield e)}else yield r}async queryStat(e){if(null!==this._whereClauseFunction)return null==e.where&&null==e.spatialFilter?this._manualStat(e.stat,e.field,e.limit??r,e.abortSignal):{calculated:!1};let t=this._whereClause;null!=e.where&&null!==this._whereClause&&(t=l(this._whereClause,e.where));const s=await this._parent.queryStat({...e,where:t});return s.calculated?s:null==e.where&&null==e.spatialFilter?this._manualStat(e.stat,e.field,e.limit??r,e.abortSignal):{calculated:!1}}async canQueryAggregate(e){if(null!==this._whereClauseFunction)return!1;let t=e.where;return null!=t?null!==this._whereClause&&(t=l(this._whereClause,t)):t=this._whereClause,this._parent.canQueryAggregate({...e,where:t})}async queryAggregate(e){let t=e.where;return null!=t?null!==this._whereClause&&(t=l(this._whereClause,t)):t=this._whereClause,this._parent.queryAggregate({...e,where:t})}getFieldsIndex(){return this._parent.getFieldsIndex()}}export{i as default};
