/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import s from"./TileKey.js";function e(s,e){const i=new Set;for(const t of s instanceof Set?s.values():s.keys())e.has(t)||i.add(t);return i}class i{constructor(s,e,i){this.target=new Map,this.previous=new Map;const t=i?s.getTileCoverage(i,0,!0,"closest"):null,r=s.getTileCoverage(e,0,!0,"closest");if(this.target.clear(),this.previous.clear(),t)for(const o of t.keys())this.previous.set(o.id,o);if(r)for(const o of r.keys())this.target.set(o.id,o)}*values(){yield*this.target.values(),yield*this.previous.values()}*keys(){yield*this.target.keys(),yield*this.previous.keys()}}class t{constructor(s){this.version=s}}class r{constructor(s){this._subscriptions=new Map,this._visible=new Set,this._version=0,this._strategy="eager",this._config=s}destroy(){}get coverage(){return this._coverage}get subscriptions(){return new Set(this._subscriptions.keys())}setVisibilityStrategy(s){this._strategy=s}suspend(){this._suspendedCoverage=this._coverage,this._coverage=null,this._updateSubscriptions()}resume(){null==this._coverage&&(this._coverage=this._suspendedCoverage,this._suspendedCoverage=null,this._updateSubscriptions())}update(s,e){return this._version=(this._version+1)%Number.MAX_SAFE_INTEGER,this._coverage=new i(this._config.tileInfoView,s,e),this._updateSubscriptions(),new Set(this._visible)}updateVisibility(){const s=this._updateVisibility();return this._visible=s,this._visible}_updateSubscriptions(){if(!this._coverage)return;const s=this._updateVisibility(),i=new Set(this._coverage.keys()),r=e(this._subscriptions,s),o=e(i,this._subscriptions),n=e(r,i);this._visible=s;for(const e of o.values())this._subscriptions.set(e,new t(this._version));for(const e of n.values())this._subscriptions.delete(e);(o.size||n.size)&&this._sendUpdateSubscriptions(o,n)}_sendUpdateSubscriptions(s,e){const i=Array.from(s.values()).map(s=>({tileId:s,version:this._subscriptions.get(s).version}));this._config.updateSubscriptions({subscribe:i,unsubscribe:Array.from(e.values())})}_updateVisibility(){if(!this._coverage)return new Set;switch(this._strategy){case"eager":return this._updateVisibilityEager(this._coverage);case"target-defer":return this._updateVisibilityTargetDefer(this._coverage)}}_updateVisibilityEager(s){const e=new Set;for(const i of s.values()){if(this._config.isDone(i)){e.add(i.id);continue}if(this._addVisibleParent(e,i))continue;this._addVisibleChildren(e,i)||e.add(i.id)}return e}_updateVisibilityTargetDefer(s){const e=new Set,i=Array.from(s.target.values());if(!i.some(s=>!this._config.isDone(s))){for(const s of i)e.add(s.id);return e}for(const t of s.values()){if(this._addVisibleParent(e,t))continue;this._addVisibleChildren(e,t)||e.add(t.id)}return e}_addVisibleParent(e,i){let t=!1;for(const r of this._visible.values()){new s(r).containsChild(i)&&(e.add(r),t=!0)}return t}_addVisibleChildren(e,i){let t=!1;for(const r of this._visible.values()){const o=new s(r);i.containsChild(o)&&(e.add(r),t=!0)}return t}}export{r as FeatureTileSubscriptionManager};
