import{FeatureSaveError as e}from"../data/FeatureSaveError.js";import{getGlobalIdField as t,getReturnServiceEditsOption as s,isGlobalIdUsed as r}from"../mapping/support/layers.js";import{isFeatureLayer as l,isSubtypeGroupLayer as n}from"../utilities/_layers.js";import{cancelify as o}from"./Cancellable.js";export async function applyEdits(a,u,i){const d={addFeatures:[],updateFeatures:[],deleteFeatures:[]},{adds:c=[],updates:p=[],deletes:f=[]}=a,y=[],m=[],h=[],g=l(u)||n(u)?t(u):void 0,{objectIdField:F}=u;for(const e of c)d.addFeatures.push(e.toGraphic()),y.push(e);for(const e of p){const t=e.toGraphic();e.source?.schema.fieldExtensions.forEach((e=>{e.field.editable||e.field.name===F||e.field.name===g||delete t.attributes[e.field.name]})),d.updateFeatures.push(t),m.push(e)}for(const e of f)"string"==typeof e?(d.deleteFeatures.push({objectId:e}),h.push({primaryKey:e})):(d.deleteFeatures.push(e.toGraphic()),h.push(e));if(0===y.length&&0===m.length&&0===h.length)return;const b=l(u)||n(u)?r(u,d):void 0,E=u.applyEdits(d,{globalIdUsed:b,returnServiceEditsOption:s(u)}),R=await o(E,i?.cancelToken);if(null==R||(R.addFeatureResults?.length??0)!==y.length||(R.updateFeatureResults?.length??0)!==m.length||(R.deleteFeatureResults?.length??0)!==h.length)throw new e("Received malformed response from server while saving feature(s).");const w=new e("Errors encountered when editing features.");if(applyEditsProcessResults(u,y,R.addFeatureResults,"add",w),applyEditsProcessResults(u,m,R.updateFeatureResults,"update",w),applyEditsProcessResults(u,h,R.deleteFeatureResults,"delete",w),null!=R.editedFeatureResults&&function(e,t,s){if(null==t.adds&&null==t.updates)return;for(const r of s)r.layerId===e.layerId&&applyEditsProcessEditedFeatures(e,t,r.editedFeatures)}(u,{adds:y,updates:m},R.editedFeatureResults),w.errors.size>0)throw w}export function applyEditsProcessResults(e,s,r,o,a){if(null!=r&&null!=s)for(const[u,i]of r.entries()){const r=s[u];if(null!=r)if(null==i.error){if("add"===o&&"schema"in r){const{primaryKeyField:s}=r.schema;if(r.attributes.set(s,i.objectId),l(e)||n(e)){const{globalId:s}=i;if(null!=s){const l=t(e);null!=l&&r.attributes.set(l,s)}}}}else{const{primaryKey:e}=r,t="title"in r?r.title:"";a.errors.set(e??`${t}-${u}`,new Error(`Failed to ${o} feature '${t}' with id ${e}. ${i.error.name} : ${i.error.message}`))}}}export function applyEditsProcessEditedFeatures(e,t,s){const r=(t,s)=>{const r=t.getAttribute(e.objectIdField);null!=r&&s.set(r.toString(),t)};if(null!=s.adds&&null!=t.adds){const e=s.adds.reduce(((e,t)=>(r(t,e),e)),new Map);for(const s of t.adds){if(null==s.primaryKey)continue;const t=e.get(s.primaryKey);null!=t&&a(s,t)}}if(null!=s.updates&&null!=t.updates){const e=s.updates.reduce(((e,t)=>{if(null==t.current)return e;if(Array.isArray(t.current))for(const s of t.current)r(s,e);else{const s=t.current;r(s,e)}return e}),new Map);for(const s of t.updates){if(null==s.primaryKey)continue;const t=e.get(s.primaryKey);null!=t&&a(s,t)}}}function a(e,t){for(const[s,r]of Object.entries(t.attributes))e.attributes.get(s)!==r&&e.attributes.set(s,r);null!=t.geometry&&(e.geometry=t.geometry.clone())}