/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{unique as e}from"../../core/arrayUtils.js";import{contains as t}from"../../geometry/support/aaBoundingRect.js";import{TileKey as i}from"./TileKey.js";class s{constructor(e,t){this.layer=e,this.parameters=t}}class l extends s{constructor(e,t,i){super(e,i),this.outSpatialReference=t,this.type="geometry"}selectTilesAtLOD(e){if(e<0)this.geometry.coordinates.forEach(e=>e.tile=null);else{const{tileInfo:t,tilemapCache:i}=this.layer,s=a(t,i)[e].level;this.geometry.coordinates.forEach(e=>e.tile=t.tileAt(s,e.x,e.y))}}allElevationTilesFetched(){return!this.geometry.coordinates.some(e=>!e.elevationTile)}clearElevationTiles(){for(const e of this.geometry.coordinates)e.elevationTile!==this.outsideExtentTile&&(e.elevationTile=null)}populateElevationTiles(e){for(const t of this.geometry.coordinates)!t.elevationTile&&t.tile?.id&&(t.elevationTile=e[t.tile.id])}remapTiles(e){for(const t of this.geometry.coordinates){const i=t.tile?.id;t.tile=i?e[i]:null}}getTilesToFetch(){return e(this.geometry.coordinates.filter(({tile:e,elevationTile:t})=>e?.id&&!t),(e,t)=>e.tile?.id===t.tile?.id).map(({tile:e})=>e)}forEachTileToFetch(e){for(const t of this.geometry.coordinates)t.tile&&!t.elevationTile&&e(t.tile,()=>t.tile=null)}}class n extends s{constructor(e,t,i,s){super(e,i),this.maskExtents=s,this.type="extent",this.elevationTiles=[],this._candidateTiles=[],this._fetchedCandidates=new Set,this.extent=t.clone().intersection(e.fullExtent)}selectTilesAtLOD(e,t){const i=this._maximumLodForRequests(t),s=Math.min(i,e);s<0?this._candidateTiles.length=0:this._selectCandidateTilesCoveringExtentAt(s)}_maximumLodForRequests(e){const{tileInfo:t,tilemapCache:i}=this.layer,s=a(t,i);if(!e)return s.length-1;const l=this.extent;if(null==l)return-1;for(let n=s.length-1;n>=0;n--){const i=s[n],o=i.resolution*t.size[0],a=i.resolution*t.size[1];if(Math.ceil(l.width/o)*Math.ceil(l.height/a)<=e)return n}return-1}allElevationTilesFetched(){return this._candidateTiles.length===this.elevationTiles.length}clearElevationTiles(){this.elevationTiles.length=0,this._fetchedCandidates.clear()}populateElevationTiles(e){for(const t of this._candidateTiles){const i=t.id&&e[t.id];i&&(this._fetchedCandidates.add(t),this.elevationTiles.push(i))}}remapTiles(e){this._candidateTiles=r(this._candidateTiles.map(t=>e[t.id]))}getTilesToFetch(){return this._candidateTiles}forEachTileToFetch(e,t){const i=this._candidateTiles;this._candidateTiles=[],i.forEach(i=>{if(this._fetchedCandidates.has(i))return void t?.(i);let s=!1;e(i,()=>s=!0),s?t?.(i):this._candidateTiles.push(i)}),this._candidateTiles=r(this._candidateTiles,t)}_selectCandidateTilesCoveringExtentAt(e){this._candidateTiles.length=0;const t=this.extent;if(null==t)return;const{tileInfo:s,tilemapCache:l}=this.layer,n=a(s,l)[e],o=s.tileAt(n.level,t.xmin,t.ymin),r=o.extent,c=n.resolution*s.size[0],h=n.resolution*s.size[1],d=Math.ceil((t.xmax-r[0])/c),f=Math.ceil((t.ymax-r[1])/h);for(let a=0;a<f;a++)for(let e=0;e<d;e++){const t=new i(o.level,o.row-a,o.col+e);s.updateTileInfo(t),this._tileIsMasked(t)||this._candidateTiles.push(t)}}_tileIsMasked(e){return this.maskExtents?.some(i=>t(i,e.extent))??!1}}function o(e){return null!=e?.tileInfo}function a(e,t){const i=e.lods;if(o(t)){const{effectiveMinLOD:e,effectiveMaxLOD:s}=t;return i.filter(t=>t.level>=e&&t.level<=s)}return i}function r(e,i){const s={},l=[];for(const t of e){const e=t.id;e&&!s[e]?(s[e]=t,l.push(t)):i?.(t)}const n=l.sort((e,t)=>e.level-t.level);return n.filter((e,s)=>{for(let l=0;l<s;l++){const s=n[l].extent;if(s&&t(s,e.extent))return i?.(e),!1}return!0})}export{n as ExtentQueryContext,l as GeometryQueryContext,a as getLods,o as isTileInfoTilemapCache};
