/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{isSome as t,equals as e}from"../../../core/arrayUtils.js";import{clone as o}from"../../../core/lang.js";import{getMetersPerUnitForSR as n}from"../../../core/units.js";import{l as r}from"../../../chunks/vec32.js";import{create as a}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import l from"../../../geometry/Circle.js";import s from"../../../geometry/Multipoint.js";import i from"../../../geometry/Point.js";import c from"../../../geometry/Polygon.js";import p from"../../../geometry/Polyline.js";import{e as y}from"../../../chunks/distanceOperator.js";import{i as u,e as f}from"../../../chunks/simplifyOperator.js";import{unnormalizeVerticesOnDatelineCrossing as m,isClockwise as x,unnormalizeGeometryOnDatelineCrossing as h}from"../../../geometry/support/coordsUtils.js";import{isValid as M}from"../../../geometry/support/spatialReferenceUtils.js";import{makeSurfacePoint as g}from"./surfaceCoordinateSystems.js";function d(t,e){const o=new i({x:t[0],y:t[1],spatialReference:e});return t.length>2&&(o.z=t[2]),o}function R(t,e){return new s({points:t,spatialReference:e})}function j(t,e,o){const n=new p({paths:t,spatialReference:e});return o&&h(n),n}function T(t,n,r,a=!0){const l=o(t);l.forEach(t=>{const o=t[0],n=t[t.length-1];e(o,n)&&1!==t.length||t.push(t[0])});let s=new c({rings:l,spatialReference:n});return s.rings.forEach(t=>{x(t)||t.reverse()}),r&&h(s),a&&!u(s)&&M(n)&&(s=f(s)||s),s}function b(e,o,n){const r=o.mapToLocalMultiple(e),a=[],l={x:r[0].x,y:r[0].y},s={x:r[1].x,y:r[1].y},i=Math.round(s.x-l.x),c=Math.round(s.y-l.y),p=Math.max(Math.abs(i),Math.abs(c));if(n){const t={x:l.x+p,y:l.y+p},e={x:l.x-p,y:l.y-p};a.push(g(t.x,e.y),g(e.x,e.y),g(e.x,t.y),g(t.x,t.y))}else{const t={x:i>0?l.x+p:l.x-p,y:c>0?l.y+p:l.y-p};a.push(g(l.x,l.y),g(t.x,l.y),g(t.x,t.y),g(l.x,t.y))}return P(T([a.map(t=>o.localToMap(t)).filter(t)],o.spatialReference,o.doUnnormalization,!0),a,o)}function U(e,o,n){let r=o.mapToLocalMultiple(e);if(1===r.length){const t=48,e=r[0];r=[g(e.x-t,e.y+t),g(e.x+t,e.y-t),g(e.x+t,e.y-t),g(e.x-t,e.y+t)]}const a=[],l={x:r[0].x,y:r[0].y},s={x:r[1].x,y:r[1].y};if(n){const t=Math.round(s.x-l.x),e=Math.round(s.y-l.y);a.push(g(l.x-t,l.y-e),g(s.x,l.y-e),g(s.x,s.y),g(l.x-t,s.y))}else a.push(g(l.x,l.y),g(s.x,l.y),g(s.x,s.y),g(l.x,s.y));return P(T([a.map(t=>o.localToMap(t)).filter(t)],o.spatialReference,o.doUnnormalization,!0),a,o)}function P(t,e,o){const n=z(e[3],e[2],o),r=z(e[1],e[2],o),a=z(e[0],e[1],o),l=z(e[0],e[3],o);return{geometry:t,midpoints:null!=n&&null!=r&&null!=a&&null!=l?{top:n,right:r,bottom:a,left:l}:null}}function z(t,e,o){I[0]=t.x,I[1]=t.y,I[2]=0,L[0]=e.x,L[1]=e.y,L[2]=0,r(I,I,L,.5),w.x=I[0],w.y=L[1],w.z=L[2];const n=o.localToMap(w);return null!=n?d(n,o.spatialReference):null}const w=g(0,0,0),I=a(),L=a();function k(t,e,o,r){const a=e.mapToLocalMultiple(t);let s=null,i=null;if(o)s=a[0],i=a[1];else{const t=a[0],e=a[1],o=Math.round(e.x-t.x),n=Math.round(e.y-t.y),r=Math.max(Math.abs(o),Math.abs(n));s=g(o>0?t.x+r/2:t.x-r/2,n>0?t.y+r/2:t.y-r/2),i=g(Math.abs(o)>Math.abs(n)?s.x-r/2:s.x,Math.abs(o)>Math.abs(n)?s.y:s.y-r/2)}const c=e.localToMap(s),p=e.localToMap(i);if(null==c||null==p)return null;e.doUnnormalization&&m([[c,p]],e.spatialReference);const u=d(c,e.spatialReference),f=d(p,e.spatialReference),x=n(e.spatialReference);let h=0;if(M(e.spatialReference))h=x*y(u,f);else{const t=s.x-i.x,e=s.y-i.y;h=x*Math.sqrt(t*t+e*e)*(r||1)}const R=new l({center:u,radius:h,radiusUnit:"meters",spatialReference:e.spatialReference});return{geometry:T(R.rings,R.spatialReference,!1),center:u,edge:f}}function v(e,o,n){const r=o.mapToLocalMultiple(e),a=r[0],l=r[1],s=Math.round(l.x-a.x),i=Math.round(l.y-a.y),c=g(n?a.x:a.x+s/2,n?a.y:a.y+i/2),p=n?s:s/2,y=n?i:i/2,u=60,f=[],m=2*Math.PI/u;function x(t){const e=Math.cos(t),o=Math.sin(t);return g(p*e+c.x,y*o+c.y)}for(let t=0;t<u;t++)f.push(x(t*m));f.push(f[0]);const{spatialReference:h,doUnnormalization:M}=o,R=T([f.map(t=>o.localToMap(t)).filter(t)],h,M,!1),j=o.localToMap(x(Math.PI/2)),b=o.localToMap(x(0)),U=o.localToMap(x(-Math.PI/2)),P=o.localToMap(x(Math.PI));return{geometry:R,midpoints:null!=j&&null!=b&&null!=U&&null!=P?{top:d(j,h),right:d(b,h),bottom:d(U,h),left:d(P,h)}:null}}export{k as createCircle,v as createEllipse,R as createMultipoint,d as createPoint,T as createPolygon,j as createPolyline,U as createRectangle,b as createSquare};
