/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{segmentLength as e}from"../../../../arcade/functions/measures.js";import{getHeightUnitCorrectionFactor as t}from"../../../geometry/coordUtils.js";import{readOffsetDistance as r,readIsProportional as o,readVertexBeforeOffset as s,readDistance as n,makeFeatureFromGroupPart as i}from"./support/builderUtils.js";import{modeBasedOffset as a}from"./support/offsetUtils.js";import{pointAlongLineWithZAndOffset as p,pointAlongLineAndOffset as c,offsetPointFromSegment as l}from"./support/shapeUtils.js";import{isPolyline as f,maxDigitizingDisplayEdits as m}from"../support/executorUtils.js";import u from"../../../../geometry/Multipoint.js";import h from"../../../../geometry/Point.js";import{e as y}from"../../../../chunks/intersectsOperator.js";import{fromSpatialReference as g}from"../../../../geometry/operators/support/apiConverter.js";import{getPaths as d,getEndpoint as j}from"../../../../geometry/support/curves/curveUtils.js";function R(e){if(T(e)&&"digitizing"!==e.mode)return Z(e)}async function Z({templatePart:f,parentTemplate:u,shape:R,edits:Z,relationships:T,mode:U,xyTolerance:b,associationFeaturesByTemplate:v}){const{builderConfig:x}=f,w=r(x),C=o(x),F=s(x),P=n(x,C),V=F||0===w?R:a(R,w,U,b);if(null===V)return;const k=u.layer.createQuery();k.geometry=V,k.spatialRelationship="intersects",k.returnM=!0,k.returnZ=!0,k.returnTrueCurves=!0,k.outSpatialReference=V.spatialReference,k.cacheHint=!1,k.returnGeometry=!0,k.historicMoment=u.layer.historicMoment,k.gdbVersion=u.layer.gdbVersion;const z=2*(g(V.spatialReference)?.getTolerance()??.001);k.distance=z;const{features:B}=await u.layer.queryFeatures(k);if(0===B.length)return;const S=[];for(const e of B)S.push(M(e.geometry));const q=t(V.spatialReference);let G=0,H=0;const O=d(V);for(let t=0;t<O.length&&!(Z.length>m);t++){const r=O[t],o=r.length;for(let s=0;s<o;s++){const n=j(r[s]);let a=new h({x:n[0],y:n[1],spatialReference:V.spatialReference,hasZ:V.hasZ,hasM:V.hasM,...V.hasZ?{z:n[2]}:{},...V.hasM?{m:n[V.hasZ?3:2]}:{}});for(const r of S)if(y(a,r)){if(s>0&&0!==P&&(G+=e(j(O[t][s-1]),O[t][s],V.hasZ,q)),0!==P){let r=1;if(C){const n=P<0?s-1:s+1;if(n>=o||n<0)return;r=e(j(O[t][s]),O[t][n],V.hasZ,q)}const n=G+r*P;a=V.hasZ?p(V,n,w,q,b):c(V,n,w,b)}else 0!==w&&(a=s===o-1?l(a,j(O[t][s-1]),O[t][s],w,b):l(a,j(O[t][s]),O[t][s+1],w,b));a&&i({templatePart:f,shape:a,edits:Z,relationships:T,mode:U,associationFeaturesByTemplate:v},`V${H.toString()}`);break}H++}}}function M(e){const t=[];for(const r of d(e))for(const e of r){const r=j(e);t.push([...r])}return new u({spatialReference:e.spatialReference,points:t})}function T(e){return null!=e.parentTemplate&&f(e.shape)}const U=!0;export{R as execute,U as isAsync};
