// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://js.arcgis.com/3.23/esri/copyright.txt for details.
//>>built
define("esri/layers/vectorTiles/views/2d/tiling/TileInfoView","require exports ./LODInfo ./TileKey ./TileSpan ./TileCoverage".split(" "),function(x,y,t,p,q,v){var w=function(){function f(a,c,b,e,d,r,l,h){this.x=a;this.ymin=c;this.ymax=b;this.invM=e;this.leftAdjust=d;this.rightAdjust=r;this.leftBound=l;this.rightBound=h}f.create=function(a,c){a[1]>c[1]&&(b=[c,a],a=b[0],c=b[1]);var b=a[0],e=a[1],d=c[0],r=c[1],l=d-b,h=r-e,h=0!==h?l/h:0,k=(Math.ceil(e)-e)*h,g=(Math.floor(e)-e)*h;return new f(b,Math.floor(e),
Math.ceil(r),h,0>l?k:g,0>l?g:k,0>l?d:b,0>l?b:d);var b};f.prototype.incrRow=function(){this.x+=this.invM};f.prototype.getLeftCol=function(){return Math.max(this.x+this.leftAdjust,this.leftBound)};f.prototype.getRightCol=function(){return Math.min(this.x+this.rightAdjust,this.rightBound)};return f}(),k=[[0,0],[0,0],[0,0],[0,0]];return function(){function f(a,c){var b=this;this.tileInfo=a;this.fullExtent=c;this.scales=[];this._lodInfos=null;this._infoByScale={};this._infoByLevel={};var e=a.lods.slice();
e.sort(function(a,b){return b.scale-a.scale});var d=this._lodInfos=e.map(function(b){return t.create(a,b,c)});e.forEach(function(a,c){b._infoByLevel[a.level]=d[c];b._infoByScale[a.scale]=d[c];b.scales[c]=a.scale},this);this._wrap=a.isWrappable}f.prototype.getTileBounds=function(a,c){var b=this._infoByLevel[c.level];return b?b.getTileBounds(a,c):a};f.prototype.getTileCoords=function(a,c){var b=this._infoByLevel[c.level];return b?b.getTileCoords(a,c):a};f.prototype.getTileCoverage=function(a){var c=
this.getClosestInfoForScale(a.scale),b=v.pool.acquire(c),e=this._wrap,d;d=Infinity;var f=-Infinity,l,h,p=b.spans;k[0][0]=k[0][1]=k[1][1]=k[3][0]=0;k[1][0]=k[2][0]=a.size[0];k[2][1]=k[3][1]=a.size[1];for(var g=0;g<k.length;g++){var m=k[g];a.toMap(m,m);m[0]=c.getColumnForX(m[0]);m[1]=c.getRowForY(m[1])}a=[];m=3;for(g=0;4>g;g++){if(k[g][1]!==k[m][1]){var n=w.create(k[g],k[m]);d=Math.min(n.ymin,d);f=Math.max(n.ymax,f);void 0===a[n.ymin]&&(a[n.ymin]=[]);a[n.ymin].push(n)}m=g}if(null==d||null==f||100<f-
d)return null;for(m=[];d<f;){null!=a[d]&&(m=m.concat(a[d]));l=Infinity;h=-Infinity;for(g=m.length-1;0<=g;g--)n=m[g],l=Math.min(l,n.getLeftCol()),h=Math.max(h,n.getRightCol());l=Math.floor(l);h=Math.floor(h);if(d>=c.first[1]&&d<=c.last[1])if(e)if(c.size[0]<c.worldSize[0])for(n=Math.floor(h/c.worldSize[0]),g=Math.floor(l/c.worldSize[0]);g<=n;g++)p.push(new q(d,Math.max(c.getFirstColumnForWorld(g),l),Math.min(c.getLastColumnForWorld(g),h)));else p.push(new q(d,l,h));else l>c.last[0]||h<c.first[0]||(l=
Math.max(l,c.first[0]),h=Math.min(h,c.last[0]),p.push(new q(d,l,h)));d+=1;for(g=m.length-1;0<=g;g--)n=m[g],n.ymax>=d?n.incrRow():m.splice(g,1)}return b};f.prototype.getTileIdAtParent=function(a,c){var b=p.pool.acquire(c),e=this._infoByLevel[b.level];if(a.resolution<e.resolution)throw Error("Cannot calculate parent tile. destination LOD's resolution "+a.resolution+" is not a parent resolution of "+e.resolution);return a.resolution===e.resolution?b.id:p.getId(a.level,Math.floor(b.row*e.resolution/a.resolution+
.01),Math.floor(b.col*e.resolution/a.resolution+.01),b.world)};f.prototype.getTileParentId=function(a){a=p.pool.acquire(a);var c=this._lodInfos.indexOf(this._infoByLevel[a.level])-1;if(0>c)return p.pool.release(a),null;c=this.getTileIdAtParent(this._lodInfos[c],a);p.pool.release(a);return c};f.prototype.getTileResolution=function(a){return(a=this._infoByLevel[a.level])?a.resolution:-1};f.prototype.getTileScale=function(a){return(a=this._infoByLevel[a.level])?a.scale:-1};f.prototype.intersects=function(a,
c){var b=p.pool.acquire(c),e=this._infoByLevel[b.level],d=a.lodInfo;if(d.resolution>e.resolution){var f=p.pool.acquire(this.getTileIdAtParent(d,b)),l=d.denormalizeCol(f.col,f.world),h=a.spans.some(function(a){return a.row===f.row&&a.colFrom<=l&&a.colTo>=l});p.pool.release(b);p.pool.release(f);return h}if(d.resolution<e.resolution){var k=a.spans.reduce(function(a,b){a[0]=Math.min(a[0],b.row);a[1]=Math.max(a[1],b.row);a[2]=Math.min(a[2],b.colFrom);a[3]=Math.max(a[3],b.colTo);return a},[Infinity,-Infinity,
Infinity,-Infinity]),h=k[0],g=k[1],m=k[2],k=k[3],n=e.denormalizeCol(b.col,b.world),q=d.getColumnForX(e.getXForColumn(n)),t=d.getRowForY(e.getYForRow(b.row)),n=d.getColumnForX(e.getXForColumn(n+1))-1,e=d.getRowForY(e.getYForRow(b.row+1))-1;p.pool.release(b);return!(q>k||n<m||t>g||e<h)}var u=d.denormalizeCol(b.col,b.world),h=a.spans.some(function(a){return a.row===b.row&&a.colFrom<=u&&a.colTo>=u});p.pool.release(b);return h};f.prototype.getClosestInfoForScale=function(a){var c=this.scales;this._infoByScale[a]||
(a=c.reduce(function(b,c,d,f){return Math.abs(c-a)<Math.abs(b-a)?c:b},c[0]));return this._infoByScale[a]};return f}()});