/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
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 r,pointFromPolylineVertex as i,offsetPointFromSegment as n,computedTotalLength as a}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:a,edits:h,relationships:m,shape:c,distance:u,isProportional:d,offsetDistance:j,vertexBeforeOffset:P,vertexPlacement:U,mode:x}){const y=P||0===j?c:g(c,j);if(null===y||!f(y))return;const V=t(y.spatialReference);j=P?j:0;let Z=0,v=null,O=0;for(let t=0;t<y.paths.length;t++){if(h.length>p&&"digitizing"===x)return;const f=y.paths[t].length,g=t===y.paths.length-1;for(let c=0;c<f;c++){if(h.length>p&&"digitizing"===x)return;if(c>0&&0!==u&&(Z+=y.hasZ?e(y.paths[t][c-1],y.paths[t][c],V):l(y.paths[t][c-1],y.paths[t][c])),(0!==c||1!==U)&&!(c===f-1&&2===U&&g||(0===c||c===f-1&&g)&&3===U||0===c&&u<0&&d||c===f-1&&u>0&&d)){if(0!==u){let s=1;if(d){const o=u<0?c-1:c+1;if(o>=f||o<0)continue;s=y.hasZ?e(y.paths[t][c],y.paths[t][o],V):l(y.paths[t][c],y.paths[t][o])}const i=Z+s*u;v=y.hasZ?o(y,i,j,V):r(y,i,0)}else v=i(y,t,c),0!==j&&(v=n(v,c===f-1?[y.paths[t][c-1],y.paths[t][c]]:[y.paths[t][c],y.paths[t][c+1]],j));v&&s({templatePart:a,shape:v,edits:h,relationships:m,mode:x},`V${(O+c).toString()}`)}}O+=f}}function g(t,e){return h(t,e,{unit:"meters",joins:"miter",miterLimit:2})}function c(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:P,vertexBeforeOffset:U,vertexPlacement:x,mode:y}){const V=U||0===P?u:g(u,P);if(null===V||!f(V))return;const Z=t(V.spatialReference);P=U?P:0;let v=null,O="";if(0!==d){const t=4===x?0:V.paths.length-1,s=4===x?0:V.paths[V.paths.length-1].length-1,i=4===x?0:a(V),n=4===x?V.paths[0].length:V.paths[V.paths.length-1].length;let p=1;if(j){const o=d<0?s-1:s+1;if(o>=n||o<0)return;p=V.hasZ?e(V.paths[t][s],V.paths[t][o],Z):l(V.paths[t][s],V.paths[t][o])}const h=i+p*d;v=V.hasZ?o(V,h,P,Z):r(V,h,P),O=4===x?"V0":`V${(c(V)-1).toString()}`}else{if(v=4===x?i(V,0,0):i(V,V.paths.length-1,V.paths[V.paths.length-1].length-1),0!==P)if(4===x)v=n(v,[V.paths[0][0],V.paths[0][1]],P);else{const t=V.paths[V.paths.length-1];v=n(v,[t[t.length-2],t[t.length-1]],P)}O=4===x?"V0":`V${(c(V)-1).toString()}`}v&&s({templatePart:p,shape:v,edits:h,relationships:m,mode:y},O)}export{m as lineVerticesOffset,g as offset,u as vertexOffsetFromStartOrEndOfLine};
