// 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/_base/lang dojo/Deferred dojo/DeferredList dojo/_base/array jimu/utils jimu/dijit/SnapShot ./CSVUtils".split(" "),function(v,p,q,t,w,u,x,y){return v("SnapShotUtils",null,{portal:null,portalUrl:"",logo:"",originMapId:"",originAppId:"",credential:null,nls:null,layerArray:[],parent:null,downloadAll:!1,time:null,constructor:function(a){this.parent=a;this.snapshot=new x(a.appConfig,a.map);this.map=this.parent.map;this.extent=this.map.extent;this.nls=p.mixin({},a.nls,window.jimuNls.drawBox,
window.jimuNls.snapshot)},createSnapShot:function(a){var f=new q,g=u.stripHTML(a.name),b=this._getTime(a.time);this.createLayerItems(a,b,g).then(p.hitch(this,function(d){this.snapshot.createSnapShot({folderOptions:{folderName:g+"_"+b,title:g+"_"+b,description:g+" "+this.nls.snapshot},mapTitle:g+" ("+this.nls.snapshot_append+" "+b+")",name:g+" ("+b+")",shareWith:{everyone:!1,org:!1,groups:a.groups.join()},mapExtent:this.map.extent,data:d}).then(function(){f.resolve()})}));return f},_getTime:function(a){a=
new Date(a);var f=a.getTimezoneOffset();return u.fieldFormatter.getFormattedDate(a,{dateFormat:"shortDateShortTime"})+" "+this.nls.utc+(0>f?"+"+Math.abs(f)/60:"-"+f/60)},createLayerItems:function(a,f,g){var b=new q,d=a.layers.reverse();this.buffers=a.buffers;this.incidents=a.incidents;a=[];for(var c=0;c<d.length;c++){var h=!0;d[c].analysisObject&&"undefined"!==typeof d[c].analysisObject.featureCount&&0===d[c].analysisObject.featureCount&&(h=!1);d[c].graphics&&0===d[c].graphics.length&&(h=!1);h&&a.push(this.createItem(d[c],
this.incidents,this.buffers,f,this.nls,g))}var k=[];(new t(a)).then(p.hitch(this,function(e){for(var m=0;m<e.length;m++){var l=e[m][1];if(Array.isArray(l))for(var n=0;n<l.length;n++)k.push(l[n]);else k.push(l)}b.resolve(k)}),p.hitch(this,function(e){b.reject(e)}));return b},createItem:function(a,f,g,b,d,c){var h=new q,k={label:a.label,title:a.label+"_"+b,desc:d.snapshot_append+" "+d.of_append+" "+(a.type?a.type:a.label)+" "+d.layer_append+" "+a.label+" ("+b+")",name:a.label+"("+b+")",tags:[c+","+
d.snapshot_append],originalLayerName:a.label};if(a.layerObject){var e=a.layerObject;c=a.analysisObject;if(e.infoTemplate&&e.infoTemplate.info)var m=e.infoTemplate.info;k.popupInfo=m;"groupedSummary"===a.type||"summary"===a.type?c.updateForIncident(f,g,null,null,!0,!0,!0).then(p.hitch(this,function(l){l=this.createAnalysisLayerJSON(l,e,d,b,k);h.resolve(l)})):c.updateForIncident(f,"closest"===a.type?this.parent.config.maxDistance:g,null,!0,!0,!0).then(p.hitch(this,function(l){l=this.createAnalysisLayerJSON(l,
e,d,b,k);h.resolve(l)}))}else a=this.createIncidentBufferLayerJSON(a.graphics,d,b,k),h.resolve(a);return h},createAnalysisLayerJSON:function(a,f,g,b,d){g=a.graphics;b=a.context._exportToCSV(g,!0);a=[];for(var c=0;c<b.length;c++){var h=b[c];"esriFieldTypeOID"!==h.type&&a.push(h)}for(b=0;b<g.length;b++)c=g[b],c.geometry.cache&&(c.geometry.clearCache(),delete c.geometry.cahce);return{graphics:g,renderer:f.renderer,infoTemplate:d.popupInfo,fields:a,tags:d.tags,description:d.desc,name:d.name,visibleOnStartup:!1,
typeIdField:f.typeIdField,types:f.types,minScale:f.minScale,maxScale:f.maxScale,originalLayerName:d.originalLayerName}},createIncidentBufferLayerJSON:function(a,f,g,b){var d=[],c=[],h=[];f=[];w.forEach(a,function(l){switch(l.geometry.type){case "point":d.push(l);break;case "polyline":c.push(l);break;case "polygon":h.push(l)}});a=[];0<d.length&&a.push(d);0<c.length&&a.push(c);0<h.length&&a.push(h);g={point:this.nls.point,polyline:this.nls.line,polygon:this.nls.polygon};for(var k=0;k<a.length;k++){var e=
a[k];if(0<e.length){var m=e[0];m=g["undefined"!==typeof m.geometry?m.geometry.type:m.type];f.push({graphics:e,fields:[],tags:b.tags,description:b.desc,name:1===a.length?b.name:m+" "+b.name,visibleOnStartup:!1,originalLayerName:b.originalLayerName})}}return f},createDownloadZip:function(a,f,g){var b=new q,d=this.nls.calculated_results;this._performAnalysis(a,f,g,this.downloadAll,!1).then(function(c){for(var h=[],k=0;k<c.length;k++){var e=c[k];(e=e.context._exportToCSV(e.graphics,!1,!0,e.analysisResults))&&
h.push(e)}0<h.length&&y.exportCalculatedResultsCSV(d,h);b.resolve("success")},function(c){b.reject(c)});return b},_performAnalysis:function(a,f,g,b,d){for(var c=new q,h=[],k=0;k<a.length;k++){var e=a[k];console.log("AO: "+e);var m=!0;e.analysisObject&&"undefined"!==typeof e.analysisObject.featureCount&&0===e.analysisObject.featureCount&&(m=!1);m&&("groupedSummary"===e.type||"summary"===e.type?h.push(e.analysisObject.updateForIncident(f,g,null,null,!0,d,b)):h.push(e.analysisObject.updateForIncident(f,
"closest"===e.type?this.parent.config.maxDistance:g,null,!0,d,b)))}var l=[];(new t(h)).then(p.hitch(this,function(n){for(var r=0;r<n.length;r++)l.push(n[r][1]);c.resolve(l)}),p.hitch(this,function(n){console.error(n);c.reject(n)}));return c}})});