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