/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{parseWhereClause as e}from"../../../../../../../core/sql.js";import{ASourceChunk as t}from"./ASourceChunk.js";import{FeatureSetReaderJSON as s}from"../../../support/FeatureSetReaderJSON.js";const r=1e4,i=1e3;class a{static async create(t){const{metadata:s,definitionExpression:r}=t,i=r?await e(r,s.fieldsIndex):null;return new a(s,i,r)}constructor(e,t,s){this.metadata=e,this._clause=t,this._definitionExpression=s}get hash(){return this._definitionExpression}testFeature(e){return null==this._clause||this._clause.testFeature(e)}}class o{constructor(){this.modified=new Map,this.removed=new Set}modify(e){this.modified.set(e.objectId,e),this.removed.has(e.objectId)&&this.removed.delete(e.objectId)}remove(e){this.modified.delete(e),this.removed.add(e)}get isEmpty(){return 0===this.modified.size&&0===this.removed.size}applyWhereClause(e){const t=new o;for(const[s,r]of this.modified)e.testFeature(r)?t.modified.set(s,r):t.removed.add(r.objectId);for(const s of this.removed)t.removed.add(s);return t}}class n extends t{constructor(e){super(),this._reader=e,this.chunkId="override",this.normalizedChunkId="override"}static fromFeatures(e,t){const r=s.fromOptimizedFeatures(e,t);return new n(r)}get reader(){return this._reader}get queryInfo(){return{}}get first(){return!1}get end(){return!1}get lodInfo(){return null}getTileReader(e){if(!this._reader.getSize())return null;const t=this.queryFeaturesInBounds(e.bounds);return t.setTransformForDisplay(e.transform),t}}class d{constructor(e,t){this.inner=e,this.isWeak=t,this.lastWeak=null}get isStrong(){return!this.isWeak}}class h{constructor(e){this._parameters=e,this._overrides=new Map,this._update=new o,this._lastCleanup=0}update(e){this._parameters=e}hasOverride(e){return this._overrides.has(e)}onChunkInsert(e){if(this._overrides.size){const t=e.reader.getCursor();for(;t.next();){const e=t.getObjectId(),s=this._overrides.get(e);if(s?.lastWeak&&(s.lastWeak=null),s?.isWeak){const e=t.readOptimizedFeatureWorldSpace(),r=s.inner?.attributes??{};e.attributes={...r,...e.attributes},s.inner=e,this._update.modify(e),this.invalidate()}}}e.registerOverrides(this)}apply(e,t){const{updateWeak:s,removeWeak:r,update:i,remove:a,release:o}=e.commands;this.invalidate();for(const n of s){const e=new d(n,!0),t=this._overrides.get(n.objectId);t?.isStrong?t.lastWeak=e:(this._overrides.set(n.objectId,e),this._update.modify(n))}for(const n of i){const e=new d(n,!1),t=this._overrides.get(n.objectId);e.lastWeak=t?.isWeak?t:t?.lastWeak??null,this._overrides.set(n.objectId,e),this._update.modify(n)}for(const n of r){const e=new d(null,!0),t=this._overrides.get(n);t?.isStrong?t.lastWeak=e:(this._overrides.set(n,e),this._update.remove(n))}for(const n of a){const e=new d(null,!1),t=this._overrides.get(n);e.lastWeak=t?.isWeak?t:t?.lastWeak??null,this._overrides.set(n,e),this._update.remove(n)}if(o.length){const e=new Set;for(const t of o){const s=this._overrides.get(t);s?.lastWeak?(this._overrides.set(t,s.lastWeak),null==s.lastWeak.inner?this._update.remove(t):this._update.modify(s.lastWeak.inner)):s&&!s.isWeak&&(this._overrides.delete(t),e.add(t))}t.forEachUnsafe(t=>{const s=t.getObjectId();e.has(s)&&(this._update.modify(t.readOptimizedFeatureWorldSpace()),e.delete(s))});for(const t of e.values())this._update.remove(t)}}clearWeakOverrides(){for(const[e,t]of this._overrides.entries())t.isWeak&&this._overrides.delete(e);this.invalidate()}cleanup(e){if(this._overrides.size<r)return;const t=performance.now();if(t-this._lastCleanup<i)return;this._lastCleanup=t;const s=this._getWeakDeletions();if(!(s.size<r)){for(const t of e){const e=t.reader.withoutOverrides().getCursor();for(;e.next();){const t=e.getObjectId();s.delete(t)}}for(const e of s)this._overrides.delete(e);s.size&&this.invalidate()}}takeOverrideUpdate(){const e=this._update;return e.isEmpty?null:(this._update=new o,e.applyWhereClause(this._parameters))}asChunk(){const e=this._parameters;if(this._lastOverrideParametersHash!==e.hash&&(this._lastOverrideParametersHash=e.hash,this._chunk=null),!this._chunk){const t=[];for(const s of this._overrides.values())null!=s.inner&&e.testFeature(s.inner)&&t.push(s.inner);this._chunk=n.fromFeatures(t,e.metadata)}return this._chunk}invalidate(){this._chunk=null}putWeakObjectIdsFromGlobalIds(e,t,s){for(const[r,i]of this._overrides.entries()){if(i.isWeak&&null!=i.inner){const a=i.inner.attributes[s];a&&t.has(a)&&!e.has(a)&&e.set(a,r);continue}if(null!=i.lastWeak&&null!=i.lastWeak.inner){const a=i.lastWeak.inner.attributes[s];a&&t.has(a)&&!e.has(a)&&e.set(a,r)}}}_getWeakDeletions(){const e=new Set;for(const[t,s]of this._overrides.entries())s.isWeak&&null==s.inner&&e.add(t);return e}}export{n as OverrideChunk,a as OverrideParameters,h as OverrideState,o as OverrideUpdate};
