// 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 esri/tasks/QueryTask ./analysisUtils".split(" "),function(G,H,B,C,v,p,t,u,x,y,z,E,I,A,F,J,q){return G("SummaryInfo",[H],{summaryLayer:null,summaryFields:[],summaryIds:[],summaryFeatures:[],tabNum:null,symbolField:null,graphicsLayer:null,lyrRenderer:null,lyrSymbol:null,
featureCount:0,mapServiceLayer:!1,loading:!1,queryOnLoad:!1,incidentCount:0,allFields:!1,configuredPopUpFields:[],constructor:function(a,e,d){this.tab=a;this.container=e;this.parent=d;this.config=d.config;this.graphicsLayer=null;this.baseLabel=""!==a.label?a.label:a.layerTitle?a.layerTitle:a.layers;this.parentNode=d.domNode},queryTabCount:function(a,e,d,f){var l=new v;this.incidentCount=a.length;var g=[this.tab.tabLayers[0]];this.mapServiceLayer&&1<this.tab.tabLayers.length&&(g=[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,e,d,f,g).then(function(c){l.resolve(c)});else{var h=new A(this.summaryLayer.url);h.infoTemplate=this.tab.tabLayers[0].infoTemplate;
g=[h];this.tab.tabLayers=g;x(h,"load",p.hitch(this,function(){this.summaryLayer=h;this.summaryFields=this._getFields(this.summaryLayer);this._performQuery(a,e,d,f,g).then(function(c){l.resolve(c)})}))}else this.loading||(h=new A(this.tab.tabLayers[0].url),this.loading=!0,x(h,"load",p.hitch(this,function(){this.summaryLayer=h;this.summaryFields=this._getFields(this.summaryLayer);for(var c=this.tab.tabLayers[0].url.split("MapServer/")[1],b=this.parent.map.itemInfo.itemData.operationalLayers,n=0;n<b.length;n++){var k=
b[n];if(-1<this.tab.tabLayers[0].url.indexOf(k.url)&&"undefined"!==typeof k.layerObject)if(k.layerObject.infoTemplates){if(k=k.layerObject.infoTemplates[c]){h.infoTemplate=k.infoTemplate;break}}else if(k.layerObject.infoTemplate){h.infoTemplate=k.layerObject.infoTemplate;break}}g=[h];this.tab.tabLayers=g;this.loading=!1;this._performQuery(a,e,d,f,g).then(function(m){l.resolve(m)})})));this.mapServiceLayer||this._performQuery(a,e,d,f,g).then(function(c){l.resolve(c)});return l},_performQuery:function(a,
e,d,f,l){var g=new v,h=[],c;0<e.length?c=q.getGeoms(e):0<a.length&&(c=q.getGeoms(a));this.summaryGeoms=c;if(0<c.length)for(a=0;a<c.length;a++){h=c[a];e=q.createDefArray(l,h,this.parent.opLayers,this.tab);var b=0===a?h=e:h=b.concat(e)}(new C(h)).then(p.hitch(this,function(n){for(var k=0,m=0;m<n.length;m++){var r=n[m][1];isNaN(r)?r&&r.features?k+=r.features.length:r&&"undefined"!==typeof r.length&&(k+=r.length):k+=r}this.updateTabCount(k,d,f);this.queryOnLoad&&p.hitch(this,this._queryFeatures(this.summaryGeoms));
g.resolve(k)}));return g},updateTabCount:function(a,e,d){this.featureCount=a;q.updateTabCount(this.featureCount,e,d,this.baseLabel,this.incidentCount)},updateForIncident:function(a,e,d,f,l,g,h){this.incidentCount=a.length;this.allFields="undefined"!==typeof g&&"undefined"!==typeof h?g?!0:h:!1;var c="undefined"!==typeof l;this.tabNum=f;if(c)var b=new v;else this.container.innerHTML="",t.add(this.container,"loading");this.summaryIds=[];this.summaryFeatures=[];if(0<this.tab.tabLayers.length){var n=this.summaryGeoms;
if("undefined"!==typeof this.tab.tabLayers[0].infoTemplate){this.summaryLayer=this.tab.tabLayers[0];var k=new A(this.summaryLayer.url);k.infoTemplate=this.tab.tabLayers[0].infoTemplate;this.tab.tabLayers[1]=k;this.summaryFields=this._getFields(this.tab.tabLayers[0]);this.configuredPopUpFields=q.getPopupConfiguredFields(this.tab.tabLayers[0]);c?this._queryFeatures(n,c).then(function(m){b.resolve(m)}):(this._initGraphicsLayer(d),p.hitch(this,this._queryFeatures(n)))}else k=new A(this.tab.tabLayers[0].url),
x(k,"load",p.hitch(this,function(){this.summaryLayer=k;if(-1<this.tab.tabLayers[0].url.indexOf("MapServer"))for(var m=this.tab.tabLayers[0].url.split("MapServer/")[1],r=this.parent.map.itemInfo.itemData.operationalLayers,D=0;D<r.length;D++){var w=r[D];if(-1<this.tab.tabLayers[0].url.indexOf(w.url)&&"undefined"!==typeof w.layerObject&&w.layerObject.infoTemplates&&(w=w.layerObject.infoTemplates[m])){k.infoTemplate=w.infoTemplate;break}}this.tab.tabLayers[1]=k;this.summaryLayer=this.tab.tabLayers[1];
this.summaryFields=this._getFields(this.tab.tabLayers[1]);this.configuredPopUpFields=q.getPopupConfiguredFields(this.tab.tabLayers[1]);c?this._queryFeatures(n,c).then(function(K){b.resolve(K)}):(this._initGraphicsLayer(d),p.hitch(this,this._queryFeatures(n)))}));if(c)return b}},_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,e){var d;e&&(d=new v);var f=[],l=-1===[null,void 0,""].indexOf(this.tab.tabLayers[0].id)?this.tab.tabLayers[0].id:this.tab.layers;l=q.getFilter(l,this.parent.opLayers);for(var g=new F,h=0;h<a.length;h++)g.geometry=a[h],g.where=l,f.push(this.summaryLayer.queryIds(g));(new C(f)).then(p.hitch(this,function(c){for(var b,
n,k=0;k<c.length;k++)c[k][0]&&(b=c[k][1],n=b=0===k?b:n.concat(b));b?(this.summaryIds=b,0<this.summaryIds.length?e?this._queryFeaturesByIds(e).then(function(m){d.resolve(m)}):this._queryFeaturesByIds():e||this._processResults()):e||this._processResults()}),p.hitch(this,function(c){console.error(c);new E({message:c})}));if(e)return d},_queryFeaturesByIds:function(a){var e,d=[];a&&(e=new v);var f=this.summaryLayer.maxRecordCount||1E3,l=this.summaryIds.slice(0,f);this.summaryIds.splice(0,f);var g=new F;
g.where=q.getFilter(this.summaryLayer.id,this.parent.opLayers);var h=!1;B.some(this.summaryFields,p.hitch(this,function(b){if("area"===b.type||"length"===b.type||this.graphicsLayer)return h=!0}));a&&(h=!0);g.returnGeometry=h;g.outSpatialReference=this.parent.map.spatialReference;g.outFields=["*"];g.objectIds=l;var c=new J(this.summaryLayer.url);for(d.push(c.execute(g));0<this.summaryIds.length;)l=this.summaryIds.slice(0,f),this.summaryIds.splice(0,f),g.objectIds=l,d.push(c.execute(g));(new C(d)).then(p.hitch(this,
function(b){this.summaryFeatures=[];for(var n=0;n<b.length;n++)if(b[n][0]){var k=b[n][1];k.features&&(this.summaryFeatures=this.summaryFeatures.concat(k.features))}a?this._processResults(a).then(p.hitch(this,function(m){this.SA_SAT_download&&t.replace(this.SA_SAT_download,"download","processing");e.resolve(m)})):(this._processResults(),this.SA_SAT_download&&t.replace(this.SA_SAT_download,"download","processing"));this.SA_SAT_download&&t.replace(this.SA_SAT_download,"download","processing")}),p.hitch(this,
function(b){console.error(b);new E({message:b})}));if(a)return e},_prepResults:function(){for(var a=0;a<this.summaryFields.length;a++){var e=this.summaryFields[a],d=e.field,f=e.total;switch(e.type){case "count":f=this.summaryFeatures.length;break;case "area":f=q.getArea(this.summaryFeatures,this.summaryGeoms,this.config.distanceSettings,this.config.distanceUnits,this.tab.advStat);break;case "length":f=q.getLength(this.summaryFeatures,this.summaryGeoms,this.config.distanceSettings,this.config.distanceUnits,
this.tab.advStat);break;case "sum":f=q.getSum(this.summaryFeatures,d);break;case "avg":f=q.getSum(this.summaryFeatures,d)/this.summaryFeatures.length;break;case "min":f=q.getMin(this.summaryFeatures,d);break;case "max":f=q.getMax(this.summaryFeatures,d)}e.total=f}},_processResults:function(a){this._prepResults();var e=this.summaryFields,d=0;if(a)var f=new v;else{this.container.innerHTML="";t.remove(this.container,"loading");if(0===this.summaryFeatures.length){this.container.innerHTML=this.parent.nls.noFeaturesFound;
return}var l=u.create("div",{style:"width:"+220*(e.length+1)+"px;"},this.container);t.add(l,"SAT_tabPanelContent");var g=u.create("div",{},l);t.add(g,"SATcolExport");t.add(g,this.parent.lightTheme?"lightThemeBorder":"darkThemeBorder");var h=u.create("div",{"data-dojo-attach-point":"SA_SAT_download",title:this.parent.nls.downloadCSV,tabindex:"0",role:"button","aria-label":this.parent.nls.downloadCSV,"class":"summaryDownLoadCSVButton"},g);z.initFirstFocusNode(this.parentNode,h);z.initLastFocusNode(this.parentNode,
h);t.add(h,[this.parent.isBlackTheme?"btnExportBlack":"btnExport","download"]);t.add(h,this.parent.lightTheme?"lightThemeBackground":"darkThemeBackground");h.focus();x(h,"click",p.hitch(this,this._exportToCSV,e));x(h,"keydown",p.hitch(this,function(m){if(!m.shiftKey||m.keyCode!==y.TAB)if(m.keyCode===y.TAB&&h.focus(),m.keyCode===y.ENTER||m.keyCode===y.SPACE)this._exportToCSV(e,m),setTimeout(function(){h.focus()},500)}))}g=[];for(var c=0;c<e.length;c++){var b=e[c],n=z.stripHTML(b.alias?b.alias:"")+
"\x3cbr/\x3e";b=q.formatNumber(b.total,b);d=b.total;b=b.num;if(a)g.push({num:b,info:n,total:d});else{d=u.create("div",{"class":"SATcol"},l);t.add(d,this.parent.lightTheme?"lightThemeBorder":"darkThemeBorder");var k=u.create("div",{style:"max-height: 60px;"},d);u.create("div",{"class":" SATcolWrap",style:"max-height: 30px; overflow: hidden;",innerHTML:n},k);u.create("div",{"class":" colSummary",innerHTML:b},d)}}l=[];c=null!==this.graphicsLayer;!a&&c&&(this.graphicsLayer.clear(),this.tab.tabLayers[1].clear());
if(this.summaryFeatures)for(n=0;n<this.summaryFeatures.length;n++)b=this.summaryFeatures[n],this.lyrSymbol?b.symbol=this.lyrSymbol:c&&this.graphicsLayer.renderer?(d=this.graphicsLayer.renderer.getSymbol(b),b.symbol=d):this.summaryLayer.renderer&&this.summaryLayer.renderer.getSymbol&&(b.symbol=this.summaryLayer.renderer.getSymbol(b)),b=b.toJson?new I(b.toJson()):b,!a&&c?(this.graphicsLayer.add(b),this.tab.tabLayers[1].add(b)):l.push(b);!a&&c&&(this.graphicsLayer.setVisibility(!0),this.parent._toggleTabLayersNew(this.tabNum),
this.tab.restore&&this.emit("summary-complete",{bubbles:!0,cancelable:!0,tab:this.tabNum}));if(a)return f.resolve({graphics:l,analysisResults:g,context:this}),f},_exportToCSV:function(a,e,d,f){if(this.parent.config.hasOwnProperty("exportFieldsOptionForCSV"))var l=this.parent.config.exportFieldsOptionForCSV;else this.parent.config.hasOwnProperty("csvAllFields")&&(l=this.parent.config.csvAllFields);a=q.exportToCSV(this.summaryFeatures,e,d,f,{type:"summary",baseLabel:this.baseLabel,csvAllFields:l,layer:this.summaryLayer,
opLayers:this.parent.opLayers,nlsValue:this.parent.nls.summary,nlsCount:this.parent.nls.count,summaryFields:this.summaryFields,calcFields:this.calcFields,allFields:this.allFields,configuredPopUpFields:this.configuredPopUpFields});this.summaryLayer=a.summaryLayer;return a.details},_getFieldInfoByFieldInfo:function(a,e,d){return this._getFieldInfo(a[e],e,d)},_getFieldInfoByName:function(a,e){var d=["count","area","length","tabCount"],f;for(f in a)if(-1===d.indexOf(f))return this._getFieldInfo(a[f],
f,e)},_getFieldInfo:function(a,e,d){for(var f=0;f<a.length;f++){var l=a[f];if(l.expression&&l.expression===d)return{field:{field:l.expression,alias:l.label,type:e,modify:l.modify,round:l.round,roundPlaces:l.roundPlaces,truncate:l.truncate,truncatePlaces:l.truncatePlaces,total:0,show1KSeparator:"undefined"!==typeof l.show1KSeparator?l.show1KSeparator:!0},index:f}}},_getFields:function(a){this.layerDefinition=z.getFeatureLayerDefinition(a);this.layerObject=a;var e=q.getSkipFields(a),d=[];if("undefined"!==
typeof this.tab.advStat){var f=p.clone(this.tab.advStat.stats);this.tab.advStat.fieldOrder&&B.forEach(this.tab.advStat.fieldOrder,p.hitch(this,function(m){if((m=m&&m.hasOwnProperty("fieldName")?this._getFieldInfoByFieldInfo(f,m.fieldType,m.fieldName):this._getFieldInfoByName(f,m))&&m.field){var r=f[m.field.type];r.splice(m.index,1);0===r.length&&delete f[m.field.type];d.push(m.field)}}));for(var l in f)0<f[l].length&&B.forEach(f[l],function(m){d.push({field:m.expression,alias:m.label,type:l,modify:m.modify,
round:m.round,roundPlaces:m.roundPlaces,truncate:m.truncate,truncatePlaces:m.truncatePlaces,total:0,show1KSeparator:"undefined"!==typeof m.show1KSeparator?m.show1KSeparator:!0})})}else{if(a.infoTemplate)var g=a.infoTemplate.info.fieldInfos;else if(-1<this.tab.tabLayers[0].url.indexOf("MapServer")){var h=this.tab.tabLayers[0].url.split("MapServer/")[1],c=this.parent.map.itemInfo.itemData.operationalLayers;g=null;for(var b=0;b<c.length;b++){var n=c[b];if(n.layerObject.infoTemplates&&(n=n.layerObject.infoTemplates[h])){g=
n.infoTemplate.info.fieldInfos;break}}}else g=a.fields;g||(g=a.fields);for(h=0;h<g.length;h++)if(c=g[h],"undefined"!==typeof a.fields){b=a.fields[h].type;var k;c.name===a.objectIdField||"esriFieldTypeDouble"!==b&&"esriFieldTypeInteger"!==b&&"esriFieldTypeSmallInteger"!==b||("undefined"!==typeof c.visible?c.visible&&(k={field:c.fieldName,alias:c.label,type:"sum",total:0}):k={field:c.name,alias:c.alias,type:"sum",total:0},k&&-1===e.indexOf(k.field)&&d.push(k),k=null)}}this.calcFields=p.clone(d);if(this.allFields)for(g=
0;g<a.fields.length;g++){k=a.fields[g];h=!0;c=0;b:for(;c<d.length;c++)if(k.name===d[c].field){h=!1;break b}-1===e.indexOf(k.name)&&h&&d.push({field:k.name,alias:k.alias,type:k.type})}a=q.getSpecialFields(a);this.dateFields=a.dateFields;this.specialFields=a.specialFields;this.typeIdField=a.typeIdField;this.types=a.types;return d}})});