/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{equals as t}from"../../core/arrayUtils.js";import{getInfo as n}from"./spatialReferenceUtils.js";import{curvesAreSameType as e}from"./curves/curveUtils.js";function r(t){if(!t)return null;if(Array.isArray(t))return t;const n=t.hasZ,e=t.hasM;if("point"===t.type)return e&&n?[t.x,t.y,t.z,t.m]:n?[t.x,t.y,t.z]:e?[t.x,t.y,t.m]:[t.x,t.y];if("polygon"===t.type)return t.rings.slice();if("polyline"===t.type)return t.paths.slice();if("multipoint"===t.type)return t.points.map(t=>[t]);if("extent"===t.type){const n=t.clone().normalize();if(!n)return null;let e=!1,r=!1;return n.forEach(t=>{t.hasZ&&(e=!0),t.hasM&&(r=!0)}),n.map(t=>{const n=[[t.xmin,t.ymin],[t.xmin,t.ymax],[t.xmax,t.ymax],[t.xmax,t.ymin],[t.xmin,t.ymin]];if(e&&t.hasZ){const e=.5*(t.zmax-t.zmin);for(let t=0;t<n.length;t++)n[t].push(e)}if(r&&t.hasM){const e=.5*(t.mmax-t.mmin);for(let t=0;t<n.length;t++)n[t].push(e)}return n})}return null}function i(t,n){const e=n[0]-t[0],r=n[1]-t[1];if(t.length>2&&n.length>2){const i=t[2]-n[2];return Math.sqrt(e*e+r*r+i*i)}return Math.sqrt(e*e+r*r)}function o(t,n,e){const r=t[0]+e*(n[0]-t[0]),i=t[1]+e*(n[1]-t[1]);return t.length>2&&n.length>2?[r,i,t[2]+e*(n[2]-t[2])]:[r,i]}function s(t,n,e,r){return u(t,n,e[r],e[r+1])}function u(t,n,e,r){const[i,o]=n,[s,u]=e,[l,f]=r,c=l-s,h=f-u,p=c*c+h*h,g=(i-s)*c+(o-u)*h,a=Math.min(1,Math.max(0,g/p));return t[0]=s+c*a,t[1]=u+h*a,t}function l(t,n,e){let r,i,o,s,u=!1,l=1/0;for(e.reset();e.nextPath();)if(e.nextPoint())for(r=e.x,i=e.y;e.nextPoint();)o=e.x,s=e.y,i>n!=s>n&&t<(o-r)*(n-i)/(s-i)+r&&(u=!u),l=Math.min(l,f(t,n,r,i,o,s)),r=o,i=s;return 0===l?0:(u?1:-1)*Math.sqrt(l)}function f(t,n,e,r,i,o){let s=e,u=r,l=i-s,f=o-u;if(0!==l||0!==f){const e=((t-s)*l+(n-u)*f)/(l*l+f*f);e>1?(s=i,u=o):e>0&&(s+=l*e,u+=f*e)}return l=t-s,f=n-u,l*l+f*f}function c(t,n){return o(t,n,.5)}function h(t){const n=t.length;let e=0;for(let r=0;r<n-1;++r)e+=i(t[r],t[r+1]);return e}function p(t,n){if(n<=0)return t[0];const e=t.length;let r=0;for(let s=0;s<e-1;++s){const e=i(t[s],t[s+1]);if(n-r<e){const i=(n-r)/e;return o(t[s],t[s+1],i)}r+=e}return t[e-1]}function g(t,n=0,e=1){let r=0;const i=t.length;let o=t[0];for(let s=1;s<i;s++){const i=t[s];r+=(i[n]-o[n])*(i[e]+o[e]),o=i}if(a(t)){const i=t[0];r+=(i[n]-o[n])*(i[e]+o[e])}return r>=0}function a(n){const e=n.length;return e>0&&!t(n[0],n[e-1])}function y(t){"rings"in t&&(m(t),x(t))}function m(t){if("rings"in t)for(const n of t.rings)a(n)&&n.push(n[0].slice())}function x(t){if(!("rings"in t))return!1;if(0===t.rings.length||g(t.rings[0]))return!1;for(const n of t.rings)n.reverse();return!0}function v(t){if("polygon"!==t.type&&"polyline"!==t.type)return t;return M("polygon"===t.type?t.rings:t.paths,t.spatialReference),t}function M(t,e){const r=n(e);if(!r)return;const i=r.valid[0],o=r.valid[1],s=o-i;for(const n of t){let t=1/0,e=-1/0;for(const s of n){const n=P(s[0],i,o);t=Math.min(t,n),e=Math.max(e,n),s[0]=n}const r=e-t;s-r<r&&n.forEach(t=>{t[0]<0&&(t[0]+=s)})}}function P(t,n,e){const r=e-n;return t<n?e-(n-t)%r:t>e?n+(t-n)%r:t}function R(t,n){return t===n||t.type===n.type&&("point"===t.type||"mesh"===t.type||"extent"===t.type||("multipoint"===t.type&&"multipoint"===n.type?t.points.length===n.points.length:"polyline"===t.type&&"polyline"===n.type?t.curvePaths?.length===n.curvePaths?.length&&t.paths.length===n.paths.length&&(t.curvePaths?.length?d(t.curvePaths,n.curvePaths,!0):d(t.paths,n.paths,!1)):"polygon"===t.type&&"polygon"===n.type&&(t.curveRings?.length===n.curveRings?.length&&t.rings.length===n.rings.length&&(t.curveRings?.length?d(t.curveRings,n.curveRings,!0):d(t.rings,n.rings,!1)))))}function d(t,n,r){if(void 0===t&&void 0===n)return!0;if(void 0===t||void 0===n)return!1;for(let i=0;i<t.length;i++){if(t[i].length!==n[i].length)return!1;if(r)for(let r=0;r<t[i].length;r++)if(!e(t[i][r],n[i][r]))return!1}return!0}export{m as closeRings,y as closeRingsAndFixWinding,l as distanceFromPointToPolygon,f as distanceToSegmentSquared,r as geometryToCoordinates,i as getLength,c as getMidpoint,h as getPathLength,p as getPointOnPath,R as hasCompatibleTopology,g as isClockwise,s as projectPointOnLine,u as projectPointOnLineSeg,v as unnormalizeGeometryOnDatelineCrossing,M as unnormalizeVerticesOnDatelineCrossing,P as unnormalizedCoordinate};
