// 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/clustering/geohashUtils","dojo/has dojo/string dojo/_base/lang dojo/_base/array ../../kernel ../../geometry/Point ../../geometry/Extent ../../SpatialReference ../../geometry/webMercatorUtils".split(" "),function(G,H,I,l,J,p,m,K,y){function q(a){var b=a.spatialReference;if(!b)return{x:a.x,y:a.y};var c=b.isWebMercator(),d;4326===b.wkid?d={x:a.x,y:a.y}:c&&(d={x:a.getLongitude(),y:a.getLatitude()});return d}function r(a){var b=a.spatialReference,c=!(!b||!b.isWebMercator()),d;b&&4326===
b.wkid?d=new m(a.toJson()):c&&(d=y.webMercatorToGeographic(a,!0));return d}function t(a,b){b.x=p.prototype._normalizeX(a.x,K.prototype._info[4326]);b.y=a.y;return b}function L(a){a=g.decimals[a].toString(2);return H.pad(a,g.bitsPerBase32Char,"0")}function z(a,b){var c;b=b.slice();b.splice(1,0,0);for(var d=b[0],e=b[2],f=0;f<a.length;f++)Number(a[f])?b[0]=b[1]:b[2]=b[1],c=b[1]=(b[0]+b[2])/2,d=b[0],e=b[2];return{value:c,min:d,max:e}}function A(a,b,c){var d=[];b=b.slice();b.splice(1,0,0);for(var e=b[0],
f=b[2],g=0;g<c;g++)a>=b[1]?(b[0]=b[1],e=1):(b[2]=b[1],e=0),b[1]=(b[0]+b[2])/2,d.push(e),e=b[0],f=b[2];return{value:d.join(""),min:e,max:f}}function B(a){var b=a*g.bitsPerBase32Char;0===b%2?a=b/=2:(a=(b+1)/2,b-=a);return{lon:a,lat:b}}function M(a){var b=0;a=a.split("").reverse().join("");for(var c=0;c<a.length;c++)b+=Number(a[c])*Math.pow(2,c);return g.base32[b]}function u(a,b){var c={x:a.xmin,y:a.ymax},d={x:a.xmax,y:a.ymin},e={x:a.xmax,y:a.ymax};return{sw:h.pointToGeohash({x:a.xmin,y:a.ymin},b),nw:h.pointToGeohash(c,
b),se:h.pointToGeohash(d,b),ne:h.pointToGeohash(e,b)}}function v(a,b,c,d,e){var f=a;do e[f]||(e[f]=!0,d.push(f)),(a=f===b)||(f=k(f,c));while(!a)}function C(a,b,c,d){var e=u(a,b);a=[];v(e.sw,e.nw,"n",a,{});b=[];v(e.se,e.ne,"n",b,{});for(e=0;e<a.length;e++)v(a[e],b[e],"e",c,d)}function D(a){return l.filter(a,function(a){return 180===a.xmax?!0:!1})[0]}function E(a){return l.filter(a,function(a){return-180===a.xmin?!0:!1})[0]}function n(a,b,c,d){var e=h.geographicToWebMercator(a);a={x:e.x-b,y:e.y-b};
b=h.webMercatorToGeographic({x:e.x+b,y:e.y+b},!0);a=h.webMercatorToGeographic(a,!0);return new m(null!=c?c:a.x,a.y,null!=d?d:b.x,b.y)}function w(a,b,c,d){var e;a=u(a,b);var f,g;"min"===d?g=180:"max"===d&&(f=-180);var k=f;d=g;e=h.geohashToCell(a.sw).extent;b=h.geohashToCell(a.ne).extent;a=null!=k?k:e.xmin;e=e.ymin;d=null!=d?d:b.xmax;b=b.ymax;a=n({x:a,y:e},c,f,g);c=n({x:d,y:b},c,f,g);return new m(a.xmin,a.ymin,c.xmax,c.ymax)}function F(a,b,c){var d=a,e=0;do e++,(a=d===b)||(d=k(d,c));while(!a);return e}
function x(a,b){var c=u(a,b),d=F(c.nw,c.ne,"e");return{rows:F(c.nw,c.sw,"s"),cols:d}}function k(a,b){var c=a.length%2,d=a.slice(-1),e=g.decimals[d],f=a.slice(0,-1);-1!=g.borders[b][c].indexOf(d)&&f&&(f=k(f,b));return f+g.neighbors[b][c][e]}var g={base32:"0123456789bcdefghjkmnpqrstuvwxyz",decimals:function(){for(var a={},b=0;32>b;b++)a["0123456789bcdefghjkmnpqrstuvwxyz"[b]]=b;return a}(),neighbors:{n:[null,"238967debc01fg45kmstqrwxuvhjyznp"],s:[null,"bc01fg45238967deuvhjyznpkmstqrwx"],e:[null,"14365h7k9dcfesgujnmqp0r2twvyx8zb"],
w:[null,"p0r21436x8zb9dcf5h7kjnmqesgutwvy"]},borders:{n:[null,"bcfguvyz"],s:[null,"0145hjnp"],e:[null,"prxz"],w:[null,"028b"]},bitsPerBase32Char:5,maxGeohashLength:12,longitudeRange:[-180,180],latitudeRange:[-90,90]};g.neighbors.n[0]="14365h7k9dcfesgujnmqp0r2twvyx8zb";g.neighbors.s[0]="p0r21436x8zb9dcf5h7kjnmqesgutwvy";g.neighbors.e[0]="238967debc01fg45kmstqrwxuvhjyznp";g.neighbors.w[0]="bc01fg45238967deuvhjyznpkmstqrwx";g.borders.n[0]="prxz";g.borders.s[0]="028b";g.borders.e[0]="bcfguvyz";g.borders.w[0]=
"0145hjnp";var h={geographicToWebMercator:function(a){a=p.lngLatToXY(a.x,a.y);return{x:a[0],y:a[1]}},webMercatorToGeographic:function(a,b){var c=p.xyToLngLat(a.x,a.y,b);return{x:c[0],y:c[1]}},geohashToCell:function(a){for(var b=[],c=0;c<a.length;c++)b.push(L(a[c]));for(var b=b.join(""),c=[],d=[],e=0;e<b.length;e++)0===e%2?c.push(b[e]):d.push(b[e]);c=c.join("");d=d.join("");c=[c,d];b=z(c[0],g.longitudeRange);c=z(c[1],g.latitudeRange);return{point:{x:b.value,y:c.value},extent:{xmin:b.min,xmax:b.max,
ymin:c.min,ymax:c.max},geohash:a}},pointToCell:function(a,b){var c,d,e;b=b||g.maxGeohashLength;var f=q(a);if(f){f=t(f,f);c=B(b);e=A(f.x,g.longitudeRange,c.lon);var h=A(f.y,g.latitudeRange,c.lat);c=e.value;d=h.value;e={xmin:e.min,xmax:e.max,ymin:h.min,ymax:h.max};for(var h=[],k=Math.ceil((c.length+d.length)/g.bitsPerBase32Char),l=0,m=0,n=0;n<k*g.bitsPerBase32Char;n++){var p=0===n%2?c[l++]:d[m++];null==p&&(p=0);h.push(p)}c=h.join("");d=g.bitsPerBase32Char;h=c.length/d;k=[];for(l=0;l<h;l++)m=c.substr(l*
d,d),k.push(M(m));c=k.join("");return{point:f,extent:e,geohash:c}}},geohashToPoint:function(a){return h.geohashToCell(a).point},pointToGeohash:function(a,b){var c=h.pointToCell(a,b);return c&&c.geohash},getCells:function(a){return l.map(a,function(a){return h.geohashToCell(a)})},getCellSize:function(a){var b=g.longitudeRange,c=g.latitudeRange,b=Math.abs(b[1]-b[0]),c=Math.abs(c[1]-c[0]);a=B(a||1);return{width:b/Math.pow(2,a.lon),height:c/Math.pow(2,a.lat)}},getCellSizeInMeters:function(a){a=h.getCellSize(a);
var b=y.metersPerDegree;a.width*=b;a.height*=b;return a},getIntersecting:function(a,b,c){b=b||1;c=c||0;var d=[],e=r(a);if(e){c&&(e=h.expandExtent(a,b,c));a=e.normalize();var f={};l.forEach(a,function(a){C(a,b,d,f)})}return d},countIntersecting:function(a,b,c){b=b||1;c=c||0;var d=0,e=r(a);if(e){c&&(e=h.expandExtent(a,b,c));a=e.normalize();if(2===a.length){d=D(a);e=E(a);c=x(d,b);var f=x(e,b);a=c.rows;c=c.cols+f.cols;d={x:d.xmin,y:d.ymax};e={x:e.xmax,y:e.ymax};d=t(d,{});e=t(e,{});d=h.pointToGeohash(d,
b);b=h.pointToGeohash(e,b);d===b&&c--;b={rows:a,cols:c}}else b=x(a[0],b);d=b.cols*b.rows}return d},getChildren:function(a){a=a||"";var b=[],c;for(c in g.decimals)b.push(a+c);return b},getNeighbors:function(a){var b=k(a,"n"),c=k(a,"s");return[b,k(b,"e"),k(b,"w"),c,k(c,"e"),k(c,"w"),k(a,"e"),k(a,"w")]},getExtentFromDistance:function(a,b){b=b||1E3;var c,d=q(a);d&&(c=n(d,b));return c},expandExtent:function(a,b,c){b=b||1;c=c||0;if(a=r(a)){if(c){a=a.normalize();if(2===a.length){var d=c;c=D(a);a=E(a);c=
w(c,b,d,"min");a=w(a,b,d,"max");360<=c.getWidth()+a.getWidth()?(b=-180,a=180):(b=c.xmin,a=a.xmax);return new m(b,c.ymin,a,c.ymax)}return w(a[0],b,c)}return a}},getNeighborsWithinDistance:function(a,b,c){c=c||1E3;b=b||1;var d=[];if(a=q(a)){c=n(a,c).normalize();var e={};l.forEach(c,function(a){C(a,b,d,e)})}return d}};G("extend-esri")&&I.setObject("layers.clustering.geohashUtils",h,J);return h});