/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{isSome as s}from"../../../core/arrayUtils.js";import{initializeProjection as t,projectMany as e}from"../../../geometry/projectionUtils.js";import{isValid as n,equals as r,isWebMercator as i}from"../../../geometry/support/spatialReferenceUtils.js";import{canProject as o,lngLatToXY as a,xyToLngLat as m}from"../../../geometry/support/webMercatorUtils.js";const l=[0,0];function u(s,t){if(!t)return null;if("x"in t){const e={x:0,y:0};return[e.x,e.y]=s(t.x,t.y,l),null!=t.z&&(e.z=t.z),null!=t.m&&(e.m=t.m),e}if("xmin"in t){const e={xmin:0,ymin:0,xmax:0,ymax:0};return[e.xmin,e.ymin]=s(t.xmin,t.ymin,l),[e.xmax,e.ymax]=s(t.xmax,t.ymax,l),t.hasZ&&(e.zmin=t.zmin,e.zmax=t.zmax,e.hasZ=!0),t.hasM&&(e.mmin=t.mmin,e.mmax=t.mmax,e.hasM=!0),e}return"rings"in t?{rings:c(t.rings,s),hasM:t.hasM,hasZ:t.hasZ}:"paths"in t?{paths:c(t.paths,s),hasM:t.hasM,hasZ:t.hasZ}:"points"in t?{points:h(t.points,s),hasM:t.hasM,hasZ:t.hasZ}:null}function c(s,t){const e=[];for(const n of s)e.push(h(n,t));return e}function h(s,t){const e=[];for(const n of s){const s=t(n[0],n[1],[0,0]);e.push(s),n.length>2&&s.push(n[2]),n.length>3&&s.push(n[3])}return e}async function p(e,n){if(!e||!n)return;const r=Array.isArray(e)?e.map(s=>null!=s.geometry?s.geometry.spatialReference:null).filter(s):[e];await t(r.map(s=>({source:s,dest:n})))}const f=u.bind(null,a),x=u.bind(null,m);function y(s,t,a,m){if(!s)return null;if(a||(a=t,t=s.spatialReference),!n(t)||!n(a)||r(t,a))return s;if(o(t,a)){const t=i(a)?f(s):x(s);return t.spatialReference=a,t}return e([s],t,a,m)[0]}class g{constructor(){this._jobs=[],this._timer=null,this._process=this._process.bind(this)}async push(s,t,e,n){if(!s?.length||!t||!e||r(t,e))return s;const i={geometries:s,inSpatialReference:t,outSpatialReference:e,options:n,resolve:Promise.withResolvers()};return this._jobs.push(i),this._timer??=setTimeout(this._process,10),i.resolve.promise}_process(){this._timer=null;const s=this._jobs.shift();if(!s)return;const{geometries:t,inSpatialReference:n,outSpatialReference:r,resolve:a,options:m}=s,l=t.filter(s=>null!=s);let u;u=o(n,r)&&null==m?.extendedParams?i(r)?l.map(f):l.map(x):e(l,n,r,m);let c=0;const h=t.map(s=>null==s?null:u[c++]);a.resolve(h),this._jobs.length>0&&(this._timer=setTimeout(this._process,10))}}const _=new g;function j(s,t,e,n){return _.push(s,t,e,n)}export{p as checkProjectionSupport,y as project,j as projectMany};
