/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../chunks/_rollupPluginBabelHelpers","./DirtyMap","./DisplayRecordStore","./WGLBuffers","./WGLTile"],(function(t,e,i,s,a,l){"use strict";let d=function(t){function l(){var e;return(e=t.apply(this,arguments)||this)._data=null,e._displayList=null,e._lastCommitTime=0,e._hasData=!1,e._invalidated=!1,e._wglBuffers=null,e._dirtyMap=new i,e}e._inheritsLoose(l,t);var d=l.prototype;return d.destroy=function(){t.prototype.destroy.call(this),this.clear()},d.getGeometry=function(t){return this._wglBuffers&&this._wglBuffers.has(t)?this._wglBuffers.get(t):null},d.getDisplayList=function(){return this._displayList},d.patch=function(t){if(!0===t.clear)return this.clear(),void(this._hasData=!1);const e=t.addOrUpdate,a=t.remove;!this._data&&e&&e.tileDisplayData.displayObjects.length?(e.tileDisplayData.computeDisplayList(),this._dirtyMap=new i,this._dispRecStore=s.fromTileData(e,this._dirtyMap),this._data=e,this._dirtyMap.markAllDirty(),this._hasData=!0,t.end&&this.ready()):this._data&&(e&&e.tileDisplayData.displayObjects.length||a.length)?this._doPatchData(t):t.end&&this.ready(),t.end&&!this._data&&this.clear(),this.requestRender(),this.emit("change")},d.commit=function(t){t.time&&t.time===this._lastCommitTime||(this._lastCommitTime=t.time,this.visible&&this._data&&(this._wglBuffers||(this._wglBuffers=new a(t.context)),(this._dirtyMap.hasDirty()||this._invalidated)&&(this._invalidated=!1,this._wglBuffers.upload(this._data.tileBufferData,this._dirtyMap),this._displayList=this._data.tileDisplayData.displayList.clone(),this._displayObjects=this._data.tileDisplayData.displayObjects.slice(),this._dirtyMap.markAllClean())))},d.clear=function(){this._data=null,this._displayList=null,this._dispRecStore=null,this._wglBuffers&&(this._wglBuffers.dispose(),this._wglBuffers=null)},d._doPatchData=function(t){this._invalidated=!0,this._patchData(t)||(this._dirtyMap.markAllDirty(),this._data.reshuffle(),this._dispRecStore=s.fromTileData(this._data,this._dirtyMap)),this.requestRender()},d._patchData=function(t){let e=!0;const i=t.addOrUpdate&&t.addOrUpdate.tileDisplayData&&t.addOrUpdate.tileDisplayData.displayObjects||[],s=(t.remove||[]).slice();for(const a of i)null!=a.insertAfter&&s.push(a.id);for(const a of s){const t=this._data.tileDisplayData.displayObjectRegistry.get(a);if(t){this._data.tileDisplayData.displayList.removeFromList(t.displayRecords);for(const i of t.displayRecords)this._dispRecStore.delete(i);this._data.tileDisplayData.displayObjectRegistry.delete(a);const e=this._data.tileDisplayData.displayObjects.indexOf(t);this._data.tileDisplayData.displayObjects.splice(e,1)}}for(const a of i){let i,s=this._data.tileDisplayData.displayObjectRegistry.get(a.id);if(s){const t=s.displayRecords;s.set(a),s.displayRecords=t;const e=s.displayRecords.length;for(let i=0;i<e;++i){const t=s.displayRecords[i],e=a.displayRecords[i];(i>=a.displayRecords.length||t.geometryType!==e.geometryType||t.symbolLevel!==e.symbolLevel||t.zOrder!==e.zOrder||t.materialKey!==e.materialKey)&&(this._dispRecStore.delete(s.displayRecords[i]),i<a.displayRecords.length&&(s.displayRecords[i]=void 0))}s.displayRecords.length=a.displayRecords.length}else{let t;s=a.copy(),s.displayRecords=[],this._data.tileDisplayData.displayObjectRegistry.set(a.id,s);const e=this._data.tileDisplayData.displayObjects;if(null!=s.insertAfter)if(i={},s.insertAfter>=0){const i=this._data.tileDisplayData.displayObjectRegistry.get(s.insertAfter);i?(t=e.indexOf(i)+1,t<e.length?e.splice(t,0,s):(e.push(s),t=e.length)):(e.push(s),t=e.length)}else e.unshift(s),t=0;else e.push(s),t=e.length;if(i){const s=a.displayRecords.length>0?1:0;let l=0;for(let a=t-1;a>=0&&l<s;--a)for(let t=e[a].displayRecords.length-1;t>=0&&l<s;--t){const s=e[a].displayRecords[t],d=this._data.tileDisplayData.displayList.getDPInfoType();i[d]||(i[d]=s,++l)}}}const l=a.displayRecords.length;for(let d=0;d<l;++d){const l=a.displayRecords[d];let r=s.displayRecords[d];r?(r.meshData=l.meshData,r.materialKey=l.materialKey):(r=l.copy(),r.vertexFrom=void 0,r.indexFrom=void 0,s.displayRecords[d]=r);const o=l.geometryType,p=this._data.tileDisplayData.displayList.getDPInfoType(),n=t.addOrUpdate.tileBufferData.geometries[o],h=n.vertexBuffer,y=n.indexBuffer;let c;i&&(c=i[p]?this._data.tileDisplayData.displayList.splitAfter(i[p]):-1),e=this._dispRecStore.setMeshData(r,l,h,y,c)&&e,i&&null!=r.indexFrom&&null!=r.indexFrom&&(i[p]=r)}}return e},e._createClass(l,[{key:"hasData",get:function(){return!!this._hasData}},{key:"displayObjects",get:function(){var t;return null!=(t=this._displayObjects)?t:[]}}]),l}(l.WGLTile);t.GraphicTile=d,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
