/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import has from"../../../../core/has.js";class e{constructor(){this._mode="eager"}updateCoverage(e){this._coverage=e}updateStrategy(e){this._mode=e}update(e){if(!this._coverage)return;const t=new Map;for(const d of e)t.set(d.id,d);switch(this._mode){case"eager":return this._updateEager(this._coverage,t);case"target-defer":return this._updateTargetDefer(this._coverage,t)}}setPresentMode(e){this._mode=e}_updateEager(e,t){const d=new Set,n=new Set;for(const r of e.values()){const e=r.id,i=t.get(e);if(i?.presenting){d.add(e),n.add(e);continue}let s=!0;if(i?.ended){if(has("esri-2d-update-debug")&&console.debug(`Tile[${e}] PresentStrategy.update: Promoting ended tile.`),i.decluttering){d.add(e),n.add(e);continue}n.add(e),s=!1}if(this._addPresentingParent(d,n,r,t,s)){has("esri-2d-update-debug")&&console.debug(`Tile[${e}] PresentStrategy.update: Found parent placeholder.`);continue}this._addPresentingChildren(d,n,r,t,s)?has("esri-2d-update-debug")&&console.debug(`Tile[${e}] PresentStrategy.update: Found child placeholder.`):i&&(has("esri-2d-update-debug")&&console.debug(`Tile[${e}] PresentStrategy.update: Promoting tile. No placeholder found.`),d.add(e),n.add(e))}this._updateTiles(t,n,d)}_updateTargetDefer(e,t){const d=Array.from(e.values()),n=new Set,r=new Set;if(d.every(e=>t.get(e.id)?.presenting)){for(const e of d)n.add(e.id),r.add(e.id);return this._updateTiles(t,r,n)}if(d.every(e=>t.get(e.id)?.decluttering))for(const i of d)r.add(i.id),n.add(i.id);if(d.every(e=>t.get(e.id)?.ended))for(const i of d)r.add(i.id);for(const i of d){const e=i.id,d=t.get(e);if(this._addPresentingParent(n,r,i,t,!0))continue;this._addPresentingChildren(n,r,i,t,!0)||d&&(n.add(e),r.add(e))}this._updateTiles(t,r,n)}_updateTiles(e,t,d){for(const n of e.values()){const e=n.decluttering,r=n.presenting;n.decluttering=t.has(n.id),e!==n.decluttering&&n.requestDeclutter(),n.presenting=d.has(n.id),has("esri-2d-update-debug")&&(e===n.decluttering&&r===n.presenting||console.debug(`Tile[${n.key.id}] PresentStrategy.update [decluttering: ${e}->${n.decluttering}, presenting: ${r}->${n.presenting}`))}}_addPresentingParent(e,t,d,n,r){let i=!1;for(const s of n.values())if(s.presenting&&s.key.containsChild(d)){e.add(s.key.id),r&&t.add(s.key.id),i=!0;break}return i}_addPresentingChildren(e,t,d,n,r){let i=!1;for(const s of n.values())s.presenting&&d.containsChild(s.key)&&(e.add(s.key.id),r&&t.add(s.key.id),i=!0);return i}}export{e as FeatureTilePresentStrategy};
