import r from"@arcgis/core/geometry/SpatialReference";import*as e from"@arcgis/core/geometry/operators/projectOperator";import*as t from"@arcgis/core/geometry/operators/support/geographicTransformationUtils";import{canProject as o,project as a}from"@arcgis/core/geometry/support/webMercatorUtils";import s from"@arcgis/core/rest/support/ProjectParameters";import{ArgumentError as n}from"../ArgumentError.js";import{config as i}from"../config.js";import{GeometryServiceError as c}from"./GeometryServiceError.js";import{checkArg as f}from"./checkArg.js";export async function project(m,u,l){const g=Array.from(m);if(g.length<=0)return g;const w=function(e){for(const r of e)if(null!==r?.spatialReference)return r.spatialReference;return r.WebMercator}(m),y=function(r,e,t){if(t?.hasOwnProperty("steps"))return t;return function(r,e){if(f("inSr",r).isNotMissing(),f("outSR",e).isNotMissing(),0===i.autoTransformations.length)return;const t=i.autoTransformations.filter((e=>e.inSpatialReference.equals(r))),o=t.filter((r=>r.outSpatialReference.equals(e)));if(o.length>0)return o[0].transformation;const a=i.autoTransformations.filter((r=>r.inSpatialReference.equals(e))),s=a.filter((e=>e.outSpatialReference.equals(r)));if(s.length>0)return s[0].transformation;return}(r,e)}(w,u,l);try{if(w.equals(u))return g;if(o(w,u))return g.map((r=>a(r,u)));e.isLoaded()||await e.load(),t.isLoaded()||await t.load();return e.executeMany(g,u,{geographicTransformation:y}).filter((r=>null!=r))}catch{}if(y&&y.steps.length>1)throw new n("GeographicTransformation","Geometry Service can't handle a two step transformation");return async function(r,e,t){const o=new s({geometries:r,outSpatialReference:e});t?.hasOwnProperty("steps")&&null!=t.steps[0]&&(o.transformation=t.steps[0].wkid?{wkid:t.steps[0].wkid}:{wkt:t.steps[0].wkt},o.transformForward=!t.steps[0].isInverse);try{const{project:r}=await import("@arcgis/core/rest/geometryService");return await r(i.geometryServiceUrl??p,o)}catch(r){throw r instanceof Error?new c(r):r}}(g,u,y)}const p="https://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer";