/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../chunks/_rollupPluginBabelHelpers","../core/Error","../layers/support/Field"],(function(e,t,n,r){"use strict";function i(e){return Object.keys(e.attributes).map((t=>{const n=e.attributes[t];return"objectid"===t.toLowerCase()||"fid"===t.toLowerCase()?new r({name:t,alias:t,type:"oid"}):new r("number"==typeof n?{name:t,alias:t,type:"double"}:{name:t,alias:t,type:"string"})}))}function o(e){if(!e.fields){const t=e.features[0];if(t.layer){e.fields=t.layer.fields;const n=Object.keys(t.attributes),r=e.fields.filter((e=>n.includes(e.name)));e.fields=r}else e.fields=i(t)}return e}function a(e){return e.map((({attributes:e})=>e))}function s(e){const{delimiter:t,fields:n=[],outFields:r=[]}=e,i=t||",",o=n.map((e=>e.name));return e=>{let t="";return o.filter((e=>r.includes(e))).forEach((r=>{const o=n.find((({name:e})=>e===r));let a=e[r]||"";if("date"===o.type&&(a=new Date(a).toString()),o.domain&&"coded-value"===o.domain.type){const e=o.domain.codedValues.find((e=>a===e.code));e&&(a=e.name)}"string"==typeof a&&a.includes(i)&&(a=`"${a}"`),t+=`${a}${i}`})),`${t}\r\n`}}function u(e){return l.apply(this,arguments)}function l(){return(l=t._asyncToGenerator((function*(e,{includeGeometry:t=!0,delimiter:i=",",outFields:u=["*"]}={}){if(e=o(e),t&&"point"!==e.geometryType)throw new n("export-csv:invalid-geometries",`the input geometry ${e.geometryType} is not supported, must be point`);const{features:l}=e;if(!l.length)return null;let d=e.fields;const[c]=u;"*"===c&&(u=d.map((e=>e.name))),t&&"point"===e.geometryType&&(d.some((e=>"x"===e.name||"y"===e.name))||(d=[...d,new r({name:"lon",alias:"Longitude",type:"double"}),new r({name:"lat",alias:"Latitude",type:"double"})],u=[...u,"lon","lat"]),l.forEach((e=>{e.attributes.lon=e.geometry.longitude,e.attributes.lat=e.geometry.latitude}))),d=d.filter((e=>u.includes(e.name)));const f=i||",",p=a(l),m=d.map((e=>e.name)).join(f),y=s({delimiter:f,outFields:u||d.map((e=>e.name)),fields:d});let g=`${m}${f}\r\n`;p.forEach((e=>{g+=y(e)}));const b=new RegExp(`${f}\r\ns*$`,"g");return g.replace(b,"")}))).apply(this,arguments)}function d(e,t){return c.apply(this,arguments)}function c(){return(c=t._asyncToGenerator((function*(e,t){if(!e&&e.features&&!e.features.length)throw new n("csv-export:features-required","exportFeaturesToCSV requires features to export");return u(e,t)}))).apply(this,arguments)}e.attributeToString=s,e.convertFeaturesToCSV=u,e.exportFeaturesToCSV=d,e.extractAttributes=a,e.extractFieldsFromFeature=i,e.validateFeatureSetFields=o,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
