/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{isSome as s}from"../../../core/arrayUtils.js";import{createResolver as t}from"../../../core/promiseUtils.js";import{initializeProjection as e,projectMany as n}from"../../../geometry/projectionUtils.js";import{isValid as r,equals as i,isWebMercator as o}from"../../../geometry/support/spatialReferenceUtils.js";import{canProject as a,lngLatToXY as m,xyToLngLat as l}from"../../../geometry/support/webMercatorUtils.js";const u=[0,0];function p(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,u),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,u),[e.xmax,e.ymax]=s(t.xmax,t.ymax,u),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 f(t,n){if(!t||!n)return;const r=Array.isArray(t)?t.map(s=>null!=s.geometry?s.geometry.spatialReference:null).filter(s):[t];await e(r.map(s=>({source:s,dest:n})))}const x=p.bind(null,m),y=p.bind(null,l);function g(s,t,e,m){if(!s)return null;if(e||(e=t,t=s.spatialReference),!r(t)||!r(e)||i(t,e))return s;if(a(t,e)){const t=o(e)?x(s):y(s);return t.spatialReference=e,t}return n([s],t,e,m)[0]}class _{constructor(){this._jobs=[],this._timer=null,this._process=this._process.bind(this)}async push(s,e,n,r){if(!s?.length||!e||!n||i(e,n))return s;const o={geometries:s,inSpatialReference:e,outSpatialReference:n,options:r,resolve:t()};return this._jobs.push(o),this._timer??=setTimeout(this._process,10),o.resolve.promise}_process(){this._timer=null;const s=this._jobs.shift();if(!s)return;const{geometries:t,inSpatialReference:e,outSpatialReference:r,resolve:i,options:m}=s,l=t.filter(s=>null!=s);let u;u=a(e,r)&&null==m?.extendedParams?o(r)?l.map(x):l.map(y):n(l,e,r,m);let p=0;i(t.map(s=>null==s?null:u[p++])),this._jobs.length>0&&(this._timer=setTimeout(this._process,10))}}const j=new _;function M(s,t,e,n){return j.push(s,t,e,n)}export{f as checkProjectionSupport,g as project,M as projectMany};
