/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../../core/Collection.js";import t from"../../core/Error.js";import{JSONMap as r}from"../../core/jsonMap.js";import i from"../../geometry/SpatialReference.js";import{createFileProvider as o}from"../graphics/sources/parquet/createFileProvider.js";import n from"./Field.js";import{isNumericField as a}from"./fieldUtils.js";import{fromParquetGeometryEncodingJSON as l}from"./parquetEncodingUtils.js";import s from"./ParquetFilesData.js";const p=new r({esriGeometryPoint:"point",esriGeometryPolygon:"polygon",esriGeometryPolyline:"polyline",esriGeometryMultipoint:"multipoint"});function m(e){return p.toJSON(e)}function d(e){return p.fromJSON(e)}async function u(t,r={}){const i=Array.isArray(t)?new s({urls:new e(t)}):t,n=await y(i,r),a=o(n,null,()=>r.customParameters);return f(await a.getMetadata(),{data:i})}async function f(e,r){if(r.geometryType&&r.spatialReference&&r.geometryEncoding&&r.fields)return r;if(!e)throw new t("parquet:bad-input","No metadata found");const o={...r};if(null==o.fields&&(o.fields=e.fields.map(e=>n.fromJSON(e))),null!=o.geometryEncoding){const e=o.geometryEncoding;switch(e.type){case"wkb":{const r=o.fields.find(t=>t.name===e.field);if(!r)throw new t("parquet:unsupported",`Geometry encoding references field ${e.field} which does not exist`);if("blob"!==r.type)throw new t("parquet:unsupported",`Invalid field type for geometry encoding. Found ${r.type} but expected 'blob'`);break}case"location":for(const r of[e.xField,e.yField])if(null!=r){const e=o.fields.find(e=>e.name===r);if(!e)throw new t("parquet:unsupported",`Geometry encoding references field ${r} which does not exist`);if(!a(e))throw new t("parquet:unsupported",`Invalid field type for location geometry encoding. Found ${e.type} but expected a numeric`)}}}return null==o.geometryEncoding&&(o.geometryEncoding=e.geometryEncoding?l(e.geometryEncoding):null),o.fields=c(o.fields,e),!o.displayOptimization&&e.displayOptimization&&(o.displayOptimization=e.displayOptimization),!o.geometryType&&e.geometryType&&(o.geometryType=d(e.geometryType)),!o.spatialReference&&e.spatialReference&&(o.spatialReference=i.fromJSON(e.spatialReference)),o}function c(e,t){const r=t.geoMetadata;if(null==r)return e;const i=new Set(Object.keys(r.columns));return e.filter(e=>!i.has(e.name))}async function y(e,r){switch(e.type){case"files":return{type:"urls",urls:e.urls.toArray()};case"portal-item":{const i=await e.portalItem.load(r);switch(i.type){case"Apache Parquet":return{type:"portal-parquet-file",itemUrl:i.itemUrl,itemCdnUrl:i.itemCdnUrl,modified:i.modified,size:i.size};case"Parquet layer":case"Parquet Layer":case"Parquet feature layer":case"Parquet Feature Layer":if(!i.url)throw new t("parquet:bad-item",`Item of type ${i.type} must have a url defined`,i);return{type:"portal-parquet-layer",itemUrl:i.itemUrl,itemCdnUrl:i.itemCdnUrl,itemMetadata:{modified:i.modified,url:i.url}};default:throw new t("parquet:bad-item",`Item of type ${i.type} is not supported`,i)}}}}export{f as completeParquetLayerInfo,y as createFileProviderSchema,d as fromParquetJSONGeometryType,u as getParquetLayerInfo,p as parquetGeometryTypeKebabDict,m as toParquetJSONGeometryType};
