/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{baseObjectMemory as t,estimateNumberMemory as e}from"../core/memoryEstimations.js";import{getGeohash as o,decodeGeohashXY as s}from"./geohashUtils.js";import r from"../geometry/SpatialReference.js";import{convertFromPolygon as i,quantizeOptimizedGeometry as h,convertFromPoint as a}from"../layers/graphics/featureConversionUtils.js";import{project as n}from"../layers/graphics/data/projectionSupport.js";import{AAggregateCell as l}from"../views/2d/layers/features/aggregation/AAggregateCell.js";import{AccumulatedStatistics as d}from"../views/2d/layers/features/aggregation/AccumulatedStatistics.js";const c=32;class _ extends l{static create(t,e,o,s){const r=d.create(t),i=new Array(c);for(let h=0;h<i.length;h++)i[h]=null;return new _(r,e,o,s,i)}constructor(t,e,o,s,r){super(t),this.xNode=e,this.yNode=o,this.depth=s,this.children=r,this._objectIds=[new Set],this._count=0,this._xWorldTotal=0,this._yWorldTotal=0,this._xGeohashTotal=0,this._yGeohashTotal=0,this.next=null}static get estimatedMemory(){let o=0;return o+=2*t,o+=e*c,o+=d.estimatedMemory,o}get id(){return`${this.xNode}.${this.yNode}`}get containedObjectIds(){return this._objectIds}get count(){return this._count}clone(){const t=new _(this._statistics.clone(),this.xNode,this.yNode,this.depth,this.children);return t._count=this._count,t._xWorldTotal=this._xWorldTotal,t._yWorldTotal=this._yWorldTotal,t._xGeohashTotal=this._xGeohashTotal,t._yGeohashTotal=this._yGeohashTotal,t.next=this.next,t._objectIds=this._objectIds.map(t=>new Set(t)),t}insert(t,e,s,r){let i=0,h=0;if("esriGeometryPoint"===t.geometryType)i=t.readXWorldSpace(),h=t.readYWorldSpace();else{const e=t.readCentroidWorldSpace();if(null==e)return;[i,h]=e.coords}const a=o(i,h,e,s);a&&(this._count+=1,this._xWorldTotal+=i,this._yWorldTotal+=h,this._xGeohashTotal+=a[0],this._yGeohashTotal+=a[1],this._statistics.insert(t,r),this._objectIds[0].add(t.getObjectId()))}merge(t){0!==t._count&&(this._count+=t._count,this._xWorldTotal+=t._xWorldTotal,this._yWorldTotal+=t._yWorldTotal,this._xGeohashTotal+=t._xWorldTotal,this._yGeohashTotal+=t._yWorldTotal,this._statistics.merge(t._statistics),this._objectIds.push(...t._objectIds))}getCentroid(t){throw new Error("getCentroid not supported for GeohashNode")}getGeometry(t,e){const o=this._getLngLatBounds(),[s,a,l,d]=o,c=n({rings:[[[s,a],[s,d],[l,d],[l,a],[s,a]]]},r.WGS84,t),_=i(c);return null!=e?h(_,"esriGeometryPolygon",e,!1,!1):_}getGeometricCentroid(t,e){const o=this._getLngLatBounds(),[s,i,l,d]=o,c=n({x:(s+l)/2,y:(i+d)/2},r.WGS84,t),_=a(c);return null!=e?h(_,"esriGeometryPoint",e,!1,!1):_}getAttributes(){const t={aggregateId:this.id};for(const e of this._statistics.values())t[e.field.name]=e.value;return t.aggregateCount=this._count,t}find(t,e,o,s,r,i){if(s>=o)return this;const h=1-s%2,a=3*h+2*(1-h),n=2*h+3*(1-h),l=30-r-a,d=30-i-n,c=((t&7*h+3*(1-h)<<l)>>l)+((e&3*h+7*(1-h)<<d)>>d)*(8*h+4*(1-h)),_=this.children[c];return null==_?null:_.find(t,e,o,s+1,r+a,i+n)}_getLngLatBounds(){const t=this.depth,e=Math.ceil(t/2),o=Math.floor(t/2),r=30-(3*e+2*o),i=30-(2*e+3*o),h=this.xNode<<r,a=this.yNode<<i;return s({geohashX:h,geohashY:a},this.depth)}}export{_ as GeohashCell};
