/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../../core/maybe","../FeatureDisplayList","./Buffer","./DisplayRecordReader","../../../../webgl/VertexArrayObject"],(function(e,t,r,i,s,n){"use strict";const f=0,o=1;let d=function(){function e(e,t){this._vaos=new Map,this._indicesInvalid=!1,this.geometryType=e}var d=e.prototype;return d.destroy=function(){for(const[e,r]of this._vaos)t.isSome(r)&&r.dispose(!1);this._indexBuffer=t.destroyMaybe(this._indexBuffer),this._vertexBuffer=t.destroyMaybe(this._vertexBuffer)},d.insert=function(e,r,n){if(!e.records.byteLength)return;const f=e.stride;if(this._vertexBuffer&&this._indexBuffer){const i=e.indices.byteLength/4,n=e.vertices.byteLength/f;this._indexBuffer.ensure(i),this._vertexBuffer.ensure(n);const{vertices:o,indices:d}=e,u=s.DisplayRecordReader.from(e.records),h=this._vertexBuffer.insert(o,0,o.byteLength/f,0),c=this._indexBuffer.insert(d,0,d.byteLength/4,h);if(u.forEach((e=>{e.indexFrom+=c,e.vertexFrom+=h})),t.unwrapOrThrow(this._records,"Expected records to be defined").link(u),r)this._indicesInvalid=!0;else if(this._displayList){const e=u.getCursor();for(;e.next();)this._displayList.addRecord(e)}}else{const t=e.indices.byteLength/4,n=e.vertices.byteLength/f,o=f/Uint32Array.BYTES_PER_ELEMENT;this._records=s.DisplayRecordReader.from(e.records),this._indexBuffer=new i.Buffer("index",t,1),this._vertexBuffer=new i.Buffer("vertex",n,o),this._indexBuffer.insert(e.indices,0,e.indices.byteLength/4,0),this._vertexBuffer.insert(e.vertices,0,e.vertices.byteLength/f,0),r&&(this._indicesInvalid=!0)}},d.remove=function(e){if(!t.isNone(this._records))for(const t of e){const e=this._records.getCursor();if(!e.lookup(t))continue;const r=e.indexFrom,i=e.vertexFrom;let s=e.indexCount,n=e.vertexCount;for(;e.next()&&e.id===t;)s+=e.indexCount,n+=e.vertexCount;this._indexBuffer.free(r,s),this._vertexBuffer.free(i,n,!0),this._records.delete(t)}},d.getVAO=function(e,r,i,s){if(!this._vertexBuffer||!this._indexBuffer||t.isNone(this._records)||!this._vertexBuffer.bufferSize)return null;const d=s?o:f;let u=this._vaos.get(d);(this._vertexBuffer.invalidated||this._indexBuffer.invalidated||s&&this._indexBuffer.invalidatedComputeBuffer)&&(t.applySome(u,(e=>e.dispose(!1))),u=null),this._vertexBuffer.upload(),this._indexBuffer.upload();const h=this._indexBuffer.getGPUBuffer(e,1===d),c=this._vertexBuffer.getGPUBuffer(e);return u||(u=new n.VertexArrayObject(e,i,r,{geometry:c},h),this._vaos.set(d,u)),u},d.forEachCommand=function(e){if(!t.isNone(this._records)){if(this._sortIndices(this._records),!this._displayList){const e=this._cursorIndexOrder;this._displayList=r.FeatureDisplayList.from(this,this.geometryType,this._records.getCursor(),e)}this._displayList.forEach(e)}},d._sortIndices=function(e){const t=!!this._indexBuffer.bufferSize;if(!this._indicesInvalid)return;this._indicesInvalid=!1;let r=0;const i=e.getCursor(),s=[],n=[],f=[];for(;i.next();)n.push(i.index),f.push(i.sortKey),s.push(i.id);n.sort(((e,t)=>{const r=f[t],i=f[e];return i===r?s[t]-s[e]:r-i}));const o=e.getCursor(),d=t?this._indexBuffer.getCPUBuffer():this._vertexBuffer.getCPUBuffer();for(const u of n){if(!o.seekIndex(u))throw new Error("Expected to find index");if(t){const{indexFrom:e,indexCount:t}=o;o.indexFrom=r;for(let i=0;i<t;i++)this._indexBuffer.set(r++,d.array[e+i])}else{const{vertexFrom:e,vertexCount:t}=o,i=this._vertexBuffer.strideInt,s=e*i,n=s+t*i;o.vertexFrom=r/i;for(let f=s;f<n;f++)this._vertexBuffer.set(r++,d.array[f])}}this._cursorIndexOrder=n,this._displayList=null},e}();e.Geometry=d,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
