/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import e from"../../core/Collection.js";import t from"../../core/Error.js";import{JSONMap as o}from"../../core/jsonMap.js";import n from"../../geometry/SpatialReference.js";import i from"./Field.js";import{isNumericField as r}from"./fieldUtils.js";import{inferLocationInfo as s}from"./locationUtils.js";import l from"./ParquetGeometryEncodingLocation.js";import c from"./ParquetGeometryEncodingWkb.js";import{createParquetFile as p,readGeoMetadata as a,readGeodisplayMetadata as u}from"../../libs/parquet/parquet.js";const m=new o({esriGeometryPoint:"point",esriGeometryPolygon:"polygon",esriGeometryPolyline:"polyline",esriGeometryMultipoint:"multipoint"}),f=new Set(["uncompressed","snappy","gzip"]);function d(e){return m.toJSON(e)}function y(e){return m.fromJSON(e)}function g(t,o={}){return w({urls:new e(t)},o)}async function w(e,o={}){if(e.urls.length<1)throw new t("parquet:bad-input","At least one url must be specified",e);if(e.geometryType&&e.spatialReference&&e.geometryEncoding&&e.fields)return e;const s=await p(e.urls.getItemAt(0),{getCustomParameters:()=>o.customParameters});for(const n of s.compressionCodecs())if(!f.has(n))throw new t("parquet:unsupported",`Compression codec ${n} is unsupported. Must be of type ${Array.from(f).join(",")}`);const l=a(s),c={...e,file:s};if(null==c.fields&&(c.fields=s.fields(!1).map(e=>i.fromJSON({name:e.name,alias:e.name,type:e.type}))),null!=c.geometryEncoding){const e=c.geometryEncoding;switch(e.type){case"wkb":{const o=c.fields.find(t=>t.name===e.field);if(!o)throw new t("parquet:unsupported",`Geometry encoding references field ${e.field} which does not exist`);if("blob"!==o.type)throw new t("parquet:unsupported",`Invalid field type for geometry encoding. Found ${o.type} but expected 'blob'`);break}case"location":for(const o of[e.xField,e.yField])if(null!=o){const e=c.fields.find(e=>e.name===o);if(!e)throw new t("parquet:unsupported",`Geometry encoding references field ${o} which does not exist`);if(!r(e))throw new t("parquet:unsupported",`Invalid field type for location geometry encoding. Found ${e.type} but expected a numeric`)}}}null==c.geometryEncoding&&(c.geometryEncoding=b(l,c.fields));const m=u(s);if(m&&(c.displayOptimization=m),c.geometryEncoding)switch(c.geometryEncoding.type){case"location":null==c.spatialReference&&(c.spatialReference=n.WGS84),null==c.geometryType&&(c.geometryType="point");break;case"wkb":{if(!l)return c;const e=l.primary_column,t=l.columns[e];if(c.geometryType||(c.geometryType=j(t)),c.spatialReference||(c.spatialReference=k(t)),c.fields)for(const o of Object.keys(l.columns))c.fields=c.fields.filter(e=>e.name!==o)}}if(c.displayOptimization){const e=c.displayOptimization.index;if(!c.spatialReference&&e.wkid&&(c.spatialReference=new n({wkid:e.wkid})),!c.geometryType)switch(e.type){case"z":c.geometryType="point";break;case"xz":c.geometryType=e.geometryType}}return c}function b(e,t){if(null!=e){const t=e.primary_column,o=e.columns[t];return new c({field:t,orientation:"counterclockwise"===o.orientation?"counter-clockwise":null})}const o=t.filter(e=>r(e)).map(e=>e.name),n=s(o);return n.latitudeFieldName&&n.longitudeFieldName?new l({xField:n.longitudeFieldName,yField:n.latitudeFieldName}):null}function h(e){switch(e){case"Point":return"point";case"Polygon":case"MultiPolygon":return"polygon";case"LineString":return"polyline";case"MultiPoint":return"multipoint";default:return null}}function j(e){const{geometry_types:o}=e,n=new Set;for(const t of o){const e=h(t);e&&n.add(e)}if(n.size>1)throw new t("parquet:unsupported","Parquet mixed geometry types are not supported",{geometryTypes:n});return 1===n.size?n.values().next().value:void 0}function k(e){const t=e.crs?.id?.code;return t&&"number"==typeof t?new n({wkid:t}):void 0}export{w as completeParquetLayerInfo,y as fromParquetJSONGeometryType,g as getParquetLayerInfo,m as parquetGeometryTypeKebabDict,d as toParquetJSONGeometryType};
