// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://@sbaseurl@/jsapi/jsapi/esri/copyright.txt and http://www.arcgis.com/apps/webappbuilder/copyright.txt for details.

//>>built
define("dojo/_base/array dojo/Deferred dojo/json dojo/number esri/Color esri/request esri/geometry/Polygon".split(" "),function(k,u,r,v,w,x,y){return{computeHistograms:function(a,b,d){var c=this,e=new u,h=b.maxImageWidth,n=b.maxImageHeight;d=this.makePolygon(d);h=this.getModelPixelSize(d,h,n);var p=b.renderingRule.toJson();this.execComputeHistograms(b,d,h,p).then(function(f){f=c.processHistogramResponse(a,f,p);e.resolve(f)}).otherwise(function(f){e.reject(f)});return e},execComputeHistograms:function(a,
b,d,c){a=a.url+"/computeHistograms";var e={f:"json"};e.geometry=r.stringify(b.toJson());e.geometryType="esriGeometryPolygon";e.renderingRule=r.stringify(c);e.pixelSize=r.stringify(d);return x({url:a,content:e,handleAs:"json",callbackParamName:"callback"},{})},getGeometries:function(a){var b=[];k.forEach(a,function(d){d.geometry&&b.push(d.geometry)});return b},getModelPixelSize:function(a,b,d){var c=0,e=0;a=a.getExtent();c=a.getWidth();e=a.getHeight();1>c&&(c=1);1>e&&(e=1);b=Math.max(Math.ceil(c/b),
Math.ceil(e/d));return{x:b,y:b}},isPolygonLayer:function(a){return a&&"esriGeometryPolygon"===a.geometryType?!0:!1},isWROModelLayer:function(a){var b="Title Url Description InputRanges OutputValues NoDataRanges RangeLabels NoDataRangeLabels".split(" ");return a&&"ArcGISImageServiceLayer"===a.type&&10.3<=a.version&&a.renderingRule&&a.renderingRule.rasterFunctionArguments&&a.renderingRule.rasterFunctionArguments.Colormap&&a.allowRasterFunction&&"Nearest"===a.defaultResamplingMethod&&k.every(b,function(d){return k.some(a.fields,
function(c){return d===c.name})})?!0:!1},makePolygon:function(a){return this.mergePolygons(this.getGeometries(a))},mergePolygons:function(a){var b=null;1===a.length?b=a[0]:(b=new y(a[0].spatialReference),k.forEach(a,function(d){k.forEach(d.rings,function(c){b.addRing(c)})}));return b},processHistogramResponse:function(a,b,d){var c={noDataCount:0,colorCounts:[]};if(!b||!b.histograms||0===b.histograms.length)return c;var e=a.util.colorRamp.tipPattern,h=b.histograms[0],n=Math.round(h.min),p=Math.round(h.max)-
1;0===n&&0<h.counts&&(c.noDataCount=h.counts[0]);var f=0;k.forEach(d.rasterFunctionArguments.Colormap,function(g){var l=0,m=g[0],q=null,t=q;g=[g[1],g[2],g[3]];var z=(new w(g)).toHex();l=m<n||m>p?0:h.counts[m-n];f+=l;a.util.colorRamp[""+m]&&(q=a.util.colorRamp[""+m],t=e.replace("{label}",q).replace("{value}",m));c.colorCounts.push({count:l,value:m,pct:0,label:q,fullLabel:t,rgb:g,hex:z})});0<c.noDataCount&&(f+=c.noDataCount);0<f&&k.forEach(c.colorCounts,function(g){var l=g.count/f*100;l=v.format(l,
{places:2});g.pct=l});return c}}});