/*
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/Evented","../../../../../core/has","../../../../../chunks/rbush","./Tile","../../../tiling/TileCoverage","../../../tiling/TileKey"],(function(e,t,s,i,n,o,l){"use strict";const h={added:[],removed:[]},d=new Set,r=new l(0,0,0,0);let c=function(t){function l(e){var n;return(n=t.call(this)||this)._tiles=new Map,n._index=i.rbush(9,s("esri-csp-restrictions")?e=>({minX:e.bounds[0],minY:e.bounds[1],maxX:e.bounds[2],maxY:e.bounds[3]}):[".bounds[0]",".bounds[1]",".bounds[2]",".bounds[3]"]),n.tiles=[],n.tileScheme=e,n}e._inheritsLoose(l,t);var c=l.prototype;return c.destroy=function(){this.clear()},c.clear=function(){this.tiles.length=0,this._tiles.clear(),this._index.clear()},c.has=function(e){return this._tiles.has(e)},c.get=function(e){return this._tiles.get(e)},c.boundsIntersections=function(e){return this._index.search({minX:e[0],minY:e[1],maxX:e[2],maxY:e[3]})},c.updateTiles=function(e){const t={added:[],removed:[]};for(const s of e.added)if(!this.has(s)){const e=new n.Tile(this.tileScheme,s);this._tiles.set(s,e),this._index.insert(e),t.added.push(e)}for(const s of e.removed)if(this.has(s)){const e=this.get(s);this._tiles.delete(s),this._index.remove(e),t.removed.push(e)}this.tiles.length=0,this._tiles.forEach((e=>this.tiles.push(e))),(t.added.length||t.removed.length)&&this.emit("update",t)},c.setViewState=function(e){const t=this.tileScheme.getTileCoverage(e,0);if(!t)return;const{spans:s,lodInfo:i}=t,{level:l}=i;if(s.length>0)for(const{row:o,colFrom:c,colTo:u}of s)for(let e=c;e<=u;e++){const t=r.set(l,o,i.normalizeCol(e),i.getWorldForColumn(e)).id;if(d.add(t),!this.has(t)){const e=new n.Tile(this.tileScheme,t);this._tiles.set(t,e),this._index.insert(e),this.tiles.push(e),h.added.push(e)}}for(let n=this.tiles.length-1;n>=0;n--){const e=this.tiles[n];d.has(e.id)||(this._tiles.delete(e.id),this.tiles.splice(n,1),this._index.remove(e),h.removed.push(e))}(h.added.length||h.removed.length)&&this.emit("update",h),o.pool.release(t),d.clear(),h.added.length=0,h.removed.length=0},l}(t);return c}));
