// 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/lang","moment/moment","./_chartHelpUtils","jimu/utils","libs/moment/twix"],function(w,x,z,v){window.makeTwix&&window.makeTwix(x);return{getClietStatisticsData:function(a){var b=a.mode;if("feature"===b)return this.getFeatureModeStatisticsData(a);if("category"===b)return this.getCategoryModeStatisticsData(a);if("count"===b)return this.getCountModeStatisticsData(a);if("field"===b)return this.getFieldModeStatisticsData(a)},sortClientStatisticsData:function(a,b,d){return this.sortStatisticsData(a,
b.mode,b.sortOrder,b.clusterField,b.valueFields,d)},getDataForMaxLabels:function(a,b){return a&&a.length&&"number"===typeof b&&0<b&&b<a.length?a.slice(0,b):a},getFeatureModeStatisticsData:function(a){var b=a.features,d=a.clusterField,c=a.valueFields;a=void 0!==a.showNullLabelData?a.showNullLabelData:!0;var f=this.separateFeaturesWhetherFieldValueNull(d,b);b=f.notNullLabelFeatures;f=f.nullLabelFeatures;b=a?b.concat(f):b;a=[];return a=b.map(function(e){var m=e.attributes,p=m&&m[d];p=this.convertingNullOrUndefinedAsPlaceholders(p);
e={label:p,values:[],features:[e]};e.values=c.map(function(g){return m[g]});return e}.bind(this))},getCategoryModeStatisticsData:function(a){function b(k){var l=[],h=null;for(r in k){h=k[r];var r=this.keepFieldValueType(r,h.type,e);var A=this.calcValuesByFeaturesForCatetoryMode(c,h,d,f,p);l.push({label:r,values:A,features:h.features,unit:h.unit})}return l}var d=a.hasStatisticsed,c=a.valueFields,f=a.operation,e=a.dateConfig,m=void 0!==a.showNullLabelData?a.showNullLabelData:!0,p=a.nullValue,g=[];g=
this.getCluseringObj(a.clusterField,a.features,e);a=g.nullLabel;g=b.call(this,g.notNullLabel);a=b.call(this,a);return g=m?g.concat(a):g},getCountModeStatisticsData:function(a){function b(m){var p=[],g=null;for(l in m){g=m[l];var k=this.calcValuesByFeaturesForCountMode(g,e);var l=this.keepFieldValueType(l,g.type,d);p.push({label:l,values:k,features:g.features,unit:g.unit})}return p}var d=a.dateConfig,c=void 0!==a.showNullLabelData?a.showNullLabelData:!0,f=[],e=a.hasStatisticsed&&!d;f=this.getCluseringObj(a.clusterField,
a.features,d);a=f.nullLabel;f=b.call(this,f.notNullLabel);a=b.call(this,a);return f=c?f.concat(a):f},calcValuesByFeaturesForCountMode:function(a,b){return b?a.features.map(function(d){var c=(d=d.attributes)&&d.STAT_COUNT;"undefined"===typeof c&&(c=d.stat_count);return c}.bind(this)):[a.count]},getFieldModeStatisticsData:function(a){var b=a.hasStatisticsed,d=a.features,c=a.operation,f=a.nullValue,e=[];return e=a.valueFields.map(w.hitch(this,function(m){var p=this.getValuesByValueFieldForFieldMode(m,
d,b,c);p=this.calcValueByOperation(p,c,f);return{label:m,values:[p]}}))},getValueFromAttributes:function(a,b,d,c){if(a)return d="average"===d?"avg":d,c?(c=v.upperCaseString(b+"_"+d),c=a[c],"undefined"===typeof c&&(c=v.lowerCaseString(b+"_"+d),c=a[c])):c=a[b],c},getValuesByValueFieldForFieldMode:function(a,b,d,c){return b.map(w.hitch(this,function(f){return this.getValueFromAttributes(f.attributes,a,c,d)}))},sortStatisticsData:function(a,b,d,c,f,e){function m(g,k,l){if("category"===k)if(l.isLabelAxis)l.isLabelAxis&&
(h=g.label);else if(l.field||1!==g.values.length){l=f.indexOf(l.field);var h=g.values[l]}else h=g.values[0];else if("count"===k)h=g.label,h=l.isLabelAxis?h:g.values[0];else if("field"===k)h=l.isLabelAxis?g.label:g.values[0];else if("feature"===k){var r=l.field;g&&g.features&&g.features[0]&&(k=g.features[0].attributes)&&(l.isLabelAxis?h=k[c]:r?h=k[r]:g.values.length&&(h=g.values[0]))}return h}if(!d)return a;var p=d.isAsc;if(!Array.isArray(a))return a;a.sort(function(g,k){g=m(g,b,d);k=m(k,b,d);if(d.isLabelAxis&&
e&&"field"!==b){var l={};l[c]=g;l=v.getDisplayValueForCodedValueOrSubtype(e,c,l);var h={};h[c]=k;h=v.getDisplayValueForCodedValueOrSubtype(e,c,h);l.isCodedValueOrSubtype&&h.isCodedValueOrSubtype&&(g=l.displayValue,k=h.displayValue)}"_NULL\x26UNDEFINED_"===g&&(g=Infinity);"_NULL\x26UNDEFINED_"===k&&(k=Infinity);v.isNumberOrNumberString(g)&&(g=Number(g));v.isNumberOrNumberString(k)&&(k=Number(k));l=g>k?p:!p;Infinity===g?l=p:Infinity===k&&(l=!p);h=0;g!==k&&(h=l?1:-1);return h}.bind(this));return a},
convertingNullOrUndefinedAsPlaceholders:function(a){return null===a||void 0===a?"_NULL\x26UNDEFINED_":a},keepFieldValueType:function(a,b,d){"number"===b&&(a=Number(a));d&&"_NULL\x26UNDEFINED_"!==a&&(a=Number(a));return a},calcValuesByFeaturesForCatetoryMode:function(a,b,d,c,f){return a.map(function(e){e=this.getValuesByValueFieldForCategoryMode(e,b.features,d,c);return this.calcValueByOperation(e,c,f)}.bind(this))},getValuesByValueFieldForCategoryMode:function(a,b,d,c){return b.map(w.hitch(this,function(f){return this.getValueFromAttributes(f.attributes,
a,c,d)}))},_isNumber:function(a){return"[object number]"===Object.prototype.toString.call(a).toLowerCase()},_getDateUnit:function(a,b){var d=b;"automatic"===b&&(b=x(a[0]).local(),a=x(a[1]).local(),a=Math.round(a.diff(b,"minute",!0)),0<=a&&1>=a?d="second":1<a&&60>=a?d="minute":60<a&&1440>=a?d="hour":1440<a&&43200>=a?d="day":43200<a&&518400>=a?d="month":518400<a&&(d="year"));return d},_getTimeRange:function(a,b){var d=a.map(w.hitch(this,function(c){return(c=c.attributes)&&c[b]}));d=d.filter(function(c){return!!c});
a=Math.min.apply(Math,d);d=Math.max.apply(Math,d);return[a,d]},_getTimeTwixs:function(a,b,d){if(0>"year quarter month day hour minute second automatic".split(" ").indexOf(d))return console.log("Invaild data formatter: "+d),!1;a=this._getTimeRange(a,b);if(a[0]===a[1])return!1;d=this._getDateUnit(a,d);b=this.getStartTimeByUnit(a[0],d);b=x(b).local();a=x(a[1]).local();a=b.twix(a).split(1,d);b={startValue:a[a.length-1].end().valueOf(),endValue:Infinity};a.push(b);return{twixs:a,dateUnit:d}},getCluseringObj:function(a,
b,d){var c={},f={};d?(a=this.getClusteringObjForDateType(a,b,d),c=a.notNullLabel,f=a.nullLabel):(c=this.separateFeaturesWhetherFieldValueNull(a,b),b=c.nullLabelFeatures,c=this.getClusteringObjByField(c.notNullLabelFeatures,a),f=this.getClusteringObjByField(b,a));return{notNullLabel:c,nullLabel:f}},calcValueByOperation:function(a,b,d){if(a&&1===a.length){if(!d)return a[0];if(!this._isNumber(a[0]))return 0}if(0!==a.length){var c=0;"max"===b?c=-Infinity:"min"===b&&(c=Infinity);a=d?a.map(function(e){this._isNumber(e)||
(e=0);return e}.bind(this)):a.filter(function(e){return this._isNumber(e)}.bind(this));var f=0;a.forEach(w.hitch(this,function(e){f++;"average"===b||"sum"===b?c+=e:"max"===b?c=Math.max(c,e):"min"===b&&(c=Math.min(c,e))}));0<f?"average"===b&&(c/=f):c=null}else c=null;return c},getClusteringObjByField:function(a,b){var d={};a.forEach(w.hitch(this,function(c){var f=c.attributes;f=f&&f[b];var e=typeof f;f=this.convertingNullOrUndefinedAsPlaceholders(f);var m=null;d.hasOwnProperty(f)?(m=d[f],m.features.push(c),
m.count++):(m={count:1,features:[c],type:e},d[f]=m)}));return d},_removeNaNDataItem:function(a){return a.filter(function(b){var d=!1;b=b.category;"number"===typeof b&&(d=isNaN(b));return!d})},separateFeaturesWhetherFieldValueNull:function(a,b){var d=[],c=[];Array.isArray(b)&&(c=b.filter(function(f){var e=f.attributes;e=e&&e[a];if(null===e||void 0===e)d.push(f);else return!0}));return{nullLabelFeatures:d,notNullLabelFeatures:c}},getStartTimeByUnit:function(a,b){return x(a).startOf(b).utc().valueOf()},
_mosaicFieldNameWithOperatorAndUpper:function(a,b){return v.upperCaseString(a+"_"+b)},_mosaicFieldNameWithOperatorAndLower:function(a,b){return v.lowerCaseString(a+"_"+b)},getClusteringObjForDateType:function(a,b,d){function c(n,q,t){n[q]?(n=n[q],n.count+=t.count,n.features=n.features.concat(t.features)):n[q]=t}function f(n,q,t,u){q=Number(q);u=this.getStartTimeByUnit(q,u);n[u]?(n=n[u],n.count+=t.count,n.features=n.features.concat(t.features)):(t.originTime=q,n[u]=t)}function e(n,q,t){t[n[0].attributes[q]]=
{count:1,features:n};return t}var m=this.separateFeaturesWhetherFieldValueNull(a,b),p=m.notNullLabelFeatures;b={};b=this.getClusteringObjByField(m.nullLabelFeatures,a);m={};if(1===p.length)m=e.call(this,p,a,m,d);else if(0!==p.length){var g=this._getTimeTwixs(p,a,d.minPeriod);if(g){var k={},l=g.dateUnit;g.twixs.forEach(function(n){var q="undefined"!==typeof n.startValue?n.startValue:n.start().valueOf(),t="undefined"!==typeof n.endValue?n.endValue:n.end().valueOf(),u={unit:l,count:0,features:[]};p.forEach(w.hitch(this,
function(B){var y=B.attributes;y=y&&y[a];y>=q&&y<t&&(u.features.push(B),u.count++)}));d.isNeedFilled?c.call(this,k,q,u):0<u.count&&c.call(this,k,q,u)}.bind(this));g={};var h;for(h in k)if(k.hasOwnProperty(h)){var r=k[h];f.call(this,g,h,r,l)}for(h in g)if(g.hasOwnProperty(h)){r=g[h];var A=r.originTime;delete r.originTime;m[A]=r}}else m=e.call(this,p,a,m,d)}return{notNullLabel:m,nullLabel:b}},_mapOptions:function(a,b){"feature"===b?(b=a.labelField,delete a.labelField,a.clusterField=b,a.showNullLabelData=
!0):"category"===b?(b=a.categoryField,delete a.categoryField,a.clusterField=b,a.showNullLabelData=!0):"count"===b?(b=a.categoryField,delete a.categoryField,a.clusterField=b):"field"===b&&(a.showNullLabelData=!0);return a},_mapDataItemForStatisticsChart:function(a,b){var d,c,f;return a.map(function(e){d=e.label;delete e.label;c=e.values;delete e.values;f=e.features;delete e.features;"feature"===b?(e.category=d,e.valueFields=c,e.dataFeatures=f):"category"===b?(e.category=d,e.valueFields=c,e.dataFeatures=
f):"count"===b?(e.fieldValue=d,e.count=c[0],e.dataFeatures=f):"field"===b&&(e.label=d,e.value=c[0]);return e})},getFeatureModeStatisticsInfo:function(a){var b=new z({featureLayer:a.layerDefinition,popupInfo:a.popupFieldInfosObj});a=this._mapOptions(a,"feature");var d=a.clusterField,c=this.getFeatureModeStatisticsData(a);c=this.sortStatisticsData(c,"feature",a.sortOrder,d);c=this.getDataForMaxLabels(c,a.maxLabels);c=b.getBestLabelDisplay(c,d,"feature");c=b.keepStatisticsDataBestDecimalPlace(a,c,"feature");
return this._mapDataItemForStatisticsChart(c,"feature")},getCategoryModeStatisticsInfo:function(a){var b=new z({featureLayer:a.layerDefinition,popupInfo:a.popupFieldInfosObj});a=this._mapOptions(a,"category");var d=a.clusterField,c=this.getCategoryModeStatisticsData(a);c=this.sortStatisticsData(c,"category",a.sortOrder,null,a.valueFields);c=this._removeNaNDataItem(c);c=this.getDataForMaxLabels(c,a.maxLabels);c=b.getBestLabelDisplay(c,d,"category");c=b.keepStatisticsDataBestDecimalPlace(a,c,"category");
return this._mapDataItemForStatisticsChart(c,"category")},getCountModeStatisticsInfo:function(a){var b=new z({featureLayer:a.layerDefinition,popupInfo:a.popupFieldInfosObj});a=this._mapOptions(a,"count");var d=a.clusterField,c=this.getCountModeStatisticsData(a);c=this.sortStatisticsData(c,"count",a.sortOrder);c=this.getDataForMaxLabels(c,a.maxLabels);c=b.getBestLabelDisplay(c,d,"count");return this._mapDataItemForStatisticsChart(c,"count")},getFieldModeStatisticsInfo:function(a){var b=new z({featureLayer:a.layerDefinition,
popupInfo:a.popupFieldInfosObj});a=this._mapOptions(a,"category");var d=this.getFieldModeStatisticsData(a);d=this.sortStatisticsData(d,"field",a.sortOrder);d=this.getDataForMaxLabels(d,a.maxLabels);d=b.getBestLabelDisplay(d,null,"field");d=b.keepStatisticsDataBestDecimalPlace(a,d,"field");return this._mapDataItemForStatisticsChart(d,"field")}}});