/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{remove as t}from"../../../../core/arrayUtils.js";import{makeHandle as i}from"../../../../core/handleUtils.js";import{getObjectId as e}from"../../../../layers/graphics/dehydratedFeatures.js";class s{constructor(t){this._graphicsCore=t,this._idToState=new Map,this._states=new Set;const i=t.owner.layer?.objectIdField;i?(this._getGraphicId=t=>e(t,i),this._getGraphics3DGraphicById=t=>this._graphicsCore.getGraphics3DGraphicByObjectId(t)):(this._getGraphicId=t=>t.uid,this._getGraphics3DGraphicById=t=>this._graphicsCore.getGraphics3DGraphicById(t))}destroy(){this._idToState.clear(),this._states.forEach((t,i)=>this.remove(i))}add(t){const e=i(()=>this.remove(t));if(this._states.has(t))return e;const s=this._getGraphicId(t.graphic),r=this._getGraphics3DGraphicById(s);this._states.has(t)||this._states.add(t);return this._ensureStateList(s).push(t),t.displaying=a(r),t.isDraped=r?.isDraped??!1,t.tracking=!0,null!=r&&t.emit("changed"),e}remove(i){if(this._states.has(i)){if(this._idToState.size){const e=this._getGraphicId(i.graphic),s=this._idToState.get(e);s&&(t(s,i),0===s.length&&this._idToState.delete(e))}this._states.delete(i),i.tracking=!1,i.displaying=!1}}addGraphic(t){this._forEachState(t.graphic,i=>{i.displaying=a(t),i.isDraped=t.isDraped,i.emit("changed")})}removeGraphic(t){this._forEachState(t.graphic,t=>{t.displaying=!1,t.isDraped=!1})}updateGraphicGeometry(t){this._forEachState(t.graphic,t=>t.emit("changed"))}updateGraphicVisibility(t){this._forEachState(t.graphic,i=>i.displaying=a(t))}updateGraphicError(t,i){this._forEachState(t,t=>t.error=i)}allGraphicsDeleted(){this._states.forEach(t=>t.displaying=!1)}_ensureStateList(t){const i=this._idToState.get(t);if(i)return i;const e=new Array;return this._idToState.set(t,e),e}_forEachState(t,i){if(0===this._states.size||0===this._idToState.size)return;const e=this._getGraphicId(t),s=this._idToState.get(e);null!=s&&s.forEach(i)}}function a(t){return t?.isVisible(1,8)??!1}export{s as GraphicStateTracking};
