// 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/_base/array dojo/json dojo/Deferred esri/tasks/query esri/tasks/QueryTask esri/tasks/FeatureSet esri/graphic esri/SpatialReference esri/tasks/ProjectParameters esri/config esri/geometry/webMercatorUtils jimu/LayerInfos/LayerInfos ./utils ./CSVUtils ./GeojsonConverters libs/polyfills/FileSaver".split(" "),function(y,f,k,v,q,z,A,r,n,B,C,t,D,E,F,w,G){function x(a,b){10>dojo.isIE?saveTextAs(b,a,"utf-8"):(b=new Blob([b],{type:"text/plain;charset\x3dutf-8"}),
saveAs(b,a,!0))}var p={createDataSource:function(a){return new H(a)},TYPE_TABLE:"table",TYPE_FEATURESET:"FeatureSet",FORMAT_CSV:"CSV",FORMAT_FEATURESET:"FeatureSet",FORMAT_GEOJSON:"GeoJSON"},H=y(null,{filename:void 0,format:void 0,nls:void 0,data:null,url:null,constructor:function(a){this.nls=window.jimuNls.exportTo;this.data=a.data;this.url=a.url;this.filename=a.filename},setFormat:function(a){this.format=a},download:function(){if(this.format===p.FORMAT_CSV)return this.exportCSV();if(this.format===
p.FORMAT_FEATURESET)return this.exportFeatureCollection();if(this.format===p.FORMAT_GEOJSON)return this.exportGeoJSON()},getFeatureSet:function(){var a=new q;if(this.data)a.resolve(this.data);else if(this.url){var b=new z;b.returnGeometry=!0;b.outFields=["*"];this.queryTask=new A(this.url);this.queryTask.execute(b,f.hitch(this,function(c){a.resolve(c)}),f.hitch(this,function(){a.resolve(null)}))}else a.resolve(null);return a},findPopupInfo:function(a){if(!a||!a.features||0===a.features.length)return null;
a=a.features[0];if(a._layer&&(a=a._layer.id,a=E.getInstanceSync().getLayerInfoById(a))){var b=a.getPopupInfo();b||(b=a.layerObject.infoTemplate&&a.layerObject.infoTemplate.info);return b}return null},findLayerDefinition:function(a){if(!a||!a.features||0===a.features.length)return null;var b=a.features[0];if(b._layer)return{drawingInfo:b._layer.drawingInfo,geometryType:a.geometryType,fields:b._layer.fields,objectIdField:b._layer.objectIdField,typeIdField:b._layer.typeIdField,types:b._layer.types};
var c=[];b=b.attributes;for(var e in b)b.hasOwnProperty(e)&&c.push({name:e});return{geometryType:a.geometryType,fields:c}},_getOutFields:function(a,b){return f.clone(a.fields&&0<a.fields.length?a.fields:b.fields)},formatAttributes:function(a){var b=new q,c=this.findPopupInfo(a),e=this.findLayerDefinition(a);if(c&&e){var g=k.map(a.features,function(d){return d.attributes});w._formattedData(a.features[0]._layer,{data:g,outFields:this._getOutFields(a,e)},{formatNumber:!0,formatDate:!0,formatCodedValue:!0,
popupInfo:c,richText:{}}).then(f.hitch(this,function(d){var h=d.datas;d=new r;var m=[];k.forEach(a.features,function(l,u){l=new n(l.toJson());l.attributes=h[u];m.push(l)});d.features=m;d.geometryType=a.geometryType;d.fieldAliases=a.fieldAliases;d.fields=a.fields;b.resolve(d)}))}else b.resolve(a);return b},exportCSV:function(){return this.getFeatureSet().then(f.hitch(this,function(a){var b=this.findPopupInfo(a),c=this.findLayerDefinition(a),e=a.features;if(c&&c.fields){var g=this._getOutFields(a,c);
this._addXYAttribute(g,"x");this._addXYAttribute(g,"y");c.fields=g;e=[];k.forEach(a.features,f.hitch(this,function(d){d=new n(d.toJson());d.attributes=this._getAttrsWithXY(d);e.push(d)}))}else for(g in c.fields=[],e[0].attributes)c.fields.push({name:g,alias:g});return w.exportCSVByGraphics(this.filename,c,e,{formatNumber:!0,formatDate:!0,formatCodedValue:!0,popupInfo:b})}))},exportGeoJSON:function(){return this.getFeatureSet().then(f.hitch(this,function(a){return this.formatAttributes(a)})).then(f.hitch(this,
function(a){return this._projectToWGS84(a)})).then(f.hitch(this,function(a){var b="";if(a&&a.features&&0<a.features.length){var c={type:"FeatureCollection",features:[]};k.forEach(a.features,function(e){c.features.push(G.arcgisToGeoJSON(e))});b=v.stringify(c)}return b})).then(f.hitch(this,function(a){x(this.filename+".geojson",a)}))},exportFeatureCollection:function(){return this.getFeatureSet().then(f.hitch(this,function(a){return this.formatAttributes(a)})).then(f.hitch(this,function(a){var b="";
a&&(a=a.toJson())&&(b=v.stringify(a));return b})).then(f.hitch(this,function(a){x(this.filename+".json",a)}))},exportToPortal:function(a,b){},_projectToWGS84:function(a){var b=new q,c=this._getSpatialReference(a);if(c)if(4326===parseInt(c.wkid,10))b.resolve(a);else if(c.isWebMercator()){c=new r;var e=[];k.forEach(a.features,function(d){var h=new n(d.toJson());h.geometry=D.webMercatorToGeographic(d.geometry);e.push(h)});c.features=e;c.geometryType=a.geometryType;c.fieldAliases=a.fieldAliases;c.fields=
a.fields;b.resolve(c)}else{c=new C;c.geometries=k.map(a.features,function(d){return d.geometry});c.outSR=new B(4326);var g=t&&t.defaults&&t.defaults.geometryService;g&&"esri.tasks.GeometryService"===g.declaredClass||(g=F.getArcGISDefaultGeometryService());g.project(c).then(function(d){var h=new r,m=[];k.forEach(a.features,function(l,u){l=new n(l.toJson());l.geometry=d[u];m.push(l)});h.features=m;h.geometryType=a.geometryType;h.fieldAliases=a.fieldAliases;h.fields=a.fields;b.resolve(h)},function(d){console.error(d);
b.resolve([])})}else b.resolve([]);return b},_getSpatialReference:function(a){if(a.spatialReference)return a.spatialReference;var b;k.some(a.features,function(c){if(c.geometry&&c.geometry.spatialReference)return b=c.geometry.spatialReference,!0});return b},_getAttrsWithXY:function(a){var b=a.geometry;return b&&"point"===b.type?(a=f.clone(a.attributes),"x"in a?a._x=b.x:a.x=b.x,"y"in a?a._y=b.y:a.y=b.y,a):a.attributes},_addXYAttribute:function(a,b){var c=k.some(a,function(e){return e.name===b})?"_"+
b:b;a.push({name:c,alias:c,format:{digitSeparator:!1,places:6},show:!0,type:"esriFieldTypeDouble"});return a}});return p});