/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../../../core/Accessor.js";import"../../../../core/has.js";import{NestedMap as r}from"../../../../core/NestedMap.js";import{nullUid as o}from"../../../../core/uid.js";import{property as s,subclass as d}from"../../../../core/accessorSupport/decorators.js";import{assert as i}from"./Util.js";class n{constructor(e,t,r,o){this.operation=e,this.geometry=t,this.states=r,this.sync=o}}let c=class extends t{constructor(e){super(e),this._residentGeomRecords=new r,this._dirtyGeomRecords=new r,this._dirtyRecordCount=0}get dirty(){return this._dirtyRecordCount>0}commitLayer(e,t){const r=this._dirtyGeomRecords.getInner(e),o=this.model.getLayer(e);if(!r||!o)return;let s=0;r.forEach((r,d)=>{const n=this._ensureGeomRecord(e,d);this._dirtyGeomRecords.delete(e,d),s+=r.size,r.forEach(({geometry:e,operation:r,states:s},c)=>{let a=!1;if(1===r){const r=n.get(c);if(r){if(4&s){const t=o.getObject(d);this.model.updateRenderGeometryTransformation(t,e,r)&&(a=!0)}a||t.updates.push({renderGeometry:r,updateType:s})}else i(!1,"ModelDirtySet.commitLayer: invalid update")}if(2===r||a){const e=n.get(c);e?(t.removes.push(e),n.delete(c)):2===r&&i(!1,"ModelDirtySet.commitLayer: invalid remove")}if(0===r||a){const r=o.getObject(d);if(null!=r){const o=this.model.getRenderGeometry(r,e);t.adds.push(o),n.set(c,o)}}}),0===n.size&&this._residentGeomRecords.delete(e,d)}),this._dirtyRecordCount-=s}commitSyncUpdates(e,t){const r=this._dirtyGeomRecords.getInner(e),o=this.model.getLayer(e);r&&o&&r.forEach((r,s)=>{const d=this._ensureGeomRecord(e,s);r.forEach(({geometry:e,operation:r,states:n,sync:c},a)=>{let m=!1;if(1===r&&c){const r=d.get(a);if(r){if(4&n){const t=o.getObject(s);this.model.updateRenderGeometryTransformation(t,e,r)&&(m=!0)}m||t.updates.push({renderGeometry:r,updateType:n})}else i(!1,"ModelDirtySet.commitSyncUpdates: invalid update")}})})}_objectStateChanged(e,t){for(const r of t.geometries)this._updateOrCreateDirtyRecord(t,r,1,e)}visibilityChanged(e){this._objectStateChanged(1,e)}highlightChanged(e){this._objectStateChanged(8,e)}occlusionChanged(e){this._objectStateChanged(16,e)}attributesChanged({object:e,geometry:t,sync:r}){this._updateOrCreateDirtyRecord(e,t,1,2,r)}layerAdded(e){e.objects.forEach(e=>this.layerObjectAdded(e))}layerRemoved(e){e.objects.forEach(e=>this.layerObjectRemoved(e))}layerObjectAdded(e){for(const t of e.geometries)this._geometryAdded(e,t)}layerObjectRemoved(e){for(const t of e.geometries)this._geometryRemoved(e,t)}layerObjectsAdded(e){for(const t of e)this.layerObjectAdded(t)}layerObjectsRemoved(e){for(const t of e)this.layerObjectRemoved(t)}transformationChanged(e){const t=this._getLayerId(e),r=e.id;this._ensureGeomRecord(t,r).forEach(({geometry:t})=>this._updateOrCreateDirtyRecord(e,t,1,4))}shaderTransformationChanged(e){const t=this._getLayerId(e),r=e.id;this._ensureGeomRecord(t,r).forEach(t=>t.objectShaderTransformationChanged(e.shaderTransformation))}geometryAdded(e){this._geometryAdded(e.object,e.geometry)}_geometryAdded(e,t){this._updateOrCreateDirtyRecord(e,t,0)}geometryRemoved(e){this._geometryRemoved(e.object,e.geometry)}_geometryRemoved(e,t){this._updateOrCreateDirtyRecord(e,t,2)}_updateOrCreateDirtyRecord(e,t,r,o=0,s=!1){const d=this._getLayerId(e),c=e.id,a=t.id,m=this._ensureDirtyRecord(d,c),y=m.get(a);if(y){const e=y.operation;2===e&&0===r&&0!==y.states?y.operation=1:2===e&&0===r||0===e&&2===r?(m.delete(a),this._dirtyRecordCount--):1!==e||2!==r&&1!==r?(i((2===e||0===e)&&1===r,"ModelDirtySet.objectGeometryAdded: inconsistent state"),y.states|=o):(y.operation=r,y.states|=o),y.sync=y.sync||s}else m.set(a,new n(r,t,o,s)),this._dirtyRecordCount++}_ensureGeomRecord(e,t){let r=this._residentGeomRecords.get(e,t);return r||(r=new Map,this._residentGeomRecords.set(e,t,r)),r}assertLayerClean(e){i(null==this._dirtyGeomRecords.getInner(e),"Dirty geometry records for removed layer")}_ensureDirtyRecord(e,t){const r=this.model.getLayer(e);i(null!=r,"Updating geometry record for an unregistered layer");let o=this._dirtyGeomRecords.get(e,t);return o||(o=new Map,this._dirtyGeomRecords.set(e,t,o)),o}_getLayerId(e){return e.layer?.id??o}formatDebugInfo(){const e=["ADD","UPD",void 0,"REM"];let t="";return this._dirtyGeomRecords.forAll((r,o,s)=>{t.length>0&&(t+="\n"),t+=o+"."+s;const d=[];r.forEach(e=>{const t=e.operation;d[t]||(d[t]=[]),d[t].push(e.geometry.id)});for(let i=0;i<d.length;i++)if(d[i]){t+=" "+e[i-1]+": ";for(let e=0;e<d[i].length;e++)t+=d[i][e]+", "}}),t}get test(){}};e([s({constructOnly:!0})],c.prototype,"model",void 0),e([s()],c.prototype,"_dirtyRecordCount",void 0),c=e([d("esri.views.3d.webgl-engine.lib.ModelDirtySet")],c);const a=c;export{a as default};
