import{DEFAULT_TILE_SIZE}from"./common.js";import{assert}from"../asserts.js";import TileRange,{createOrUpdate as createOrUpdateTileRange}from"../TileRange.js";import{isSorted,linearFindNearest}from"../array.js";import{createOrUpdate,getTopLeft}from"../extent.js";import{clamp}from"../math.js";import{toSize}from"../size.js";import{createOrUpdate as createOrUpdateTileCoord}from"../tilecoord.js";var tmpTileCoord=[0,0,0],TileGrid=function(){function t(t){var i;if(this.minZoom=void 0!==t.minZoom?t.minZoom:0,this.resolutions_=t.resolutions,assert(isSorted(this.resolutions_,function(t,i){return i-t},!0),17),!t.origins)for(var e=0,o=this.resolutions_.length-1;e<o;++e)if(i){if(this.resolutions_[e]/this.resolutions_[e+1]!==i){i=void 0;break}}else i=this.resolutions_[e]/this.resolutions_[e+1];this.zoomFactor_=i,this.maxZoom=this.resolutions_.length-1,this.origin_=void 0!==t.origin?t.origin:null,this.origins_=null,void 0!==t.origins&&(this.origins_=t.origins,assert(this.origins_.length==this.resolutions_.length,20));var r=t.extent;void 0===r||this.origin_||this.origins_||(this.origin_=getTopLeft(r)),assert(!this.origin_&&this.origins_||this.origin_&&!this.origins_,18),this.tileSizes_=null,void 0!==t.tileSizes&&(this.tileSizes_=t.tileSizes,assert(this.tileSizes_.length==this.resolutions_.length,19)),this.tileSize_=void 0!==t.tileSize?t.tileSize:this.tileSizes_?null:DEFAULT_TILE_SIZE,assert(!this.tileSize_&&this.tileSizes_||this.tileSize_&&!this.tileSizes_,22),this.extent_=void 0!==r?r:null,this.fullTileRanges_=null,this.tmpSize_=[0,0],void 0!==t.sizes?this.fullTileRanges_=t.sizes.map(function(t,i){return new TileRange(Math.min(0,t[0]),Math.max(t[0]-1,-1),Math.min(0,t[1]),Math.max(t[1]-1,-1))},this):r&&this.calculateTileRanges_(r)}return t.prototype.forEachTileCoord=function(t,i,e){for(var o=this.getTileRangeForExtentAndZ(t,i),r=o.minX,n=o.maxX;r<=n;++r)for(var s=o.minY,l=o.maxY;s<=l;++s)e([i,r,s])},t.prototype.forEachTileCoordParentTileRange=function(t,i,e,o){var r,n,s=null,l=t[0]-1;for(2===this.zoomFactor_?(r=t[1],n=t[2]):s=this.getTileCoordExtent(t,o);l>=this.minZoom;){if(i(l,2===this.zoomFactor_?(r=Math.floor(r/2),n=Math.floor(n/2),createOrUpdateTileRange(r,r,n,n,e)):this.getTileRangeForExtentAndZ(s,l,e)))return!0;--l}return!1},t.prototype.getExtent=function(){return this.extent_},t.prototype.getMaxZoom=function(){return this.maxZoom},t.prototype.getMinZoom=function(){return this.minZoom},t.prototype.getOrigin=function(t){return this.origin_||this.origins_[t]},t.prototype.getResolution=function(t){return this.resolutions_[t]},t.prototype.getResolutions=function(){return this.resolutions_},t.prototype.getTileCoordChildTileRange=function(t,i,e){if(t[0]<this.maxZoom){var o;if(2===this.zoomFactor_)return r=2*t[1],o=2*t[2],createOrUpdateTileRange(r,1+r,o,1+o,i);var r=this.getTileCoordExtent(t,e);return this.getTileRangeForExtentAndZ(r,t[0]+1,i)}return null},t.prototype.getTileRangeExtent=function(t,i,e){var o=this.getOrigin(t),r=this.getResolution(t),t=toSize(this.getTileSize(t),this.tmpSize_),n=o[0]+i.minX*t[0]*r,s=o[0]+(i.maxX+1)*t[0]*r,l=o[1]+i.minY*t[1]*r,o=o[1]+(i.maxY+1)*t[1]*r;return createOrUpdate(n,l,s,o,e)},t.prototype.getTileRangeForExtentAndZ=function(t,i,e){var o=tmpTileCoord,r=(this.getTileCoordForXYAndZ_(t[0],t[3],i,!1,o),o[1]),n=o[2];return this.getTileCoordForXYAndZ_(t[2],t[1],i,!0,o),createOrUpdateTileRange(r,o[1],n,o[2],e)},t.prototype.getTileCoordCenter=function(t){var i=this.getOrigin(t[0]),e=this.getResolution(t[0]),o=toSize(this.getTileSize(t[0]),this.tmpSize_);return[i[0]+(t[1]+.5)*o[0]*e,i[1]-(t[2]+.5)*o[1]*e]},t.prototype.getTileCoordExtent=function(t,i){var e=this.getOrigin(t[0]),o=this.getResolution(t[0]),r=toSize(this.getTileSize(t[0]),this.tmpSize_),n=e[0]+t[1]*r[0]*o,e=e[1]-(t[2]+1)*r[1]*o,t=n+r[0]*o,r=e+r[1]*o;return createOrUpdate(n,e,t,r,i)},t.prototype.getTileCoordForCoordAndResolution=function(t,i,e){return this.getTileCoordForXYAndResolution_(t[0],t[1],i,!1,e)},t.prototype.getTileCoordForXYAndResolution_=function(t,i,e,o,r){var n=this.getZForResolution(e),s=e/this.getResolution(n),l=this.getOrigin(n),a=toSize(this.getTileSize(n),this.tmpSize_),h=o?.5:0,t=Math.floor((t-l[0])/e+(o?.5:0)),l=Math.floor((l[1]-i)/e+h),i=s*t/a[0],e=s*l/a[1],e=o?(i=Math.ceil(i)-1,Math.ceil(e)-1):(i=Math.floor(i),Math.floor(e));return createOrUpdateTileCoord(n,i,e,r)},t.prototype.getTileCoordForXYAndZ_=function(t,i,e,o,r){var n=this.getOrigin(e),s=this.getResolution(e),l=toSize(this.getTileSize(e),this.tmpSize_),a=o?.5:0,t=Math.floor((t-n[0])/s+(o?.5:0)),n=Math.floor((n[1]-i)/s+a),i=t/l[0],s=n/l[1],s=o?(i=Math.ceil(i)-1,Math.ceil(s)-1):(i=Math.floor(i),Math.floor(s));return createOrUpdateTileCoord(e,i,s,r)},t.prototype.getTileCoordForCoordAndZ=function(t,i,e){return this.getTileCoordForXYAndZ_(t[0],t[1],i,!1,e)},t.prototype.getTileCoordResolution=function(t){return this.resolutions_[t[0]]},t.prototype.getTileSize=function(t){return this.tileSize_||this.tileSizes_[t]},t.prototype.getFullTileRange=function(t){return this.fullTileRanges_?this.fullTileRanges_[t]:null},t.prototype.getZForResolution=function(t,i){t=linearFindNearest(this.resolutions_,t,i||0);return clamp(t,this.minZoom,this.maxZoom)},t.prototype.calculateTileRanges_=function(t){for(var i=this.resolutions_.length,e=new Array(i),o=this.minZoom;o<i;++o)e[o]=this.getTileRangeForExtentAndZ(t,o);this.fullTileRanges_=e},t}();export default TileGrid;