/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../request.js";import t from"../../core/Error.js";import"../../core/has.js";import{QueueProcessor as n}from"../../core/QueueProcessor.js";import{isBlobProtocol as o}from"../../core/urlUtils.js";import{loadParquetModule as r}from"./loadParquetModule.js";const a=new n({concurrency:8,process:async t=>{const{data:n}=await e(t.url,t.options);return n}}),s=e=>async function(t,n,o,r){const s=e?.(),i=s??{};return await a.push({url:t,options:{responseType:"array-buffer",query:i,headers:{range:`bytes=${n}-${o}`}}})},i=t=>async function(n){if(o(n)){const{data:t}=await e(n,{responseType:"blob"});return t.size}const{data:r}=await e(n,{responseType:"native",method:"head",query:t?.()}),a=r.headers.get("Content-Length");if(null==a)throw new Error("Unable to parse content length");return parseInt(a,10)};function l(e,n){switch(n){case"esriGeometryPoint":return e.GeometryType.Point;case"esriGeometryPolygon":case"polygon":return e.GeometryType.Polygon;case"esriGeometryPolyline":case"polyline":return e.GeometryType.Polyline;case"esriGeometryMultipoint":case"multipoint":return e.GeometryType.Multipoint;default:throw new t("parquet",`Found unexpected GeometryType: ${n}`)}}function u(e,t){const n=t.encoding,o=l(e,t.geometryType),r=t.spatialReference.wkid;if(null==r)throw new Error("InternalError: Wkid must be defined.");switch(n.type){case"wkb":return e.GeometryField.fromWkb(n.field,r,o);case"location":{const{yField:t,xField:a}=n;return e.GeometryField.fromLocation(t,a,r,o)}}}function c(e,t){const n=e.GeometryInfo.new();t.geometry&&n.setGeometry(u(e,t.geometry));const o=t.displayOptimization;if(o){const{index:t,parentColumn:r}=o,a=e=>null!=r?[r,e].join("."):e;if("z"===t.type){const o=e.DisplayOptimizationZBuilder.new();o.setCodeField(a(t.code)),o.setXColumn(a(t.xColumn)),o.setYColumn(a(t.yColumn)),o.setCooordinatePrecision(t.coordinatePrecision),o.setFullExtent(t.fullExtent),o.setSpatialReference(t.wkid,t.wkt),o.setHasZ(!!t.zColumn),o.setHasM(!!t.mColumn),n.setOptmizationZ(o)}if("xz"===t.type){const o=l(e,t.geometryType),r=e.DisplayOptimizationXZBuilder.new();r.setCodeField(a(t.code)),r.setEncoding(t.encoding),r.setFullExtent(t.fullExtent),r.setGeometryType(o),r.setSpatialReference(t.wkid,t.wkt),r.setMaxLevel(t.maxLevel),r.setHasZ(t.hasZ??!1),r.setHasM(t.hasM??!1);for(const n of t.levels??[]){const[t,o]=n.transform.translate,[s,i]=n.transform.scale,l=new Float64Array([t,o]),u=new Float64Array([s,i]),c=a(n.column);r.addLevel(e.MultiscaleGeometryField.new(n.level,n.scale,c,l,u))}n.setOptmizationXZ(r)}}return n}async function p(e,n={}){const o=await r(),a=s(n.getCustomParameters),l=i(n.getCustomParameters),u=n.geometryInfo?c(o,n.geometryInfo):null;try{return await o.ParquetFile.fromUrl(e,a,l,u)}catch(p){throw new t("parquet","Failed to parse file",{error:p})}}function d(e){const t=e.keyValueMetadata("geo");return null!=t?JSON.parse(t):null}function y(e){if(e.keyValueMetadata("esri"))throw new t("parquet:unsupported","File was created using an unsupported experimental display index. Please regenerate the file.");const n=e.keyValueMetadata("geodisplay");if(null!=n)return JSON.parse(n);const o=e.keyValueMetadata("org.apache.spark.sql.parquet.row.metadata");if(null!=o){const e=JSON.parse(o);for(const n of e.fields)if(null!=n.metadata&&"type"in n.metadata&&("xz"===n.metadata.type||"z"===n.metadata.type)){const e={parentColumn:n.name,index:n.metadata};if("xz"===e.index.type&&(e.index.geometryType=e.index.geometryType.toLowerCase(),"esri-pbf"===e.index.encoding&&(e.index.encoding="esriPBF"),"esriPBF"!==e.index.encoding))throw new t("parquet:unsupported",`Encoding for display index must be of type esriPBF, but found ${e.index.encoding}`);return e}}return null}export{p as createParquetFile,d as readGeoMetadata,y as readGeodisplayMetadata};
