/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../../Graphic.js";import t from"../../core/Error.js";import r from"../../core/Logger.js";import{getMetersPerCartesianUnitForSR as a}from"../../core/units.js";import{parseData as s}from"../../core/urlUtils.js";import{isEqualBaseGCS as o,canProjectWithoutEngine as n,projectWithoutEngine as l}from"../../geometry/projectionUtils.js";import{fromJSON as i}from"../../geometry/support/jsonUtils.js";import u from"../../geometry/support/MeshTransform.js";import{equals as d}from"../../geometry/support/spatialReferenceUtils.js";import{isFeatureIdentifierArrayWithGlobalId as c,isFeatureIdentifierArrayWithObjectId as p}from"./editingSupport.js";async function m(e,t,r){const{geometry:s}=t,i={...t.attributes};if(null!=r&&"mesh"===s?.type){const{transformFieldRoles:t}=r,{origin:c,spatialReference:p,vertexSpace:m}=s,f=s.transform??new u,g="local"===m.type,b=e.spatialReference,y=b.isGeographic,R=d(b,p),h=o(p,b)&&n(p,b);if(!(g&&y&&h||!g&&!y&&R))return null;const I=l(c,p,b);if(null==I)return null;if(i[t.originX]=I.x,i[t.originY]=I.y,i[t.originZ]=I.z??0,null!=f){const{translation:e,scale:r,rotation:s}=f,o=g?1:a(p)/a(b);i[t.translationX]=e[0]*o,i[t.translationY]=e[2]*o,i[t.translationZ]=-e[1]*o,i[t.scaleX]=r[0],i[t.scaleY]=r[2],i[t.scaleZ]=r[1],i[t.rotationX]=s[0],i[t.rotationY]=s[2],i[t.rotationZ]=-s[1],i[t.rotationDeg]=s[3]}return{attributes:i}}return null==s?{attributes:i}:"mesh"===s.type||"extent"===s.type?null:{geometry:s.toJSON(),attributes:i}}async function f(e,t){const r=await Promise.all((t.addAttachments??[]).map(t=>g(e,t))),a=await Promise.all((t.updateAttachments??[]).map(t=>g(e,t))),s=t.deleteAttachments??[];return r.length||a.length||s.length?{adds:r,updates:a,deletes:[...s]}:null}async function g(e,t){const{feature:r,attachment:a}=t,{globalId:o,name:n,contentType:l,data:i,uploadId:u}=a,d={globalId:o};if(r&&("attributes"in r?d.parentGlobalId=r.attributes?.[e.globalIdField]:r.globalId&&(d.parentGlobalId=r.globalId)),u)d.uploadId=u;else if(i){const e=await s(i);e&&(d.contentType=e.mediaType,d.data=e.data),i instanceof File&&(d.name=i.name)}return n&&(d.name=n),l&&(d.contentType=l),d}function b(e,t,r){if(!t||0===t.length)return[];if(r&&c(t))return t.map(e=>e.globalId);if(p(t))return t.map(e=>e.objectId);const a=r?e.globalIdField:e.objectIdField;return a?t.map(e=>e.getAttribute(a)):[]}function y(e){const t=e?.assetMaps;if(t){for(const e of t.addResults)e.success||r.getLogger("esri.layers.graphics.sources.support.sourceUtils").error(`Failed to map asset to feature with globalId ${e.globalId}.`);for(const e of t.updateResults)e.success||r.getLogger("esri.layers.graphics.sources.support.sourceUtils").error(`Failed to map asset to feature with globalId ${e.globalId}.`)}const a=e?.attachments,s={addFeatureResults:e?.addResults?.map(R)??[],updateFeatureResults:e?.updateResults?.map(R)??[],deleteFeatureResults:e?.deleteResults?.map(R)??[],addAttachmentResults:a?.addResults?a.addResults.map(R):[],updateAttachmentResults:a?.updateResults?a.updateResults.map(R):[],deleteAttachmentResults:a?.deleteResults?a.deleteResults.map(R):[]};return e?.editMoment&&(s.editMoment=e.editMoment),s}function R(e){const r=!0===e.success?null:e.error||{code:void 0,description:"Feature edit failed"};return{objectId:e.objectId,globalId:e.globalId,error:r?new t("feature-layer-source:edit-failure",r.description,{code:r.code}):null}}function h(t,r){return new e({attributes:t.attributes,geometry:i({...t.geometry,spatialReference:r})})}function I(e,t){return{adds:e?.adds?.map(e=>h(e,t))||[],updates:e?.updates?.map(e=>({original:h(e[0],t),current:h(e[1],t)}))||[],deletes:e?.deletes?.map(e=>h(e,t))||[],spatialReference:t}}function j(e){const t=e.details.raw,r=+t.code,a=+t.extendedCode;return 500===r&&(-2147217144===a||-2147467261===a)}export{I as createEditedFeatures,R as createFeatureEditResult,f as getAttachmentEditsJSON,b as getFeatureIds,m as getFeatureJSON,j as isProtectedOrPrivateVersionError,y as unpackEditResultData};
