/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import t from"../../../../../../core/Error.js";import"../../../../../../core/has.js";import{notDeepEqual as e}from"../../../../../../core/object.js";import{throwIfAborted as s}from"../../../../../../core/promiseUtils.js";import{difference as i}from"../../../../../../core/SetUtils.js";import{openWithPorts as a}from"../../../../../../core/workers/workers.js";import{loadParquetModule as r}from"../../../../../../libs/parquet/loadParquetModule.js";import{ALoadStrategy as o}from"./ALoadStrategy.js";import{ParquetSnapshotSourceChunk as l}from"./chunks/ParquetSnapshotSourceChunk.js";import{FeatureSetReaderParquet as n}from"../../support/FeatureSetReaderParquet.js";const h=8;class d extends o{constructor(t,e,s,i){super(t.metadata,e,s),this._service=t,this._files=new Map,this._loaded=this._load(i)}destroy(){super.destroy(),this._loaded.finally(()=>{this._client.close(),this._client=null;for(const t of this._ports)t.close()}).catch(()=>{})}get about(){return{willQueryAllFeatures:!0,willQueryFullResolutionGeometry:!0}}get availableFields(){return this._schema.partial.availableFields}get definitionExpression(){return this._schema.full.definitionExpression}async tryUpdate(s,i){if(await this._loaded,e(this.availableFields,i.availableFields)&&await this._updateFields(i.availableFields),e(this._schema.partial.urls,i.urls)){for(const t of i.urls)this._files.has(t)||await this._loadFile(t);for(const e of this._files.keys())if(!i.urls.includes(e))throw new t("unsupported","Removing parquet files is not supported",{previous:this._schema.partial.urls,next:i.urls})}return this._schema.partial=i,!0}async load(t){return this._loaded}unload(t){}_onOverride(){}async _load(t){this._ports=await t.layerView.openMemoryPorts(),this._client=await a(this._ports,{maxNumberOfConcurrentJobs:20}),this._proxy=this._client.createInvokeProxy(),this._indexMap=await this._proxy.getIndexMap(),await this._download()}async _updateFields(t){const e=new Set(t),s=i(e,new Set(this.availableFields));if(null!=this._service.metadata.fieldsIndex&&s.size){const t=Array.from(this.store.insertedChunks()).map(t=>t.updateFields(this._proxy,Array.from(s),this._options));await Promise.all(t)}}async _loadFile(t){const e=await r(),i=await this._proxy.getFileId(t);this._files.set(t,i),s(this._options);const a=await this._proxy.queryStream(t,{type:"snapshot",outFields:Array.from(this.availableFields),returnGeometry:!0,outSpatialReference:this.metadata.outSpatialReference.toJSON()},this._options);let o=0;const d=[],p=async(r=0)=>{const h=await this._proxy.getStreamNext(t,a,this._options);if(s(this._options),null==h)return;const d=e.ParquetChunk.deserialize(new Uint8Array(h)),u=new n(this.metadata,this._indexMap,d,i),c=new l(this.metadata,u,o++,!1,i,t);return this.store.insert(c),p(r+1)};for(let s=0;s<h;s++)d.push(p());await Promise.all(d)}async _download(){try{await Promise.all(this._schema.partial.urls.map(t=>this._loadFile(t)));const t=new l(this.metadata,null,-1,!0,0,"");this.store.insert(t)}catch(t){throw console.error(t),t}}}export{d as ParquetSnapshotLoadStrategy};
