// 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/declare dojo/Evented dojo/_base/array dojo/DeferredList dojo/Deferred dojo/_base/lang dojo/dom-class dojo/dom-construct dojo/on dojo/keys jimu/utils jimu/dijit/Message esri/graphic esri/layers/FeatureLayer esri/tasks/query ./analysisUtils".split(" "),function(G,H,D,B,w,p,u,v,z,C,x,E,I,A,F,r){return G("GroupedCountInfo",[H],{summaryLayer:null,summaryFields:[],summaryIds:[],summaryFeatures:[],tabNum:null,popupFields:[],groupedResults:{},specialFields:null,dateFields:{},symbolField:null,
graphicsLayer:null,lyrRenderer:null,lyrSymbol:null,featureCount:0,incidentCount:0,displayCount:!1,allFields:!1,configuredPopUpFields:[],constructor:function(a,c,b){this.tab=a;this.container=c;this.parent=b;this.config=b.config;this.graphicsLayer=null;this.specialFields={};this.typeIdField="";this.types=[];this.dateFields={};this.baseLabel=""!==a.label?a.label:a.layerTitle?a.layerTitle:a.layers;this.parentNode=b.domNode},queryTabCount:function(a,c,b,m){var d=new w;this.displayCount=m;this.incidentCount=
a.length;var f=[this.tab.tabLayers[0]];this.mapServiceLayer&&1<this.tab.tabLayers.length&&(f=[this.tab.tabLayers[1]]);if(0<this.tab.tabLayers.length&&this.tab.tabLayers[0].url&&-1<this.tab.tabLayers[0].url.indexOf("MapServer"))if(this.mapServiceLayer=!0,"undefined"!==typeof this.tab.tabLayers[0].infoTemplate)if(this.summaryLayer=this.tab.tabLayers[0],this.summaryLayer.hasOwnProperty("loaded")&&this.summaryLayer.loaded)this.summaryFields=this._getFields(this.summaryLayer),this._performQuery(a,c,b,
m,f).then(function(e){d.resolve(e)});else{var g=new A(this.summaryLayer.url);g.infoTemplate=this.tab.tabLayers[0].infoTemplate;f=[g];this.tab.tabLayers=f;z(g,"load",p.hitch(this,function(){this.summaryLayer=g;this.summaryFields=this._getFields(this.summaryLayer);this._performQuery(a,c,b,m,f).then(function(e){d.resolve(e)})}))}else this.loading||(g=new A(this.tab.tabLayers[0].url),this.loading=!0,z(g,"load",p.hitch(this,function(){this.summaryLayer=g;this.summaryFields=this._getFields(this.summaryLayer);
for(var e=this.tab.tabLayers[0].url.split("MapServer/")[1],k=this.parent.map.itemInfo.itemData.operationalLayers,l=0;l<k.length;l++){var h=k[l];if(-1<this.tab.tabLayers[0].url.indexOf(h.url)&&"undefined"!==typeof h.layerObject)if(h.layerObject.infoTemplates){if(h=h.layerObject.infoTemplates[e]){g.infoTemplate=h.infoTemplate;break}}else if(h.layerObject.infoTemplate){g.infoTemplate=h.layerObject.infoTemplate;break}}f=[g];this.tab.tabLayers=f;this.loading=!1;this._performQuery(a,c,b,m,f).then(function(n){d.resolve(n)})})));
this.mapServiceLayer||this._performQuery(a,c,b,m,f).then(function(e){d.resolve(e)});return d},_performQuery:function(a,c,b,m,d){var f=new w,g=[],e;0<c.length?e=r.getGeoms(c):0<a.length&&(e=r.getGeoms(a));this.summaryGeoms=e;if(0<e.length)for(a=0;a<e.length;a++){g=e[a];c=r.createDefArray(d,g,this.parent.opLayers,this.tab);var k=0===a?g=c:g=k.concat(c)}(new B(g)).then(p.hitch(this,function(l){for(var h=0,n=0;n<l.length;n++){var q=l[n][1];isNaN(q)?q&&q.features?h+=q.features.length:q&&"undefined"!==
typeof q.length&&(h+=q.length):h+=q}this.updateTabCount(h,b,m);this.queryOnLoad&&p.hitch(this,this._queryFeatures(this.summaryGeoms));f.resolve(h)}));return f},updateTabCount:function(a,c,b){this.displayCount=b;this.featureCount=a;r.updateTabCount(this.featureCount,c,b,this.baseLabel,this.incidentCount)},updateForIncident:function(a,c,b,m,d,f,g){this.incidentCount=a.length;this.allFields="undefined"!==typeof f&&"undefined"!==typeof g?f?!0:g:!1;var e="undefined"!==typeof d;this.tabNum=m;if(e)var k=
new w;else this.container.innerHTML="",u.add(this.container,"loading");this.summaryIds=[];this.summaryFeatures=[];this.groupedResults={};if(0<this.tab.tabLayers.length){var l=[];if(0<c.length)l=c;else for(c=0;c<a.length;c++)m=a[c].geometry?a[c].geometry:a[c],"polygon"===m.type&&l.push(m);if("undefined"!==typeof this.tab.tabLayers[0].infoTemplate){this.summaryLayer=this.tab.tabLayers[0];var h=new A(this.summaryLayer.url);h.infoTemplate=this.tab.tabLayers[0].infoTemplate;this.tab.tabLayers[1]=h;this.summaryFields=
this._getFields(this.tab.tabLayers[0]);this.configuredPopUpFields=r.getPopupConfiguredFields(this.tab.tabLayers[0]);e?this._queryFeatures(l,e).then(function(n){k.resolve(n)}):(this._initGraphicsLayer(b),p.hitch(this,this._queryFeatures(l)))}else h=new A(this.tab.tabLayers[0].url),z(h,"load",p.hitch(this,function(){this.summaryLayer=h;if(-1<this.tab.tabLayers[0].url.indexOf("MapServer"))for(var n=this.tab.tabLayers[0].url.split("MapServer/")[1],q=this.parent.map.itemInfo.itemData.operationalLayers,
y=0;y<q.length;y++){var t=q[y];if(-1<this.tab.tabLayers[0].url.indexOf(t.url)&&"undefined"!==typeof t.layerObject&&t.layerObject.infoTemplates&&(t=t.layerObject.infoTemplates[n])){h.infoTemplate=t.infoTemplate;break}}this.tab.tabLayers[1]=h;this.summaryLayer=this.tab.tabLayers[1];this.summaryFields=this._getFields(this.tab.tabLayers[1]);this.configuredPopUpFields=r.getPopupConfiguredFields(this.tab.tabLayers[1]);e?this._queryFeatures(l,e).then(function(J){k.resolve(J)}):(this._initGraphicsLayer(b),
p.hitch(this,this._queryFeatures(l)))}));if(e)return k}},_initGraphicsLayer:function(a){null!==a&&(this.graphicsLayer=a,this.graphicsLayer.clear(),this.summaryLayer&&this.summaryLayer.renderer&&(this.lyrRenderer=this.summaryLayer.renderer,this.graphicsLayer.renderer=this.lyrRenderer,"undefined"!==typeof this.summaryLayer.renderer.attributeField?this.symbolField=this.summaryLayer.renderer.attributeField:this.lyrSymbol=this.lyrRenderer.symbol))},_queryFeatures:function(a,c){var b;c&&(b=new w);var m=
[],d=-1===[null,void 0,""].indexOf(this.tab.tabLayers[0].id)?this.tab.tabLayers[0].id:this.tab.layers;d=r.getFilter(d,this.parent.opLayers);for(var f=new F,g=0;g<a.length;g++)f.geometry=a[g],f.where=d,m.push(this.summaryLayer.queryIds(f));(new B(m)).then(p.hitch(this,function(e){for(var k,l,h=0;h<e.length;h++)e[h][0]&&(k=e[h][1],l=k=0===h?k:l.concat(k));k?(this.summaryIds=k,0<this.summaryIds.length?c?this._queryFeaturesByIds(c).then(function(n){b.resolve(n)}):this._queryFeaturesByIds():c||this._processResults()):
c||this._processResults()}),p.hitch(this,function(e){console.error(e);new E({message:e})}));if(c)return b},_queryFeaturesByIds:function(a){var c,b=[];a&&(c=new w);var m=this.summaryLayer.maxRecordCount||1E3,d=this.summaryIds.slice(0,m);this.summaryIds.splice(0,m);var f=new F,g=-1===[null,void 0,""].indexOf(this.summaryLayer.id)?this.summaryLayer.id:this.tab.layers;f.where=r.getFilter(g,this.parent.opLayers);var e=!1;D.some(this.summaryFields,p.hitch(this,function(k){if("area"===k.type||"length"===
k.type||this.graphicsLayer)return e=!0}));a&&(e=!0);this.summaryLayer.supportsAdvancedQueries&&(f.orderByFields=[this.summaryFields[0].field]);f.returnGeometry=e;f.outSpatialReference=this.parent.map.spatialReference;f.outFields=["*"];f.objectIds=d;for(b.push(this.summaryLayer.queryFeatures(f));0<this.summaryIds.length;)d=this.summaryIds.slice(0,m),this.summaryIds.splice(0,m),f.objectIds=d,b.push(this.summaryLayer.queryFeatures(f));(new B(b)).then(p.hitch(this,function(k){this.summaryFeatures=[];
for(var l=0;l<k.length;l++)if(k[l][0]){var h=k[l][1];h.features&&(this.summaryFeatures=this.summaryFeatures.concat(h.features))}a?this._processResults(a).then(p.hitch(this,function(n){this.SA_SAT_download&&u.replace(this.SA_SAT_download,"download","processing");c.resolve(n)})):(this._processResults(),this.SA_SAT_download&&u.replace(this.SA_SAT_download,"download","processing"));this.SA_SAT_download&&u.replace(this.SA_SAT_download,"download","processing")}),p.hitch(this,function(k){console.error(k);
new E({message:k})}));if(a)return c},_prepGroupedResults:function(){for(var a=0;a<this.summaryFeatures.length;a++){var c=this.summaryFeatures[a];if("undefined"!==typeof this.summaryFields&&0<this.summaryFields.length){var b=r.getFieldValue(this.summaryFields[0].field,c.attributes[this.summaryFields[0].field],this.specialFields,this.dateFields,"longMonthDayYear",this.typeIdField,this.types,this.layerObject&&this.layerObject.renderer?this.layerObject:this.layerDefinition,c.attributes);b="undefined"!==
typeof b&&null!==b?x.stripHTML(b.toString()):"";b in this.groupedResults?this.groupedResults[b].features.push(c):this.groupedResults[b]={features:[c]}}}},_prepResults:function(){for(var a in this.groupedResults){var c=this.summaryFields[0];c.total=this.groupedResults[a].features.length;this.groupedResults[a].total=c.total;this.groupedResults[a].type=c.type;this.groupedResults[a].label=c.alias}},_processResults:function(a){this._prepGroupedResults();this._prepResults();var c=this.groupedResults,b=
0;if(a)var m=new w;else{this.container.innerHTML="";u.remove(this.container,"loading");if(0===Object.keys(this.groupedResults).length){this.container.innerHTML=this.parent.nls.noFeaturesFound;return}var d=Object.keys(this.groupedResults).length+1;var f=v.create("div",{style:"width:"+220*d+"px;"},this.container);u.add(f,"SAT_tabPanelContent");d=v.create("div",{},f);u.add(d,"SATcolExport");u.add(d,this.parent.lightTheme?"lightThemeBorder":"darkThemeBorder");var g=v.create("div",{"data-dojo-attach-point":"SA_SAT_download",
title:this.parent.nls.downloadCSV,tabindex:"0",role:"button","aria-label":this.parent.nls.downloadCSVdownloadCSV,"class":"grpSummaryDownLoadCSVButton"},d);u.add(g,[this.parent.isBlackTheme?"btnExportBlack":"btnExport","download"]);u.add(g,this.parent.lightTheme?"lightThemeBackground":"darkThemeBackground");g.focus();x.initFirstFocusNode(this.parentNode,g);x.initLastFocusNode(this.parentNode,g);z(g,"click",p.hitch(this,this._exportToCSV,c));z(g,"keydown",p.hitch(this,function(t){if(!t.shiftKey||t.keyCode!==
C.TAB)if(t.keyCode===C.ENTER||t.keyCode===C.SPACE)this._exportToCSV(c,t),setTimeout(function(){g.focus()},500)}))}var e=Object.keys(c).sort();d=[];this.displayCount&&d.push({total:this.featureCount,a:void 0,info:this.parent.nls.count,c:void 0});for(var k in e){b=e[k];var l=c[b],h=x.stripHTML(b.toString());b=l.total;isNaN(b)&&(b=0);b=x.localizeNumber(b);var n="pre"===l.type?l.label.trim():h;h="pre"===l.type?h:l.label.trim();l=""!==l.label?"colGroupedSummary":"colSummary";if(a)d.push({total:b,a:n,info:""===
h?n:h,c:l});else{var q=v.create("div",{"class":"SATcol"},f);u.add(q,this.parent.lightTheme?"lightThemeBorder":"darkThemeBorder");var y=v.create("div",{style:"max-height: 45px;"},q);v.create("div",{"class":"SATcolWrap",style:"max-height: 30px; overflow: hidden;",innerHTML:n},y);v.create("div",{"class":"SATcolWrap",style:"max-height: 30px; overflow: hidden;",innerHTML:h},y);v.create("div",{"class":l,innerHTML:b},q)}}k=[];f=null!==this.graphicsLayer;!a&&f&&(this.graphicsLayer.clear(),this.tab.tabLayers[1]&&
this.tab.tabLayers[1].clear());if(this.summaryFeatures)for(e=0;e<this.summaryFeatures.length;e++)b=this.summaryFeatures[e],this.lyrSymbol?b.symbol=this.lyrSymbol:this.graphicsLayer?this.graphicsLayer.renderer&&(n=this.graphicsLayer.renderer.getSymbol(b),b.symbol=n):this.summaryLayer.renderer&&this.summaryLayer.renderer.getSymbol&&(b.symbol=this.summaryLayer.renderer.getSymbol(b)),b=b.toJson?new I(b.toJson()):b,!a&&f?(this.graphicsLayer.add(b),this.tab.tabLayers[1].add(b)):k.push(b);!a&&f&&(this.graphicsLayer.setVisibility(!0),
this.parent._toggleTabLayersNew(this.tabNum),this.tab.retsore&&this.emit("summary-complete",{bubbles:!0,cancelable:!0,tab:this.tabNum}));if(a)return m.resolve({graphics:k,analysisResults:d,context:this}),m},_exportToCSV:function(a,c,b,m){if(this.parent.config.hasOwnProperty("exportFieldsOptionForCSV"))var d=this.parent.config.exportFieldsOptionForCSV;else this.parent.config.hasOwnProperty("csvAllFields")&&(d=this.parent.config.csvAllFields);a=r.exportToCSV(this.summaryFeatures,c,b,m,{type:"grouped",
baseLabel:this.baseLabel,csvAllFields:d,layer:this.summaryLayer,opLayers:this.parent.opLayers,nlsValue:this.parent.nls.groupedSummary,nlsCount:this.parent.nls.count,summaryFields:this.summaryFields,allFields:this.allFields,configuredPopUpFields:this.configuredPopUpFields});this.summaryLayer=a.summaryLayer;return a.details},_getFields:function(a){this.layerDefinition=x.getFeatureLayerDefinition(a);this.layerObject=a;var c=r.getSkipFields(a),b,m=[];if("undefined"!==typeof this.tab.advStat){var d=this.tab.advStat.stats,
f;for(f in d)0<d[f].length&&D.forEach(d[f],function(e){var k={field:e.expression,alias:e.label+"",type:f,total:0};b=e.expression;m.push(k)})}d=r.getSpecialFields(a);this.dateFields=d.dateFields;this.specialFields=d.specialFields;this.typeIdField=d.typeIdField;this.types=d.types;if(this.allFields)for(d=0;d<a.fields.length;d++){var g=a.fields[d];-1===c.indexOf(g.name)&&b!==g.name&&m.push({field:g.name,alias:g.alias,type:g.type})}return m}})});