/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../../../Graphic.js";import a from"../../../../core/Error.js";import{generateBracedUUID as t}from"../../../../core/uuid.js";import{isLoadedSharedTemplate as s}from"../../../templateUtils.js";import{isRadial as r}from"./executorUtils.js";import n from"../../../../geometry/Multipoint.js";import i from"../../../../geometry/Point.js";import o from"../../../../geometry/Polygon.js";import h from"../../../../geometry/Polyline.js";import{isCurvedGeometry as l,isCoordinate as u,cloneCurve as f,isBezierCurve as p,isCircularArc as d}from"../../../../geometry/support/curves/curveUtils.js";import{isSubtypeGroupLayer as c}from"../../../../layers/support/layerUtils.js";function m({edits:n,geometry:i,relationships:o,tag:l="",template:u}){if(r(i)){const e=i.geometry.clone();for(const a of e.paths){m({geometry:new h({spatialReference:e.spatialReference,paths:[a],hasZ:e.hasZ,hasM:e.hasM}),template:u,edits:n,relationships:o,tag:l})}return}const{definition:f,layer:p}=u;i=Z(i,p);const d=new e({attributes:f.defaultValues?{...f.defaultValues}:{},geometry:i,sourceLayer:p});if(j(d,p),p.globalIdField&&(d.attributes[p.globalIdField]=t()),n.push({id:p.layerId,graphic:d,tag:l,layer:p}),0!==f.relationships.length)for(const r of f.relationships||[]){const i=r.template;if(null==r.relationshipMetadata||!s(i))throw new a("shared-template:missing-relationship-metadata-or-definition","Relationship part must have metadata and a fully loaded template with definition");const{layer:h}=i,l=new e({attributes:{...f.defaultValues},sourceLayer:h});j(l,h),h.globalIdField&&(l.attributes[h.globalIdField]=t()),n.push({graphic:l,id:h.layerId,tag:"",layer:h}),o.push({...r.relationshipMetadata,sourceGraphic:d,destinationGraphic:l})}}function Z(e,a){if(!e)return null;if(e.hasZ===a.hasZ&&e.hasM===a.hasM)return e;switch(e.type){case"point":return new i({spatialReference:e.spatialReference,x:e.x,y:e.y,...a.hasZ?e.hasZ?{z:e.z}:{z:b(a)}:{},...a.hasM?e.hasM?{m:e.m}:{m:R(a)??void 0}:{}});case"polygon":return new o({spatialReference:e.spatialReference,...l(e)?{curveRings:y(e.curveRings,{hasZ:a.hasZ,hasM:a.hasM,defaultZ:b(a),defaultM:R(a),mIndex:e.hasM?e.hasZ?3:2:-1,zIndex:e.hasZ?2:-1})}:{rings:M(e.rings,{hasZ:a.hasZ,hasM:a.hasM,defaultZ:b(a),defaultM:R(a),mIndex:e.hasM?e.hasZ?3:2:-1,zIndex:e.hasZ?2:-1})},hasZ:a.hasZ,hasM:a.hasM});case"polyline":return new h({spatialReference:e.spatialReference,...l(e)?{curvePaths:y(e.curvePaths,{hasZ:a.hasZ,hasM:a.hasM,defaultZ:b(a),defaultM:R(a),mIndex:e.hasM?e.hasZ?3:2:-1,zIndex:e.hasZ?2:-1})}:{paths:M(e.paths,{hasZ:a.hasZ,hasM:a.hasM,defaultZ:b(a),defaultM:R(a),mIndex:e.hasM?e.hasZ?3:2:-1,zIndex:e.hasZ?2:-1})},hasZ:a.hasZ,hasM:a.hasM});case"multipoint":return new n({spatialReference:e.spatialReference,hasZ:a.hasZ,hasM:a.hasM,points:g(e.points,{hasZ:a.hasZ,hasM:a.hasM,defaultZ:b(a),defaultM:R(a),mIndex:e.hasM?e.hasZ?3:2:-1,zIndex:e.hasZ?2:-1})});default:return e}}function M(e,a){const t=[];for(const s of e)t.push(g(s,a));return t}function g(e,a){const t=[];for(const s of e){const e=[s[0],s[1]];a.hasZ&&(e[2]=a.zIndex>-1?s[2]:a.defaultZ),a.hasM&&(a.mIndex>-1?e[a.hasZ?3:2]=s[a.mIndex]:e[a.hasZ?3:2]=a.defaultM),t.push(e)}return t}function y(e,a){return e.map(e=>I(e,a))}function I(e,a){const t=[];for(const s of e)if(u(s)){const e=x(s,a);t.push(e)}else{const e=f(s);p(e)?e.b[0]=x(e.b[0],a):d(e)?e.c[0]=x(e.c[0],a):e.a[0]=x(e.a[0],a),t.push(e)}return t}function x(e,a){const t=[e[0],e[1]];return a.hasZ&&(t[2]=a.zIndex>-1?e[2]:a.defaultZ),a.hasM&&(a.mIndex>-1?t[a.hasZ?3:2]=e[a.mIndex]:t[a.hasZ?3:2]=a.defaultM),t}function b(e){return e.capabilities.editing?.zDefault??0}function R(e){return e.capabilities.editing.supportsUpdateWithoutM?void 0:0}function j(e,a){c(a)&&(e.sourceLayer=a.findSublayerForFeature(e))}export{m as createFeatureServiceEdit};
