// 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/geometry/normalizeUtils","dojo/_base/array dojo/_base/lang dojo/_base/Deferred dojo/has ../kernel ../config ../deferredUtils ./Extent ./Polyline ./Polygon ./webMercatorUtils ./jsonUtils".split(" "),function(n,q,E,N,O,P,y,Q,t,A,z,R){function x(a,d){return Math.ceil((a-d)/(2*d))}function B(a,d){var b=a.paths||a.rings,c,f,e=b.length,m;for(c=0;c<e;c++)for(m=b[c].length,f=0;f<m;f++){var l=a.getPoint(c,f);a.setPoint(c,f,l.offset(d,0))}return a}function F(a,d){if(!(a instanceof t||a instanceof
A))throw console.error("_straightLineDensify: the input geometry is neither polyline nor polygon"),Error("_straightLineDensify: the input geometry is neither polyline nor polygon");var b=a instanceof t,c=[],f;n.forEach(b?a.paths:a.rings,function(a){c.push(f=[]);f.push([a[0][0],a[0][1]]);var e,b,k,g,h,p,n,q,t,r,v,u;for(h=0;h<a.length-1;h++){e=a[h][0];b=a[h][1];k=a[h+1][0];g=a[h+1][1];n=Math.sqrt((k-e)*(k-e)+(g-b)*(g-b));q=(g-b)/n;t=(k-e)/n;r=n/d;if(1<r){for(p=1;p<=r-1;p++)u=p*d,v=t*u+e,u=q*u+b,f.push([v,
u]);p=(n+Math.floor(r-1)*d)/2;v=t*p+e;u=q*p+b;f.push([v,u])}f.push([k,g])}});return b?new t({paths:c,spatialReference:a.spatialReference}):new A({rings:c,spatialReference:a.spatialReference})}function C(a,d,b){d&&(a=F(a,1E6),a=z.webMercatorToGeographic(a,!0));b&&(a=B(a,b));return a}function D(a,d,b){var c=a.x||a[0],f;c>d?(f=x(c,d),a.x?a=a.offset(-2*f*d,0):a[0]=c+-2*f*d):c<b&&(f=x(c,b),a.x?a=a.offset(-2*f*b,0):a[0]=c+-2*f*b);return a}function H(a,d){var b=-1;n.forEach(d.cutIndexes,function(c,f){var e=
d.geometries[f];n.forEach(e.rings||e.paths,function(a,f){n.some(a,function(b){if(!(180>b[0])){b=0;var c,d=a.length,m;for(c=0;c<d;c++)m=a[c][0],b=m>b?m:b;b=Number(b.toFixed(9));b=-360*x(b,180);d=a.length;for(c=0;c<d;c++)m=e.getPoint(f,c),e.setPoint(f,c,m.offset(b,0))}return!0})});c===b?e.rings?n.forEach(e.rings,function(b){a[c]=a[c].addRing(b)}):n.forEach(e.paths,function(b){a[c]=a[c].addPath(b)}):(b=c,a[c]=e)});return a}function I(a,d,b,c){var f=new E;f.addCallbacks(b,c);d=d||P.defaults.geometryService;
var e=[],m=[],l,k,g,h,p,q,G,w,r=0;n.forEach(a,function(a){if(a)if(l||(l=a.spatialReference,k=l._getInfo(),h=(g=l._isWebMercator())?2.0037508342788905E7:180,p=g?-2.0037508342788905E7:-180,q=g?102100:4326,G=new t({paths:[[[h,p],[h,h]]],spatialReference:{wkid:q}}),w=new t({paths:[[[p,p],[p,h]]],spatialReference:{wkid:q}})),k){var b=R.fromJson(a.toJson()),c=a.getExtent();"point"===a.type?e.push(D(b,h,p)):"multipoint"===a.type?(b.points=n.map(b.points,function(a){return D(a,h,p)}),e.push(b)):"extent"===
a.type?(b=c._normalize(null,null,k),e.push(b.rings?new A(b):b)):c?(a=2*x(c.xmin,p)*h,b=0===a?b:B(b,a),c=c.offset(a,0),c.intersects(G)&&c.xmax!==h?(r=c.xmax>r?c.xmax:r,b=C(b,g),m.push(b),e.push("cut")):c.intersects(w)&&c.xmin!==p?(r=2*c.xmax*h>r?2*c.xmax*h:r,b=C(b,g,360),m.push(b),e.push("cut")):e.push(b)):e.push(b)}else e.push(a);else e.push(a)});b=new t;c=x(r,h);for(var v=-90,u=c;0<c;){var y=-180+360*c;b.addPath([[y,v],[y,-1*v]]);v*=-1;c--}0<m.length&&0<u?d?d.cut(m,b,function(b){m=H(m,b);var c=[];
n.forEach(e,function(b,f){if("cut"===b){var d=m.shift();a[f].rings&&1<a[f].rings.length&&d.rings.length>=a[f].rings.length?(e[f]="simplify",c.push(d)):e[f]=!0===g?z.geographicToWebMercator(d):d}});0<c.length?d.simplify(c,function(a){n.forEach(e,function(b,c){"simplify"===b&&(e[c]=!0===g?z.geographicToWebMercator(a.shift()):a.shift())});f.callback(e)},function(a){f.errback(a)}):f.callback(e)},function(a){f.errback(a)}):f.errback(Error("esri.geometry.normalizeCentralMeridian: 'geometryService' argument is missing.")):
(n.forEach(e,function(a,b){if("cut"===a){var c=m.shift();e[b]=!0===g?z.geographicToWebMercator(c):c}}),f.callback(e));return f}function w(a,d,b,c){var f=!1,e;q.isObject(a)&&a&&(q.isArray(a)?a.length&&((e=a[0]&&a[0].declaredClass)&&-1!==e.indexOf("Graphic")?(a=n.map(a,function(a){return a.geometry}),f=a.length?!0:!1):e&&-1!==e.indexOf("esri.geometry.")&&(f=!0)):(e=a.declaredClass)&&-1!==e.indexOf("FeatureSet")?(a=n.map(a.features||[],function(a){return a.geometry}),f=a.length?!0:!1):e&&-1!==e.indexOf("esri.geometry.")&&
(f=!0));f&&d.push({index:b,property:c,value:a})}function J(a,d){var b=[];n.forEach(d,function(c){var f=c.i,e=a[f];c=c.p;var d;if(q.isObject(e)&&e)if(c)if("*"===c[0])for(d in e)e.hasOwnProperty(d)&&w(e[d],b,f,d);else n.forEach(c,function(a){w(q.getObject(a,!1,e),b,f,a)});else w(e,b,f)});return b}function K(a,d){var b=0,c={};n.forEach(d,function(f){var e=f.index,d=f.property,l=f.value,k=l.length||1,g=a.slice(b,b+k);q.isArray(l)||(g=g[0]);b+=k;delete f.value;d?(c[e]=c[e]||{},c[e][d]=g):c[e]=g});return c}
function L(a){for(var d=[],b=0,c=0,f=Math.min,e=Math.max,m=0;m<a.length;m++){for(var l=a[m],k=null,g=0;g<l.length;g++)k=l[g],d.push(k),0===g?c=b=k[0]:(b=f(b,k[0]),c=e(c,k[0]));k&&d.push([(b+c)/2,0])}return d}var M={normalizeCentralMeridian:I,_foldCutResults:H,_prepareGeometryForCut:C,_offsetMagnitude:x,_pointNormalization:D,_updatePolyGeometry:B,_straightLineDensify:F,_createWrappers:function(a){var d=q.isObject(a)?a.prototype:q.getObject(a+".prototype");n.forEach(d.__msigns,function(a){var b=d[a.n];
d[a.n]=function(){var c=this,e=[],d,l=new E(y._dfdCanceller);a.f&&y._fixDfd(l);for(d=0;d<a.c;d++)e[d]=arguments[d];var k={dfd:l};e.push(k);var g,h=[],p;c.normalization&&!c._isTable&&(g=J(e,a.a),n.forEach(g,function(a){h=h.concat(a.value)}),h.length&&(p=I(h)));p?(l._pendingDfd=p,p.addCallbacks(function(a){l.canceled||(k.assembly=K(a,g),l._pendingDfd=b.apply(c,e))},function(b){var d=c.declaredClass;d&&-1!==d.indexOf("FeatureLayer")?c._resolve([b],null,e[a.e],l,!0):c._errorHandler(b,e[a.e],l)})):l._pendingDfd=
b.apply(c,e);return l}})},_disassemble:J,_addToBucket:w,_reassemble:K,getDenormalizedExtent:function(a){if(!a)return null;var d=a.getExtent();if(!d)return null;var b=a.spatialReference&&a.spatialReference._getInfo();if(!b)return d;var c=b.valid[0],b=b.valid[1],f=2*b,e=d.getWidth(),m=d.xmax,l=d.xmin;if("extent"===a.type||0===e||e<=b||e>f||m<c||l>b)return d;var k;switch(a.type){case "polygon":if(1<a.rings.length)k=L(a.rings);else return d;break;case "polyline":if(1<a.paths.length)k=L(a.paths);else return d;
break;case "multipoint":k=a.points}a=Math.min;for(var c=Math.max,f=new Q(d.toJson()),g=0;g<k.length;g++){var h=k[g][0];0>h?(h+=b,l=c(h,l)):(h-=b,m=a(h,m))}f.xmin=m;f.xmax=l;return f.getWidth()<e?(f.xmin-=b,f.xmax-=b,f):d}};N("extend-esri")&&q.mixin(q.getObject("geometry",!0,O),M);return M});