/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{collectPath as t}from"../geometryCursorCollectUtils.js";import{getMidpoint as n,getLength as e}from"./coordsUtils.js";function r(t){return t?t.hasZ?[t.xmax-t.xmin/2,t.ymax-t.ymin/2,t.zmax-t.zmin/2]:[t.xmax-t.xmin/2,t.ymax-t.ymin/2]:null}function l(t){return u(t.rings,t.hasZ??!1)}function o(t){const n=l(t);if(!n)return null;const{hasZ:e,spatialReference:r}=t,[o,u,i]=n;return isNaN(o)||isNaN(u)||e&&isNaN(i)?null:{x:o,y:u,z:e?i:void 0,spatialReference:r}}function u(t,n){if(!t?.length)return null;const e=[],r=[],l=n?[1/0,-1/0,1/0,-1/0,1/0,-1/0]:[1/0,-1/0,1/0,-1/0];for(let o=0,u=t.length;o<u;o++){const e=i(t[o],n,l);e&&r.push(e)}if(r.sort((t,e)=>{let r=t[2]-e[2];return 0===r&&n&&(r=t[4]-e[4]),r}),r.length&&(e[0]=r[0][0],e[1]=r[0][1],n&&(e[2]=r[0][3]),(e[0]<l[0]||e[0]>l[1]||e[1]<l[2]||e[1]>l[3]||n&&(e[2]<l[4]||e[2]>l[5]))&&(e.length=0)),!e.length){const r=t[0]&&t[0].length?h(t[0],n):null;if(!r)return null;e[0]=r[0],e[1]=r[1],n&&r.length>2&&(e[2]=r[2])}return e}function i(t,n,e){let r=0,l=0,o=0,u=0,i=0;const s=t.length?t[0][0]:0,h=t.length?t[0][1]:0,I=t.length&&n?t[0][2]:0;for(let f=0;f<t.length;f++){const c=t[f],N=t[(f+1)%t.length],[a,x,g]=c,m=a-s,P=x-h,[T,y,E]=N,p=T-s,S=y-h,z=m*S-p*P;if(u+=z,r+=(m+p)*z,l+=(P+S)*z,n&&c.length>2&&N.length>2){const t=g-I,n=E-I,e=m*n-p*t;o+=(t+n)*e,i+=e}a<e[0]&&(e[0]=a),a>e[1]&&(e[1]=a),x<e[2]&&(e[2]=x),x>e[3]&&(e[3]=x),n&&(g<e[4]&&(e[4]=g),g>e[5]&&(e[5]=g))}if(u>0&&(u*=-1),i>0&&(i*=-1),!u)return null;u*=.5,i*=.5;const c=[r/(6*u)+s,l/(6*u)+h,u];return n&&(e[4]===e[5]||0===i?(c[3]=(e[4]+e[5])/2,c[4]=0):(c[3]=o/(6*i)+I,c[4]=i)),c}function s(t,n){let e=0,r=0,l=0;t.nextPoint();const o=t.pathSize?t.x:0,u=t.pathSize?t.y:0;for(let i=0;i<t.pathSize;i++){t.seekInPath(i);const s=[t.x,t.y];t.seekInPath((i+1)%t.pathSize);const h=[t.x,t.y],[I,c]=s,f=I-o,N=c-u,[a,x]=h,g=a-o,m=x-u,P=f*m-g*N;l+=P,e+=(f+g)*P,r+=(N+m)*P,I<n[0]&&(n[0]=I),I>n[1]&&(n[1]=I),c<n[2]&&(n[2]=c),c>n[3]&&(n[3]=c)}if(l>0&&(l*=-1),!l)return null;l*=.5;return[e/(6*l)+o,r/(6*l)+u,l]}function h(t,r){const l=r?[0,0,0]:[0,0],o=r?[0,0,0]:[0,0];let u=0,i=0,s=0,h=0;for(let I=0,c=t.length;I<c-1;I++){const c=t[I],f=t[I+1];if(c&&f){l[0]=c[0],l[1]=c[1],o[0]=f[0],o[1]=f[1],r&&c.length>2&&f.length>2&&(l[2]=c[2],o[2]=f[2]);const t=e(l,o);if(t){u+=t;const e=n(c,f);i+=t*e[0],s+=t*e[1],r&&e.length>2&&(h+=t*e[2])}}}return u>0?r?[i/u,s/u,h/u]:[i/u,s/u]:t.length?t[0]:null}function I(n){const{hasZ:e,totalSize:r}=n;if(0===r)return null;const l=[],o=[],u=e?[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];for(n.reset();n.nextPath();){const e=i(t(n),n.hasZ,u);e&&o.push(e)}if(o.sort((t,n)=>{let r=t[2]-n[2];return 0===r&&e&&(r=t[4]-n[4]),r}),o.length&&(l[0]=o[0][0],l[1]=o[0][1],e&&(l[2]=o[0][3]),(l[0]<u[0]||l[0]>u[1]||l[1]<u[2]||l[1]>u[3]||e&&(l[2]<u[4]||l[2]>u[5]))&&(l.length=0)),!l.length){n.reset(),n.nextPath();const t=n.pathSize?c(n):null;if(!t)return null;l[0]=t[0],l[1]=t[1],e&&t.length>2&&(l[2]=t[2])}return l}function c(t){const{hasZ:r}=t,l=r?[0,0,0]:[0,0],o=r?[0,0,0]:[0,0];let u=0,i=0,s=0,h=0;if(t.nextPoint()){let I=t.x,c=t.y,f=t.z;for(;t.nextPoint();){const N=t.x,a=t.y,x=t.z;l[0]=I,l[1]=c,o[0]=N,o[1]=a,r&&(l[2]=f,o[2]=x);const g=e(l,o);if(g){u+=g;const t=n(l,o);i+=g*t[0],s+=g*t[1],r&&t.length>2&&(h+=g*t[2])}I=N,c=a,f=x}}return u>0?r?[i/u,s/u,h/u]:[i/u,s/u]:t.pathSize?(t.seekPathStart(),t.nextPoint(),[t.x,t.y]):null}const f=1e-6;function N(t){let n=0;for(t.reset();t.nextPath();)n+=t.getCurrentRingArea();if(n<f){const n=I(t);return n?[n[0],n[1]]:null}const e=[0,0];if(t.reset(),!t.nextPath()||!t.nextPoint())return null;const r=[t.x,t.y];for(t.reset();t.nextPath();)x(e,r,t);return e[0]*=1/n,e[1]*=1/n,e[0]+=r[0],e[1]+=r[1],e}const a=1/3;function x(t,n,e){if(!t||!e||e.pathSize<3)return null;e.nextPoint();const r=e.x,l=e.y;e.nextPoint();let o,u=e.x-r,i=e.y-l,s=0,h=0;for(;e.nextPoint();)s=e.x-r,h=e.y-l,o=.5*a*(s*i-h*u),t[0]+=o*(u+s),t[1]+=o*(i+h),u=s,i=h;const I=e.getCurrentRingArea(),c=[r,l];return c[0]-=n[0],c[1]-=n[1],c[0]*=I,c[1]*=I,t[0]+=c[0],t[1]+=c[1],t}export{r as extentCentroid,h as lineCentroid,c as lineCentroidCursor,l as polygonCentroid,o as polygonCentroidPoint,i as ringCentroid,s as ringCentroidCursorXY,u as ringsCentroid,I as ringsCentroidCursor,N as weightedAreaCentroid};
