/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{GeometryCursor as t}from"../../geometry/GeometryCursor.js";const e=.03;class n{constructor(t=0,e=!1){}isEmpty(t){if(!t.nextPoint())return!0;let e,n,s,r;for(e=t.x,n=t.y;t.nextPoint();e=n,n=r)if(s=t.x,r=t.y,s!==e||r!==n)return t.seekPathStart(),!1;return t.seekPathStart(),!0}normalize(t){const e=Math.sqrt(t[0]*t[0]+t[1]*t[1]);0!==e&&(t[0]/=e,t[1]/=e)}getLength(t,e,n,s){const r=n-t,o=s-e;return Math.sqrt(r*r+o*o)}getSegLength(t){const[[e,n],[s,r]]=t;return this.getLength(e,n,s,r)}getCoord2D(t,e,n,s,r){return[t+(n-t)*r,e+(s-e)*r]}getSegCoord2D(t,e){const[[n,s],[r,o]]=t;return this.getCoord2D(n,s,r,o,e)}getAngle(t,e,n,s,r){const o=n-t,i=s-e;return Math.atan2(i,o)}getAngleCS(t,e,n,s,r,o){null==t&&(t=[0,0]);const i=s-e,h=r-n,u=Math.sqrt(i*i+h*h);return 0!==u?(t[0]=i/u,t[1]=h/u,t):(t[0]=1,t[1]=0,t)}getSegAngleCS(t,e,n){const[[s,r],[o,i]]=e;return this.getAngleCS(t,s,r,o,i,n)}cut(t,e,n,s,r,o){return[r<=0?[t,e]:this.getCoord2D(t,e,n,s,r),o>=1?[n,s]:this.getCoord2D(t,e,n,s,o)]}getSubCurve(e,n,s){const r=t.createEmptyOptimizedCIM("esriGeometryPolyline");return this.appendSubCurve(r,e,n,s)?r:null}appendSubCurve(t,e,n,s){t.startPath(),e.seekPathStart();let r=0,o=!0;if(!e.nextPoint())return!1;let i=e.x,h=e.y;for(;e.nextPoint();){const u=this.getLength(i,h,e.x,e.y);if(0!==u){if(o){if(r+u>n){const l=(n-r)/u;let c=1,a=!1;r+u>=s&&(c=(s-r)/u,a=!0);const f=this.cut(i,h,e.x,e.y,l,c);if(f&&t.pushPoints(f),a)break;o=!1}}else{if(r+u>s){const n=this.cut(i,h,e.x,e.y,0,(s-r)/u);n&&t.pushPoint(n[1]);break}t.pushXY(e.x,e.y)}r+=u,i=e.x,h=e.y}else i=e.x,h=e.y}return!0}getCIMPointAlong(t,e){if(!t.nextPoint())return null;let n,s,r,o,i=0;for(n=t.x,s=t.y;t.nextPoint();n=r,s=o){r=t.x,o=t.y;const h=this.getLength(n,s,r,o);if(0!==h){if(i+h>e){const t=(e-i)/h;return this.getCoord2D(n,s,r,o,t)}i+=h}}return null}offset(t,e,n,s,r){if(!t||t.length<2)return null;let o=0,i=t[o++],h=o;for(;o<t.length;){const e=t[o];e[0]===i[0]&&e[1]===i[1]||(o!==h&&(t[h]=t[o]),i=t[h++]),o++}const u=t[0][0]===t[h-1][0]&&t[0][1]===t[h-1][1];if(u&&--h,h<(u?3:2))return null;const l=[];i=u?t[h-1]:null;let c=t[0];for(let a=0;a<h;a++){const r=a===h-1?u?t[0]:null:t[a+1];if(i)if(r){const t=[r[0]-c[0],r[1]-c[1]];this.normalize(t);const o=[c[0]-i[0],c[1]-i[1]];this.normalize(o);const h=o[0]*t[1]-o[1]*t[0],u=o[0]*t[0]+o[1]*t[1];if(0===h&&1===u){c=r;continue}if(h>=0==e<=0){if(u<1){const n=[t[0]-o[0],t[1]-o[1]];this.normalize(n);const r=Math.sqrt((1+u)/2);if(r>1/s){const t=-Math.abs(e)/r;l.push([c[0]-n[0]*t,c[1]-n[1]*t])}}}else switch(n){case"Mitered":{const n=Math.sqrt((1+u)/2);if(n>0&&1/n<s){const s=[t[0]-o[0],t[1]-o[1]];this.normalize(s);const r=Math.abs(e)/n;l.push([c[0]-s[0]*r,c[1]-s[1]*r]);break}}case"Bevelled":l.push([c[0]+o[1]*e,c[1]-o[0]*e]),l.push([c[0]+t[1]*e,c[1]-t[0]*e]);break;case"Rounded":if(u<1){l.push([c[0]+o[1]*e,c[1]-o[0]*e]);const n=Math.floor(2.5*(1-u));if(n>0){const s=1/n;let r=s;for(let i=1;i<n;i++,r+=s){const n=[o[1]*(1-r)+t[1]*r,-o[0]*(1-r)-t[0]*r];this.normalize(n),l.push([c[0]+n[0]*e,c[1]+n[1]*e])}}l.push([c[0]+t[1]*e,c[1]-t[0]*e])}break;default:if(h<0)l.push([c[0]+(o[1]+o[0])*e,c[1]+(o[1]-o[0])*e]),l.push([c[0]+(t[1]-t[0])*e,c[1]-(t[0]+t[1])*e]);else{const n=Math.sqrt((1+Math.abs(u))/2),s=[t[0]-o[0],t[1]-o[1]];this.normalize(s);const r=e/n;l.push([c[0]-s[0]*r,c[1]-s[1]*r])}}}else{const t=[c[0]-i[0],c[1]-i[1]];this.normalize(t),l.push([c[0]+t[1]*e,c[1]-t[0]*e])}else{const t=[r[0]-c[0],r[1]-c[1]];this.normalize(t),l.push([c[0]+t[1]*e,c[1]-t[0]*e])}i=c,c=r}return l.length<(u?3:2)?null:(u&&l.push([l[0][0],l[0][1]]),l)}}export{n as CurveHelper,e as pixelTolerance};
