/*
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","../../core/Logger","../../core/maybe","./enums","./Util"],(function(e,t,i,n,r,s){"use strict";const o=i.getLogger("esri.views.webgl.VertexArrayObject");let u=function(){function e(e,t,i,n,s=null){this._context=e,this._locations=t,this._layout=i,this._buffers=n,this._indexBuffer=s,this._glName=null,this._initialized=!1,e.instanceCounter.increment(r.ResourceType.VertexArrayObject,this)}var i=e.prototype;return i.dispose=function(e=!0){if(!this._context)return void((this._glName||e&&Object.getOwnPropertyNames(this._buffers).length>0)&&o.warn("Leaked WebGL VAO"));if(this._glName){var t,i;const e=null==(t=this._context)||null==(i=t.capabilities)?void 0:i.vao;e?(e.deleteVertexArray(this._glName),this._glName=null):o.warn("Leaked WebGL VAO")}if(this._context.getBoundVAO()===this&&this._context.bindVAO(null),e){for(const e in this._buffers)this._buffers[e].dispose(),delete this._buffers[e];this._indexBuffer=n.disposeMaybe(this._indexBuffer)}this._context.instanceCounter.decrement(r.ResourceType.VertexArrayObject,this),this._context=null},i.initialize=function(){if(this._initialized)return;const e=this._context.capabilities.vao;if(e){const t=e.createVertexArray();e.bindVertexArray(t),this._bindLayout(),e.bindVertexArray(null),this._glName=t}this._initialized=!0},i.bind=function(){this.initialize();const e=this._context.capabilities.vao;e?e.bindVertexArray(this.glName):(this._context.bindVAO(null),this._bindLayout())},i._bindLayout=function(){const{_buffers:e,_layout:t,_indexBuffer:i}=this;e||o.error("Vertex buffer dictionary is empty!");const r=this._context.gl;for(const n in e){const i=e[n];i||o.error("Vertex buffer is uninitialized!");const r=t[n];r||o.error("Vertex element descriptor is empty!"),s.bindVertexBufferLayout(this._context,this._locations,i,r)}if(n.isSome(i)){!!this._context.capabilities.vao?r.bindBuffer(r.ELEMENT_ARRAY_BUFFER,i.glName):this._context.bindBuffer(i)}},i.unbind=function(){this.initialize();const e=this._context.capabilities.vao;e?e.bindVertexArray(null):this._unbindLayout()},i._unbindLayout=function(){const{_buffers:e,_layout:t}=this;e||o.error("Vertex buffer dictionary is empty!");for(const i in e){const n=e[i];n||o.error("Vertex buffer is uninitialized!");const r=t[i];s.unbindVertexBufferLayout(this._context,this._locations,n,r)}n.isSome(this._indexBuffer)&&this._context.unbindBuffer(this._indexBuffer.bufferType)},t._createClass(e,[{key:"glName",get:function(){return this._glName}},{key:"context",get:function(){return this._context}},{key:"vertexBuffers",get:function(){return this._buffers}},{key:"indexBuffer",get:function(){return this._indexBuffer}},{key:"size",get:function(){return Object.keys(this._buffers).reduce(((e,t)=>e+this._buffers[t].size),n.isSome(this._indexBuffer)?this._indexBuffer.size:0)}},{key:"layout",get:function(){return this._layout}},{key:"locations",get:function(){return this._locations}}]),e}();e.VertexArrayObject=u,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
