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