/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{getOrCreateMapValue as e}from"../../../../../core/MapUtils.js";import{GeohashCell as r}from"../../../../../geohash/GeohashCell.js";import{GeohashTree as t}from"../../../../../geohash/GeohashTree.js";import{getGeohash as o,getGeohashBounds as s}from"../../../../../geohash/geohashUtils.js";import{AAggregateSpatialIndex as i}from"./AAggregateSpatialIndex.js";import{createArcadeEvaluationOptions as n}from"../support/arcadeUtils.js";class a extends i{constructor(e,r){super(e),this._reader=r,this._tree=new t,this.insert(this._reader)}get usedMemory(){return this._tree.usedMemory}forEachInCell(e,r){throw new Error("Not supported.")}insert(e){const r=e.getCursor(),{featureFilter:t,sqlOptions:s,geohashLevel:i,spatialReference:n}=this.options.inner;for(;r.next();){const e=r;if(null!==t&&!t.check(e,s))continue;let a=0,h=0;if("esriGeometryPoint"===e.geometryType)a=e.readXWorldSpace(),h=e.readYWorldSpace();else{const r=e.readCentroidWorldSpace();if(null==r)continue;[a,h]=r.coords}const d=o(a,h,i,n);d&&this._tree.insert(e,d[0],d[1],i)}}put(e,r){throw new Error("Not supported.")}putBounded(t,o,i,a){const{arcadeContextInfo:h,scale:d,geohashLevel:l,spatialReference:c,fields:p}=this.options.inner,u=n(d,h),f=s(i,a,l,c);null!=f&&this._tree.forEachInBounds(f,s=>{let i=null;for(const n of s.indices){this._reader.setIndex(n);const a=this._reader.getObjectId();o.has(a)||(i??=e(t,s.id,()=>r.create(p,s.xNode,s.yNode,s.depth)),i.insert(this._reader,l,c,u),o.add(a))}})}}export{a as GeohashSpatialIndex};
