/*
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"../../../../../../core/Error.js";import{notDeepEqual as s}from"../../../../../../core/object.js";import{throwIfAborted as t}from"../../../../../../core/promiseUtils.js";import{difference as i}from"../../../../../../core/SetUtils.js";import r from"../../../../../../layers/support/FieldsIndex.js";import{loadParquetModule as a}from"../../../../../../libs/parquet/loadParquetModule.js";import{createParquetFile as l}from"../../../../../../libs/parquet/parquet.js";import{ALoadStrategy as o}from"./ALoadStrategy.js";import{FeatureSnapshotSourceChunk as n}from"./chunks/FeatureSnapshotSourceChunk.js";import{FeatureSetReaderJSON as u}from"../../support/FeatureSetReaderJSON.js";import{FeatureSetReaderParquet as h}from"../../support/FeatureSetReaderParquet.js";const m=12;class d extends o{constructor(e,s,t){super(e.metadata,s,t),this._service=e,this._chunkId=0,this._files=new Map,this._chunksByFile=new Map}destroy(){super.destroy();for(const{file:e}of this._files.values())e.free();this._files.clear()}get about(){return{willQueryAllFeatures:!0,willQueryFullResolutionGeometry:!0}}get availableFields(){return this._schema.partial.availableFields}get definitionExpression(){return this._schema.full.definitionExpression}async tryUpdate(t,i){if(s(this.availableFields,i.availableFields)&&await this._updateFields(i.availableFields),s(this._schema.partial.urls,i.urls)){for(const e of i.urls)this._files.has(e)||await this._insert(e);for(const s of this._files.keys())if(!i.urls.includes(s))throw new e("unsupported","Removing parquet files is not supported",{previous:this._schema.partial.urls,next:i.urls})}return this._schema.partial=i,!0}async load(e){return null==this._promise&&(this._promise=this._download()),this._promise}unload(e){}_onOverride(){}async _updateFields(e){await this._promise;const s=new Set(e),t=i(s,new Set(this.availableFields));if(null!=this._fieldsIndex&&t.size){const e=Array.from(t),s=[];for(const[t,i]of this._chunksByFile.entries()){const{file:r}=this._files.get(t);for(const t of i)s.push(r.updateChunkFields(t,e))}await Promise.all(s)}}_ensureFieldsIndex(e){if(!this._fieldsIndex){const s=e.fields(!1).map(s=>({name:s.name,alias:s.name,type:s.type,column:e.columnForFieldName(s.name)})),{timeZoneByFieldName:t}=this._service.metadata.fieldsIndex,i=r.fromJSON({fields:s,timeZoneByFieldName:t});this._fieldsIndex=i}return this._fieldsIndex}async _insert(e){const s=await a(),i=await l(e,{geometryInfo:this._service.geometryInfo,getCustomParameters:()=>this._schema.full.customParameters}),r=this._files.size;this._files.set(e,{file:i,fileId:r}),t(this._options);const o=this._ensureFieldsIndex(i),u=s.Query.new();u.setOutFields(this.availableFields.filter(e=>null!=o.get(e)?.column)),u.setReturnGeometry(!0),u.setOutSpatialReference(this.metadata.outSpatialReference.toJSON().wkid);const d=await i.executeQuery(u,this._signal);t(this._signal);const c=[],f=d.next(this._signal);for(let t=0;t<m;t++)c.push(d.next(this._signal));const p=[];this._chunksByFile.set(e,p);let _=await f;for(;null!=_;){t(this._signal),p.push(_);const e=this._chunkId++,s=new h(this.metadata,o,_,r),i=new n(s,null,e,!1);this.store.insert(i);const a=c.shift();c.push(d.next(this._signal)),_=await a}}async _download(){try{await Promise.all(this._schema.partial.urls.map(e=>this._insert(e)));const e=new n(u.empty(this.metadata),null,-1,!0);this.store.insert(e)}catch(e){throw console.error(e),e}}}export{d as ParquetSnapshotLoadStrategy};
