/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import has from"../../../../../core/has.js";import{AProcessorStrategy as e,UpdateBatch as t,AppendBatch as s,ASendState as r}from"./AProcessorStrategy.js";import{FeatureTileUpdateMessage as a,FeatureTileAppendMessage as o}from"../sources/FeatureSourceMessage.js";import{OverrideChunk as n}from"../sources/strategies/chunks/Overrides.js";import{createArcadeEvaluationOptions as d}from"../support/arcadeUtils.js";import{FeatureSetReaderJSON as i}from"../support/FeatureSetReaderJSON.js";class u extends r{}class h extends e{constructor(e,t,s,r){super(e,t,r),this._arcadeContextInfo=s,this.handledChunks=new Set,this.handledChunksForIdCreation=new Set,this.handledChunksForAttributeData=new Set,this._streamLayerDeferredObjectIdsToRemove=[]}destroy(){super.destroy();for(const e of this._source.chunks())this._cleanupChunkIds(e)}invalidateAttributeData(e){this.handledChunksForAttributeData.clear(),this._arcadeContextInfo=e,null!=this._evalOptions&&(this._evalOptions=d(this._evalOptions.$view.scale,e))}onSubscribe(e){super.onSubscribe(e),this._evalOptions=e.tile.createArcadeEvaluationOptions(this._arcadeContextInfo)}createState(e){return new u(e)}get aggregateQueryEngine(){return null}displayMap(e,t,s){const r=new Map(e.map(e=>[t(e),e])),a=[];for(const o of this._source.chunks()){const e=o.reader.getCursor();for(;e.next();){const t=e.getObjectId(),o=e.getDisplayId(),n=r.get(t);if(null!=n){const e=s(o,n,t);a.push(e),r.delete(t)}}}return a}getDisplayFeatures(e){const t=new Set(e),s=new Set,r=[];for(const a of this._source.chunks()){const e=a.reader.getCursor();for(;e.next();){const a=e.getObjectId(),o=e.getDisplayId();t.has(o)&&!s.has(a)&&(r.push({...e.readLegacyFeatureWorldSpace(),displayId:o}),s.add(a))}}return{features:r,aggregates:[],tracks:[]}}async applyOverrideUpdate(e){const s=new t,r=[];for(const t of e.modified.values()){const e=this._attributeStore.createDisplayIdForObjectId(t.objectId);t.displayId=e,r.push(e)}const o=i.fromOptimizedFeatures(Array.from(e.modified.values()),this._source.metadata).getCursor();for(;o.next();)this._attributeStore.setAttributeData(o.getDisplayId(),o,this._evalOptions,this._sqlOptions);const d=[];for(const t of e.removed){const e=this._attributeStore.getDisplayIdForObjectId(t);null!=e&&d.push(e)}has("esri-2d-update-debug")&&console.debug("FeatureUpdateStrategy.applyLocalEdit",{message:e,modifiedDisplayIds:r,removedDisplayIds:d});const u=n.fromFeatures(Array.from(e.modified.values()),this._source.metadata);this.handledChunks.add(u.chunkId),this.handledChunksForAttributeData.add(u.chunkId),this.handledChunksForIdCreation.add(u.chunkId);for(const t of this._sendStates.values())t.handledChunks.add(u.chunkId),s.messages.push(new a(t.subscription,null,r,!1,u.queryInfo));for(const t of this._sendStates.values()){const e=u.getTileReader(t.subscription.tile);s.messages.push(new a(t.subscription,e,d,!1,u.queryInfo))}for(const t of e.removed)this._attributeStore.releaseDisplayIdForObjectId(t);return s}async updateChunks(e,t){const r=new s;if(!e.length)return r;this._updateAttributeData();for(const s of this._sendStates.values()){const{subscription:t,handledChunks:a}=s;for(const n of e){const{chunkId:e}=n;if(a.has(e)||!this.handledChunksForIdCreation.has(e)||!this.handledChunksForAttributeData.has(e))continue;a.add(e);const d=n.getTileReader(t.tile);if(!d)continue;const i=new o(s.subscription,d,n.end,n.queryInfo);r.messages.push(i)}}return r}removeChunks(e){for(const t of e)this.handledChunks.delete(t.chunkId),this.handledChunksForAttributeData.delete(t.chunkId),this._cleanupChunkIds(t)}afterUpdateChunks(){for(const e of this._streamLayerDeferredObjectIdsToRemove)this._attributeStore.releaseDisplayIdForObjectId(e);this._streamLayerDeferredObjectIdsToRemove=[]}_cleanupChunkIds(e){if(this.handledChunksForIdCreation.has(e.chunkId)){const t=e.reader.getCursor();for(;t.next();){const e=t.getObjectId();this._source.isStream?this._streamLayerDeferredObjectIdsToRemove.push(e):this._attributeStore.releaseDisplayIdForObjectId(e)}this.handledChunksForIdCreation.delete(e.chunkId)}}_updateAttributeData(){for(const e of this._source.chunks()){const{chunkId:t,reader:s}=e;if(!this.handledChunksForIdCreation.has(t)){this.handledChunksForIdCreation.add(t);const e=s.getCursor();for(;e.next();){const t=this._attributeStore.createDisplayIdForObjectId(e.getObjectId());e.setDisplayId(t)}}}for(const e of this._source.chunks())if(this._attributeStore.referencesScale()||!this.handledChunksForAttributeData.has(e.chunkId)){this.handledChunksForAttributeData.add(e.chunkId);const t=e.reader.getCursor();for(;t.next();){const e=t.getDisplayId();this._attributeStore.setAttributeData(e,t,this._evalOptions,this._sqlOptions)}}}}export{u as DefaultSendState,h as FeatureUpdateStrategy};
