import e from"@arcgis/core/geometry/Multipoint.js";import t from"@arcgis/core/geometry/Point.js";import r from"@arcgis/core/geometry/Polygon.js";import i from"@arcgis/core/geometry/Polyline.js";import n from"@arcgis/core/geometry/SpatialReference";import{ArgumentError as o}from"../ArgumentError.js";import{checkArg as s}from"./checkArg.js";import{isExtent as a,isMultipoint as c,isPoint as m,isPolygon as f,isPolyline as p}from"./esri.js";import{parse as u}from"./number.js";const h=/^(POINT|MULTIPOINT|LINESTRING|MULTILINESTRING|POLYGON)\s(?:(ZM|Z|M)\s)?(\(.+\))$/,l=/^\(?([-\d.]+)\s([-\d.]+)(?:\s([-\d.]+))?(?:\s([-\d.]+))?\)?$/,d=/^\(([-\d.\s(),]+)\)$/,T=/^(PROJCS|GEOGCS|VERTCS|GEOGTRAN|VERTTRAN)\s?\[\s?"([^"]+)",.*/,w={4326:{name:"GCS_WGS_1984",wkt:'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]'},102100:{name:"WGS_1984_Web_Mercator_Auxiliary_Sphere",wkt:'PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Mercator_Auxiliary_Sphere"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",0.0],PARAMETER["Standard_Parallel_1",0.0],PARAMETER["Auxiliary_Sphere_Type",0.0],UNIT["Meter",1.0]]'}},G={PROJCS:"projected",GEOGCS:"geographic",VERTCS:"vertical",GEOGTRAN:"transformations",VERTTRAN:"vertical-transformations"},R={};export function wktToEsri(a,c){s("geometry",a).isNotMissing();const m=a.match(h);if(m){const o=m[2]?.includes("Z"),s=m[2]?.includes("M");switch(m[1]){case"POINT":{const e=N(m[3],m[2]);return new t({x:e[0],y:e[1],z:o?e[2]:void 0,m:!o&&s?e[2]:s?e[3]:void 0,hasZ:o,hasM:s,spatialReference:c??n.WGS84})}case"MULTIPOINT":{const t=E(m[3],m[2]);return new e({points:t,hasZ:o,hasM:s,spatialReference:c??n.WGS84})}case"LINESTRING":{const e=E(m[3],m[2]);return new i({paths:[e],hasZ:o,hasM:s,spatialReference:c??n.WGS84})}case"MULTILINESTRING":{const e=S(m[3],m[2]);return new i({paths:e,hasZ:o,hasM:s,spatialReference:c??n.WGS84})}case"POLYGON":{const e=S(m[3],m[2]);return new r({rings:e,hasZ:o,hasM:s,spatialReference:c??n.WGS84})}}}throw new o("Cannot determine what type of esri geometry to create")}export function esriToWKT(e){s("geometry",e).isNotMissing();const t=`${e.hasZ?"Z":""}${e.hasM?"M":""}${e.hasM||e.hasZ?" ":""}`;if(m(e)){const{x:r,y:i,z:n,m:o}=e,s=[r,i];return e.hasZ&&s.push(n),e.hasM&&s.push(o),`POINT ${t}(${y(s)})`}if(c(e))return`MULTIPOINT ${t}(${O(e.points)})`;if(p(e))return e.paths.length>1?`MULTILINESTRING ${t}(${I(e.paths)})`:`LINESTRING ${t}(${O(e.paths[0])})`;if(f(e))return`POLYGON ${t}(${I(e.rings)})`;if(a(e)){const r=[e.xmin,e.ymax];return`POLYGON ${t}(${I([[r,[e.xmax,e.ymax],[e.xmax,e.ymin],[e.xmin,e.ymin],r]])})`}throw new o("Cannot determine what type of WKT geometry to create")}export async function esriWkidToWkt(e){s("wkid",e).isNotMissing();let t=w[`${e}`];if(!t)for(const r of Object.values(G)){if(t=(await _(r))[e],t){const r=t.wkt.match(T);if(r){w[e]={name:r[2],wkt:t.wkt};break}}}if(!t)throw new Error(`Failed to find Wkt for id ${e}`);return t.wkt}export async function esriWktToWkid(e){s("wkt",e).isNotMissing();const t=e.match(T);if(t){let e=Object.keys(w).find((e=>w[e].name===t[2]));if(!e){const r=G[t[1]];if(r){const i=await _(r);e=Object.keys(i).find((e=>i[e].name===t[2])),e&&(w[e]={name:t[2],wkt:i[e].wkt})}}if(e)return parseInt(e)}throw new Error(`Failed to find Wkid for ${e}`)}function S(e,t){const r=e.match(d);if(r)return M(r[1]).map((e=>E(e,t)));throw new o(`Detected invalid WKT 2D point array: ${e}`)}function E(e,t){const r=e.match(d);if(r)return M(r[1]).map((e=>N(e,t)));throw new o(`Detected invalid WKT point array: ${e}`)}function M(e){let t=e.trim().replace(/\s*,\s*/g,","),r=0;for(let e=0;e<t.length;e++)switch(t.charAt(e)){case"(":r++;break;case")":r--;break;case",":0===r&&(t=`${t.substr(0,e)}&${t.substr(e+1)}`)}return t.split("&")}function N(e,t){const r=t??"",i=e.match(l);if(i){const e=[g(i[1]),g(i[2])];return(r.includes("Z")||!r.includes("Z")&&r.includes("M"))&&i[3]&&e.push(g(i[3])),r.includes("M")&&i[4]&&e.push(g(i[4])),e}throw new o(`Detected invalid WKT point: ${e}`)}function g(e){return u({locale:"inv"},e)}function I(e){return e.map((e=>`(${O(e)})`)).join(", ")}function O(e){return e.map(y).join(", ")}function y(e){return e.join(" ")}async function _(e){if(void 0===R[e])switch(e){case G.GEOGCS:R[e]=(await import("esri-proj-codes/geographic.json")).default;break;case G.PROJCS:R[e]=(await import("esri-proj-codes/projected.json")).default;break;case G.GEOGTRAN:R[e]=(await import("esri-proj-codes/transformations.json")).default;break;case G.VERTCS:R[e]=(await import("esri-proj-codes/vertical.json")).default;break;case G.VERTTRAN:R[e]=(await import("esri-proj-codes/vertical-transformations.json")).default}const t=R[e];if(!t)throw new Error(`Invalid esri-proj-codes module: ${e}`);return t}