/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import t from"./OptimizedGeometry.js";function n(t,n){return t?n?4:3:n?3:2}function r(n){if(null==n)return null;if(!n?.lengths.length)return null;const{lengths:r,coords:u,stride:N,hasZ:o,hasM:l}=n,s=new t([],[],o,!1),c=s.coords,f=[],i=o?[Number.POSITIVE_INFINITY,Number.NEGATIVE_INFINITY,Number.POSITIVE_INFINITY,Number.NEGATIVE_INFINITY,Number.POSITIVE_INFINITY,Number.NEGATIVE_INFINITY]:[Number.POSITIVE_INFINITY,Number.NEGATIVE_INFINITY,Number.POSITIVE_INFINITY,Number.NEGATIVE_INFINITY];let T=0;for(const t of r){const n=e(i,u,T,t,o,l);n&&f.push(n),T+=t*N}if(f.sort((t,n)=>{let r=t[2]-n[2];return 0===r&&o&&(r=t[4]-n[4]),r}),f.length){let t=6*f[0][2];c[0]=f[0][0]/t,c[1]=f[0][1]/t,o&&(t=6*f[0][4],c[2]=0!==t?f[0][3]/t:0),(c[0]<i[0]||c[0]>i[1]||c[1]<i[2]||c[1]>i[3]||o&&(c[2]<i[4]||c[2]>i[5]))&&(c.length=0)}if(!c.length){const t=n.lengths[0]?I(u,0,r[0],o,l):null;if(!t)return null;c[0]=t[0],c[1]=t[1],o&&(c[2]=t[2]??0)}return s}function e(t,r,e,I,u,N){const o=n(u,N);let l=e,s=e+o,c=0,f=0,i=0,T=0,E=0;for(let n=0,h=I-1;n<h;n++,l+=o,s+=o){const n=r[l],e=r[l+1],I=r[l+2],N=r[s],o=r[s+1],m=r[s+2];let h=n*o-N*e;T+=h,c+=(n+N)*h,f+=(e+o)*h,u&&(h=n*m-N*I,i+=(I+m)*h,E+=h),n<t[0]&&(t[0]=n),n>t[1]&&(t[1]=n),e<t[2]&&(t[2]=e),e>t[3]&&(t[3]=e),u&&(I<t[4]&&(t[4]=I),I>t[5]&&(t[5]=I))}if(T>0&&(T*=-1),E>0&&(E*=-1),!T)return null;const m=[c,f,.5*T];return u&&(m[3]=i,m[4]=.5*E),m}function I(t,r,e,I,s){const c=n(I,s);let f=r,i=r+c,T=0,E=0,m=0,h=0;for(let n=0,b=e-1;n<b;n++,f+=c,i+=c){const n=t[f],r=t[f+1],e=t[f+2],s=t[i],c=t[i+1],b=t[i+2],F=I?N(n,r,e,s,c,b):u(n,r,s,c);if(F)if(T+=F,I){const t=l(n,r,e,s,c,b);E+=F*t[0],m+=F*t[1],h+=F*t[2]}else{const t=o(n,r,s,c);E+=F*t[0],m+=F*t[1]}}return T>0?I?[E/T,m/T,h/T]:[E/T,m/T]:e>0?I?[t[r],t[r+1],t[r+2]]:[t[r],t[r+1]]:null}function u(t,n,r,e){const I=r-t,u=e-n;return Math.sqrt(I*I+u*u)}function N(t,n,r,e,I,u){const N=e-t,o=I-n,l=u-r;return Math.sqrt(N*N+o*o+l*l)}function o(t,n,r,e){return[t+.5*(r-t),n+.5*(e-n)]}function l(t,n,r,e,I,u){return[t+.5*(e-t),n+.5*(I-n),r+.5*(u-r)]}export{r as getCentroidOptimizedGeometry,I as lineCentroid};
