// 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("exports dojo/_base/lang dojo/_base/array dojo/_base/html dojo/has dojo/Deferred jimu/utils esri/lang esri/tasks/QueryTask esri/tasks/query".split(" "),function(h,v,q,A,w,u,t,D,E,F){function B(a){var b=v.clone(a.attributes);(a=a.geometry)&&"point"===a.type&&("x"in b?b._x=a.x:b.x=a.x,"y"in b?b._y=a.y:b.y=a.y);return b}h.exportCSV=function(a,b,c,d,f){return h._createCSVStr(b,c,d,f).then(function(e){return h._download(a+".csv",e)})};h.exportCalculatedResultsCSV=function(a,b){var c="",d=0;q.forEach(b,
function(f){c+=f.name+" ("+f.type+")\r\n";h._createCSVStr([],[],f.appendColumns,f.appendDatas).then(function(e){c+=e;d++;if(d===b.length)return h._download(a+".csv",c);c+="\r\n\r\n"})})};h.exportCSVFromFeatureLayer=function(a,b,c){c=c||{};return h._getExportData(b,{datas:c.datas,fromClient:c.fromClient,withGeometry:c.withGeometry,outFields:c.outFields,filterExpression:c.filterExpression}).then(function(d){return h._formattedData(b,d,{formatNumber:c.formatNumber,formatDate:c.formatDate,formatCodedValue:c.formatCodedValue,
popupInfo:c.popupInfo,appendColumns:c.appendColumns,appendDatas:c.appendDatas}).then(function(f){return h.exportCSV(a,f.datas,f.columns,f.appendColumns,f.appendDatas)})})};h.exportCSVByAttributes=function(a,b,c,d){d=v.mixin({},d);d.datas=c;return h.exportCSVFromFeatureLayer(a,b,d)};h.exportCSVByGraphics=function(a,b,c,d){c=q.map(c,function(f){return f.attributes});return h.exportCSVByAttributes(a,b,c,d)};h._createCSVStr=function(a,b,c,d){var f=new u,e="",g=0,k=0,l="",n="";try{if(b&&0<b.length){q.forEach(b,
function(m){e=e+l+m;l=","});e+="\r\n";g=a.length;k=b.length;for(var r=0;r<g;r++){l="";for(var p=0;p<k;p++)(n=a[r][b[p]])||"number"===typeof n||(n=""),n&&/[",\r\n]/g.test(n)&&(n='"'+n.replace(/(")/g,'""')+'"'),e=e+l+n,l=",";e+="\r\n"}}"undefined"!==typeof c&&"undefined"!==typeof d&&0<c.length&&0<d.length&&(l="",q.forEach(c,function(m){e=e+l+m;l=","}),l="",e+="\r\n",q.forEach(d,function(m){Array.isArray(m)?(q.forEach(m,function(x){e=e+l+x;l=","}),l="",e+="\r\n"):(e=e+l+m,l=",")}));f.resolve(e)}catch(m){console.error(m),
f.resolve("")}return f};h._isIE11=function(){return 11===t.has("ie")};h._isEdge=function(){return t.has("edge")};h._getDownloadUrl=function(a){return window.Blob&&window.URL&&window.URL.createObjectURL?(a=new Blob(["\ufeff"+a],{type:"text/csv"}),URL.createObjectURL(a)):"data:attachment/csv;charset\x3dutf-8,\ufeff"+encodeURIComponent(a)};h._download=function(a,b){var c=new u;try{if(w("ie")&&10>w("ie")){var d=window.top.open("about:blank","_blank");d.document.write("sep\x3d,\r\n"+b);d.document.close();
d.document.execCommand("SaveAs",!0,a);d.close()}else if(10===w("ie")||h._isIE11()||h._isEdge()){var f=new Blob(["\ufeff"+b],{type:"text/csv"});navigator.msSaveBlob(f,a)}else{var e=A.create("a",{href:h._getDownloadUrl(b),target:"_blank",download:a},document.body);if(w("safari")){var g=document.createEvent("MouseEvents");g.initEvent("click",!0,!0);e.dispatchEvent(g)}else e.click();A.destroy(e)}c.resolve()}catch(k){c.reject(k)}return c};h._getExportData=function(a,b){var c=new u,d=null,f=b.datas,e=b.withGeometry;
d=b.outFields;d&&d.length||(d=a.fields);d=v.clone(d);if(e&&!(f&&0<f.length)){var g="";g=-1!==d.indexOf("x")?"_x":"x";d.push({name:g,alias:g,format:{digitSeparator:!1,places:6},show:!0,type:"esriFieldTypeDouble"});g=-1!==d.indexOf("y")?"_y":"y";d.push({name:g,alias:g,format:{digitSeparator:!1,places:6},show:!0,type:"esriFieldTypeDouble"})}f&&0<f.length?c.resolve({data:f||[],outFields:d}):b.fromClient?(f=q.map(a.graphics,function(k){return e?B(k):v.clone(k)}),c.resolve({data:f||[],outFields:d})):h._getExportDataFromServer(a,
d,b).then(function(k){c.resolve({data:k||[],outFields:d})});return c};h._getExportDataFromServer=function(a,b,c){var d=new u;if("esri.layers.FeatureLayer"!==a.declaredClass)return d.resolve([]),d;var f=new E(a.url),e=new F;e.where=c.filterExpression||a.getDefinitionExpression&&a.getDefinitionExpression()||"1\x3d1";0<b.length?(a=q.map(b,function(g){return g.name}),e.outFields=a):e.outFields=["*"];e.returnGeometry=c.withGeometry;f.execute(e,function(g){g=q.map(g.features,function(k){return B(k)});d.resolve(g)},
function(g){console.error(g);d.resolve([])});return d};h._formattedData=function(a,b,c){var d=new u,f=[],e=b.data;b=b.outFields;for(var g=t.getFeatureLayerDefinition(a),k=0,l=e.length;k<l;k++){for(var n={},r=0;r<b.length;r++){var p=b[r];n[p.alias||p.name]=h._getExportValue(e[k][p.name],p,a.objectIdField,a.typeIdField,e[k][a.typeIdField],a.types,c,g,e[k],a)}f.push(n)}a=q.map(b,function(m){return m.alias||m.name});d.resolve({datas:f,columns:a,appendColumns:c.appendColumns,appendDatas:c.appendDatas});
return d};h._getExportValue=function(a,b,c,d,f,e,g,k,l,n){function r(y){if(p&&D.isDefined(p.fieldInfos))for(var z=0,G=p.fieldInfos.length;z<G;z++){var C=p.fieldInfos[z];if(C.fieldName===y)return C.format}return null}var p=g.popupInfo,m=!!b.domain&&g.formatCodedValue;g="esriFieldTypeDate"===b.type&&g.formatDate;var x=c&&b.name===c;d=d&&b.name===d;n=n&&n.renderer?n:k;return g?(a=t.fieldFormatter.getFormattedDate(a,r(b.name)),-1<a.indexOf(",")?a.replace(",",""):a):(d||m)&&k&&l&&(k=t.getDisplayValueForCodedValueOrSubtype(n,
b.name,l))&&k.hasOwnProperty("displayValue")?k.displayValue:m||g||x||d?a:(m=null,c&&e&&0<e.length&&(c=(c=q.filter(e,function(y){return y.id===f}))&&c[0])&&c.domains&&c.domains[b.name]&&c.domains[b.name].codedValues&&(b=t.getDisplayValueForCodedValueOrSubtype(n,b.name,l))&&b.hasOwnProperty("displayValue")&&(m=b.displayValue),null!==m?m:a)}});