/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../../../core/has","./FreeList","./Utils"],(function(e,t,r){"use strict";const i=["FILL","LINE","MARKER","TEXT","LABEL"];function n(e){const t=e.getStrides(),r={};for(let n=0;n<t.length;n++)r[i[n]]=t[n];return r}const o=.5;return function(){function e(e,i,n,o){this._strides=e,this._displayList=i,this._freeListsAndStorage={},this._dirtyMap=null,this._dirtyMap=n;for(const s in e){this._freeListsAndStorage[s]={vtxFreeList:o?new t.FreeList(o):null,idxFreeList:o?new t.FreeList(o):null,vertexBuffers:{},indexBuffer:o?new Uint32Array(o):null};for(const t in e[s])this._freeListsAndStorage[s].vertexBuffers[t]={data:o?r.allocateTypedArrayBuffer(o,e[s][t]):null,stride:e[s][t]}}}e.fromTileData=function(o,s){const a=n(o),d=[0,0,0,0,0],u=[0,0,0,0,0],f=o.tileDisplayData.displayObjects;for(const e of f)for(const t of e.displayRecords)d[t.geometryType]=Math.max(d[t.geometryType],t.vertexFrom+t.vertexCount),u[t.geometryType]=Math.max(u[t.geometryType],t.indexFrom+t.indexCount);const x=new e(a,o.tileDisplayData.displayList,s,null);for(let e=0;e<o.tileBufferData.geometries.length;++e){const n=d[e],s=u[e],a=o.tileBufferData.geometries[e],f=i[e],c=x._storageFor(f),l=o.tileBufferData.geometries[e].indexBuffer;let v;c.indexBuffer=l,c.idxFreeList=new t.FreeList(l.length),c.idxFreeList.allocate(s);for(const t in a.vertexBuffer){const i=o.tileBufferData.geometries[e].vertexBuffer[t];c.vertexBuffers[t].data=i.data,c.vertexBuffers[t].stride=i.stride;const n=r.strideToPackingFactor(i.stride),s=i.data.length*n/i.stride;v||(v=s)}c.vtxFreeList=new t.FreeList(v),c.vtxFreeList.allocate(n)}return x};var s=e.prototype;return s.delete=function(e){const t=i[e.geometryType];this._freeVertices(t,e.vertexFrom,e.vertexCount),this._freeIndices(t,e.indexFrom,e.indexCount),this._displayList.removeFromList(e),e.vertexFrom=void 0,e.indexFrom=void 0},s.setMeshData=function(e,t,n,o,s){const a=i[e.geometryType];let d,u;e.meshData=null,void 0===e.vertexFrom?(u=t.vertexCount,d=this._allocateVertices(a,u)):t.vertexCount>e.vertexCount?(this._freeVertices(a,e.vertexFrom,e.vertexCount),u=t.vertexCount,d=this._allocateVertices(a,u)):t.vertexCount===e.vertexCount?(d=e.vertexFrom,u=e.vertexCount):(this._freeVertices(a,e.vertexFrom+t.vertexCount,e.vertexCount-t.vertexCount),d=e.vertexFrom,u=t.vertexCount);let f,x,c,l=!0;if(void 0===e.indexFrom?(f=s,c=t.indexCount,x=this._allocateIndices(a,c)):t.indexCount>e.indexCount?(f=this._displayList.removeFromList(e),this._freeIndices(a,e.indexFrom,e.indexCount),c=t.indexCount,x=this._allocateIndices(a,c)):t.indexCount===e.indexCount?(l=!1,x=e.indexFrom,c=e.indexCount):(f=this._displayList.removeFromList(e),this._freeIndices(a,e.indexFrom+t.indexCount,e.indexCount-t.indexCount),x=e.indexFrom,c=t.indexCount),-1!==d&&-1!==x){const i=this._storageFor(a);if(r.copyMeshData(d,x,i.vertexBuffers,i.indexBuffer,t,n,o),e.vertexFrom=d,e.indexFrom=x,e.vertexCount=t.vertexCount,e.indexCount=t.indexCount,this._dirtyMap){this._dirtyMap.markDirtyIndices(e.geometryType,e.indexFrom,e.indexCount);for(const t in n)this._dirtyMap.markDirtyVertices(e.geometryType,t,e.vertexFrom,e.vertexCount)}return l&&this._displayList.addToList(e,f),!0}return-1!==d&&this._freeVertices(a,d,u),-1!==x&&this._freeIndices(a,x,c),e.setMeshDataFromBuffers(t,n,o),e.vertexFrom=void 0,e.vertexCount=0,e.indexFrom=void 0,e.indexCount=0,!1},s.tryAddMeshData=function(e,t){const n=t.vertexBuffer,o=t.indexBuffer,s=i[e.geometryType],a=this._allocateVertices(s,e.vertexCount);if(-1===a)return this._freeVertices(s,a,e.vertexCount),!1;const d=this._allocateIndices(s,e.indexCount);if(-1===d)return this._freeVertices(s,a,e.vertexCount),this._freeIndices(s,d,e.indexCount),!1;const u=this._storageFor(s);if(r.copyMeshData(a,d,u.vertexBuffers,u.indexBuffer,e,n,o),e.vertexFrom=a,e.indexFrom=d,this._dirtyMap){this._dirtyMap.markDirtyIndices(e.geometryType,e.indexFrom,e.indexCount);for(const t in n)this._dirtyMap.markDirtyVertices(e.geometryType,t,a,e.vertexCount)}return this._displayList.addToList(e),!0},s._allocateVertices=function(e,t){const r=this._storageFor(e),i=r.vtxFreeList.allocate(t);if(-1===i)return-1;return r.vtxFreeList.fragmentation>o?-1:i},s._freeVertices=function(e,t,r){this._storageFor(e).vtxFreeList.free(t,r)},s._freeIndices=function(e,t,r){this._storageFor(e).idxFreeList.free(t,r)},s._allocateIndices=function(e,t){const r=this._storageFor(e),i=r.idxFreeList.allocate(t);if(-1===i)return-1;return r.idxFreeList.fragmentation>o?-1:i},s._storageFor=function(e){return this._freeListsAndStorage[e]},s._stridesFor=function(e,t){return this._strides[e][t]},e}()}));
