/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{createArea as e,createLength as t}from"../../core/quantity.js";import{deriveLengthUnitFromAreaUnit as r}from"../../core/units.js";import{set as o,scale as n,normalize as i,cross as s,subtract as a,dot as c,add as f}from"../../core/libs/gl-matrix-2/math/vec3.js";import{create as m}from"../../core/libs/gl-matrix-2/factories/vec3f64.js";import{isEarth as p,isSphericalECEF as u}from"../../geometry/ellipsoidUtils.js";import l from"../../geometry/Point.js";import g from"../../geometry/Polygon.js";import h from"../../geometry/Polyline.js";import{projectWithoutEngine as y}from"../../geometry/projectionUtils.js";import d from"../../geometry/SpatialReference.js";import{WGS84ECEFSpatialReference as j,getSphericalPCPF as R,SphericalECEFSpatialReference as O}from"../../geometry/spatialReferenceEllipsoidUtils.js";import{e as w}from"../../chunks/areaOperator.js";import{e as S}from"../../chunks/centroidOperator.js";import{l as J,e as N}from"../../chunks/geodeticAreaOperator.js";import{l as k,e as v}from"../../chunks/geodeticLengthOperator.js";import{e as P}from"../../chunks/lengthOperator.js";import{e as U}from"../../chunks/simplifyOperator.js";import{projectDirection as x}from"../../geometry/projection/projectDirection.js";import{isWGS84 as M,isWebMercator as C,isCGCS2000 as b,equals as q}from"../../geometry/support/spatialReferenceUtils.js";import{makeOrthonormal as z}from"../3d/support/mathUtils.js";async function E({geometryJSON:t,options:o}){await J();const n=Z(t,o),i=o?.unit??"square-meters",s=U(n)??n,a=N(s,{unit:i,...o}),c=e(a,i),f=o?.lengthUnit??r(i),[m,p]=await Promise.all([o?.returnCentroid?$(s).then(e=>e.toJSON()):void 0,o?.returnLength?A(s,{unit:f}):void 0]);return{area:c,centroid:m,length:p}}function L(){J(),k()}function Z(e,t){const r="rings"in e?g.fromJSON(e):h.fromJSON(e);if(t?.stagedPoint){const e=l.fromJSON(t.stagedPoint),o=("rings"in r?r.rings:r.paths).at(-1);if(o){const{x:t,y:n,z:i,m:s}=e,a=r.hasZ&&r.hasM?[t,n,i??0,s??0]:r.hasZ?[t,n,i??0]:r.hasM?[t,n,s??0]:[t,n];o.push(a)}}return r}async function $(e){return S(e)}async function A(e,r){await k();const o=r?.unit??"meters";return t(v(e,{...r,unit:o}),o)}async function D({geometryJSON:e,options:t}){return A(Z(e,t),t)}function W({geometryJSON:e,options:t}){return B(Z(e,t),t)}async function B(t,o){const n=o?.unit??"square-meters",i=o?.lengthUnit??r(n),s=U(t)??t,a=w(s,{unit:n,...o}),c=e(a,n),[f,m]=await Promise.all([o?.returnCentroid?$(s).then(e=>e.toJSON()):void 0,o?.returnLength?F(s,{unit:i}):void 0]);return{area:c,centroid:f,length:m}}async function F(e,r){const o=r?.unit??"meters";return t(P(e,{...r,unit:o}),o)}async function G({geometryJSON:e,options:t}){return F(Z(e,t),t)}async function H(e){const{geometryJSON:t}=e,r=T(t.spatialReference);if(!r)throw new Error("Cannot compute horizontal area without a valid spatial reference.");switch(r.mode){case 0:return W(e);case 1:return I(r,e)}}async function I(e,{geometryJSON:t,options:r}){const o=Z(t,r),n=Y,i=V,s=X,a=q(o.spatialReference,e.spatialReference)?o.clone():y(o,o.spatialReference,e.spatialReference);if(!a)throw new Error(`Cannot project geometry from ${o.spatialReference?.wkid??"unknown"} to ${e.spatialReference.wkid}.`);Q(a,n,i,s);const{area:c,centroid:f,length:m}=await B(a,r);return{area:c,centroid:K(f,n,i,s,e.spatialReference,o.spatialReference),length:m}}function K(e,t,r,o,i,s){if(!e)return e;n(r,r,e.x),n(o,o,e.y);const a=_;return f(a,r,o),f(a,a,t),e.x=a[0],e.y=a[1],e.z=a[2],e.spatialReference=i,y(e,e.spatialReference,s)?.toJSON()??void 0}function Q(e,t,r,f){let m=0;o(t,0,0,0);for(const o of e.rings)for(const e of o)t[0]+=e[0],t[1]+=e[1],t[2]+=e[2],m++;n(t,t,1/m);const p=ee;i(p,t);const u=te,l=Math.atan2(p[1],p[0]);o(u,-Math.sin(l),Math.cos(l),0),q(e.spatialReference,j)&&(x(t,p,O,p,e.spatialReference),x(t,u,O,u,e.spatialReference)),z(u,p,r),s(f,r,p);const g=_;for(const n of e.rings)for(const e of n){o(g,e[0],e[1],e[2]),a(g,g,t);const n=c(r,g),i=c(f,g);e[0]=n,e[1]=i}e.hasZ=!1,e.spatialReference=d.WebMercator}function T(e){return e?p(e)?M(e)||C(e)||b(e)||u(e)||q(e,j)?{mode:1,spatialReference:j}:{mode:0,spatialReference:e}:{mode:1,spatialReference:R(e)}:null}const V=m(),X=m(),Y=m(),_=m(),ee=m(),te=m();export{W as area2D,H as areaHorizontal,E as geodesicArea,D as geodesicLength,G as length2D,L as preloadGeodesic};
