/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{getHeightUnitCorrectionFactor as t}from"../../../../geometry/coordUtils.js";import{segmentLength3d as e}from"../../../../geometry/lineUtils.js";import{makeFeatureFromGroupPart as s}from"./builderUtils.js";import{pointAlongLineWithZAndOffset as o,pointAlongLineAndOffset as a,pointFromPolylineVertex as i,offsetPointFromSegment as r,computedTotalLength as n}from"./shapeUtils.js";import{maxDigitizingDisplayEdits as p}from"../../support/executorUtils.js";import{execute as h}from"../../../../../geometry/operators/offsetOperator.js";import{getLength as l}from"../../../../../geometry/support/coordsUtils.js";import{isPolyline as f}from"../../../../../geometry/support/jsonTypeUtils.js";function m({templatePart:n,edits:h,relationships:m,shape:g,distance:u,isProportional:d,offsetDistance:j,vertexBeforeOffset:y,vertexPlacement:P,mode:U,associationFeaturesByTemplate:x}){const B=y||0===j?g:c(g,j);if(null===B||!f(B))return;const T=t(B.spatialReference);j=y?j:0;let V=0,Z=null,v=0;for(let t=0;t<B.paths.length;t++){if(h.length>p&&"digitizing"===U)return;const f=B.paths[t].length,c=t===B.paths.length-1;for(let g=0;g<f;g++){if(h.length>p&&"digitizing"===U)return;if(g>0&&0!==u&&(V+=B.hasZ?e(B.paths[t][g-1],B.paths[t][g],T):l(B.paths[t][g-1],B.paths[t][g])),(0!==g||1!==P)&&!(g===f-1&&2===P&&c||(0===g||g===f-1&&c)&&3===P||0===g&&u<0&&d||g===f-1&&u>0&&d)){if(0!==u){let s=1;if(d){const o=u<0?g-1:g+1;if(o>=f||o<0)continue;s=B.hasZ?e(B.paths[t][g],B.paths[t][o],T):l(B.paths[t][g],B.paths[t][o])}const i=V+s*u;Z=B.hasZ?o(B,i,j,T):a(B,i,0)}else Z=i(B,t,g),0!==j&&(Z=r(Z,g===f-1?[B.paths[t][g-1],B.paths[t][g]]:[B.paths[t][g],B.paths[t][g+1]],j));Z&&s({templatePart:n,shape:Z,edits:h,relationships:m,mode:U,associationFeaturesByTemplate:x},`V${(v+g).toString()}`)}}v+=f}}function c(t,e){return h(t,e,{unit:"meters",joins:"miter",miterLimit:2})}function g(t){let e=0;for(const s of t.paths)e+=s.length;return e}function u({templatePart:p,edits:h,relationships:m,shape:u,distance:d,isProportional:j,offsetDistance:y,vertexBeforeOffset:P,vertexPlacement:U,mode:x,associationFeaturesByTemplate:B}){const T=P||0===y?u:c(u,y);if(null===T||!f(T))return;const V=t(T.spatialReference);y=P?y:0;let Z=null,v="";if(0!==d){const t=4===U?0:T.paths.length-1,s=4===U?0:T.paths[T.paths.length-1].length-1,i=4===U?0:n(T),r=4===U?T.paths[0].length:T.paths[T.paths.length-1].length;let p=1;if(j){const o=d<0?s-1:s+1;if(o>=r||o<0)return;p=T.hasZ?e(T.paths[t][s],T.paths[t][o],V):l(T.paths[t][s],T.paths[t][o])}const h=i+p*d;Z=T.hasZ?o(T,h,y,V):a(T,h,y),v=4===U?"V0":`V${(g(T)-1).toString()}`}else{if(Z=4===U?i(T,0,0):i(T,T.paths.length-1,T.paths[T.paths.length-1].length-1),0!==y)if(4===U)Z=r(Z,[T.paths[0][0],T.paths[0][1]],y);else{const t=T.paths[T.paths.length-1];Z=r(Z,[t[t.length-2],t[t.length-1]],y)}v=4===U?"V0":`V${(g(T)-1).toString()}`}Z&&s({templatePart:p,shape:Z,edits:h,relationships:m,mode:x,associationFeaturesByTemplate:B},v)}export{m as lineVerticesOffset,c as offset,u as vertexOffsetFromStartOrEndOfLine};
