/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{estimateNumberMemory as e}from"../../../../../core/memoryEstimations.js";import{getMetersPerUnitForSR as t,inchesPerMeter as s}from"../../../../../core/units.js";import{AAggregateSpatialIndex as i}from"./AAggregateSpatialIndex.js";import{GridCell as l}from"./GridCell.js";const o=96;function r(e,i){return t(e)*s*o/i}class n extends i{constructor(e){super(e),this._cells=new Map,this._pixelsPerMapUnit=r(e.spatialReference,e.scale)}get usedMemory(){const t=this._cells.values().next().value;return t?(e+t.usedMemory)*this._cells.size:0}put(e){for(const t of this._cells.values()){const s=e.get(t.id);s?s.merge(t):e.set(t.id,t.clone())}}putBounded(e,t,s){const i=[t.xmin,t.ymin,t.xmax,t.ymax],[l,o,r,n]=i,c=Math.floor(l*this._pixelsPerMapUnit/this._options.cellSize),p=Math.floor(o*this._pixelsPerMapUnit/this._options.cellSize),a=Math.ceil(r*this._pixelsPerMapUnit/this._options.cellSize),h=Math.ceil(n*this._pixelsPerMapUnit/this._options.cellSize);for(let _=p;_<=h;_++)for(let t=c;t<=a;t++){const s=`${t}.${_}`,i=this._cells.get(s);if(!i)continue;const l=e.get(i.id);l?i&&!e.has(i.id)&&l.merge(i):e.set(i.id,i.clone())}}_insert(e,t,s,i){const l=t*this._pixelsPerMapUnit,o=s*this._pixelsPerMapUnit,r=Math.floor(l/this._options.cellSize),n=Math.floor(o/this._options.cellSize);this._getCellOrCreate(r,n).insert(e,i,t,s)}_getCellOrCreate(e,t){const s=l.createId(e,t);let i=this._cells.get(s);if(!i){const o=1*this._options.cellSize/this._pixelsPerMapUnit;i=l.create(e,t,this._options.fields,o),this._cells.set(s,i)}return i}}export{n as GridSpatialIndex,r as pixelsPerMapUnit};
