/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{segmentLength as e}from"../../../../../arcade/functions/measures.js";import{getHeightUnitCorrectionFactor as t}from"../../../../geometry/coordUtils.js";import{segmentLength3d as o}from"../../../../geometry/lineUtils.js";import{makeFeatureFromGroupPart as r}from"./builderUtils.js";import{pointAlongLineWithZAndOffset as s,pointAlongLineAndOffset as n,pointFromPolylineVertex as i,offsetPointFromSegment as l,computedTotalLength as a}from"./shapeUtils.js";import{maxDigitizingDisplayEdits as m}from"../../support/executorUtils.js";import{execute as f}from"../../../../../geometry/operators/offsetOperator.js";import{getLength as p}from"../../../../../geometry/support/coordsUtils.js";import{isPolyline as c}from"../../../../../geometry/support/jsonTypeUtils.js";import{fromJSON as u}from"../../../../../geometry/support/jsonUtils.js";import{getPaths as g,getEndpoint as h,isCurvedGeometry as d,isCoordinate as y}from"../../../../../geometry/support/curves/curveUtils.js";import{densifyCurvedGeometry as j}from"../../../../../geometry/support/curves/densifyCurvedGeometry.js";function x({templatePart:o,edits:a,relationships:f,shape:p,distance:u,isProportional:d,offsetDistance:y,vertexBeforeOffset:j,vertexPlacement:x,mode:v,xyTolerance:P,associationFeaturesByTemplate:U}){const Z=p,B=j||0===y?p:T(p,y,v,P);if(null===B||!c(B))return;const S=t(Z.spatialReference);y=j?y:0;let V=0,F=null,O=0;const $=g(Z);for(let t=0;t<$.length;t++){if(a.length>m&&"digitizing"===v)return;const p=$[t].length,c=t===$.length-1;for(let g=0;g<p;g++){if(a.length>m&&"digitizing"===v)return;if(g>0&&0!==u&&(V+=e(h($[t][g-1]),$[t][g],Z.hasZ,S)),(0!==g||1!==x)&&!(g===p-1&&2===x&&c||(0===g||g===p-1&&c)&&3===x||0===g&&u<0&&d||g===p-1&&u>0&&d)){if(0!==u){let o=1;if(d){const r=u<0?g-1:g+1;if(r>=p||r<0)continue;o=e(h($[t][g]),$[t][r],Z.hasZ,S)}const r=V+o*u;F=Z.hasZ?s(B,r,y,S,P):n(B,r,0,P)}else F=i(Z,t,g),0!==y&&(F=g===p-1?l(F,h($[t][g-1]),$[t][g],y,P):l(F,h($[t][g]),$[t][g+1],y,P));F&&r({templatePart:o,shape:F,edits:a,relationships:f,mode:v,associationFeaturesByTemplate:U},`V${(O+g).toString()}`)}}O+=p}}function v(e,t){return f(e,t,{unit:"meters"})}function P(e){let t=0;for(const o of g(e))t+=o.length;return t}function U({templatePart:m,edits:f,relationships:u,shape:j,distance:x,isProportional:v,offsetDistance:U,vertexBeforeOffset:Z,vertexPlacement:B,mode:S,xyTolerance:V,associationFeaturesByTemplate:F}){const O=j,$=Z||0===U?j:T(j,U,S,V);if(null===$||!c($))return;const z=t(O.spatialReference),C=g(O),D=d(O);U=Z?U:0;let L=null,R="";if(0!==x){const t=4===B?0:C.length-1,r=4===B?0:C[C.length-1].length-1,i=4===B?0:a(O),l=4===B?C[0].length:C[C.length-1].length;let m=1;if(v){const s=x<0?r-1:r+1;if(s>=l||s<0)return;m=!D||y(C[t][r])&&y(C[t][s])?O.hasZ?o(C[t][r],C[t][s],z):p(C[t][r],C[t][s]):s<r?e(h(C[t][s]),C[t][r],O.hasZ,z):e(h(C[t][r]),C[t][s],O.hasZ,z)}const f=i+m*x;L=O.hasZ?s($,f,U,z,V):n($,f,U,V),R=4===B?"V0":`V${(P(O)-1).toString()}`}else{if(L=4===B?i(O,0,0):i(O,C.length-1,C[C.length-1].length-1),0!==U)if(4===B)L=l(L,h(C[0][0]),C[0][1],U,V);else{const e=C[C.length-1];L=l(L,h(e[e.length-2]),e[e.length-1],U,V)}R=4===B?"V0":`V${(P(O)-1).toString()}`}L&&r({templatePart:m,shape:L,edits:f,relationships:u,mode:S,associationFeaturesByTemplate:F},R)}function T(e,t,o,r){if(!d(e))return v(e,t);const s=j(e,"completion"===o?{maxSegmentLength:r}:{maxSegmentsPerCurve:300,maxSegmentLength:r});return v(u(s),t)}export{x as lineVerticesOffset,T as modeBasedOffset,v as offset,U as vertexOffsetFromStartOrEndOfLine};
