/*
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/promiseUtils","./types","../../../../../views/webgl/enums"],(function(e,t,s,i,n){"use strict";let r=function(){function e(e,t,s,n,r){this.buffer=e,this.componentType=s,this.dataType=n,this.data=[],this.isFinalized=!1,this.accessorIndex=-1,this.accessorAttribute=null,this.accessorMin=null,this.accessorMax=null,t.bufferViews||(t.bufferViews=[]),this.index=t.bufferViews.length,this.bufferView={buffer:e.index,byteLength:-1,target:r};const a=this._getElementSize();a>=4&&r!==i.TargetBuffer.ELEMENT_ARRAY_BUFFER&&(this.bufferView.byteStride=a),t.bufferViews.push(this.bufferView),this.numComponentsForDataType=this._calculateNumComponentsForDataType()}var r=e.prototype;return r.push=function(e){const t=this.data.length;if(this.data.push(e),this.accessorIndex>=0){const s=t%this.numComponentsForDataType,i=this.accessorMin[s];this.accessorMin[s]="number"!=typeof i?e:Math.min(i,e);const n=this.accessorMax[s];this.accessorMax[s]="number"!=typeof n?e:Math.max(n,e)}},r.getByteOffset=function(){if(!this.isFinalized)throw new Error("Cannot get BufferView offset until it is finalized");return this.buffer.getByteOffset(this)},r._createTypedArray=function(e,t){switch(this.componentType){case n.DataType.BYTE:return new Int8Array(e,t);case n.DataType.FLOAT:return new Float32Array(e,t);case n.DataType.SHORT:return new Int16Array(e,t);case n.DataType.UNSIGNED_BYTE:return new Uint8Array(e,t);case n.DataType.UNSIGNED_INT:return new Uint32Array(e,t);case n.DataType.UNSIGNED_SHORT:return new Uint16Array(e,t)}},r.writeOutToBuffer=function(e,t){this._createTypedArray(e,t).set(this.data)},r.writeAsync=function(e){if(this.asyncWritePromise)throw new Error("Can't write multiple bufferView values asynchronously");return this.asyncWritePromise=e.then((e=>{const t=new Uint8Array(e);for(let s=0;s<t.length;++s)this.data.push(t[s]);delete this.asyncWritePromise})),this.asyncWritePromise},r.startAccessor=function(e){if(this.accessorIndex>=0)throw new Error("Accessor was started without ending the previous one");this.accessorIndex=this.data.length,this.accessorAttribute=e;const t=this.numComponentsForDataType;this.accessorMin=new Array(t),this.accessorMax=new Array(t)},r.endAccessor=function(){if(this.accessorIndex<0)throw new Error("An accessor was not started, but was attempted to be ended");const e=this._getElementSize(),t=this.numComponentsForDataType,s=(this.data.length-this.accessorIndex)/t;if(s%1)throw new Error("An accessor was ended with missing component values");for(let n=0;n<this.accessorMin.length;++n)"number"!=typeof this.accessorMin[n]&&(this.accessorMin[n]=0),"number"!=typeof this.accessorMax[n]&&(this.accessorMax[n]=0);const i={byteOffset:e*(this.accessorIndex/t),componentType:this.componentType,count:s,type:this.dataType,min:this.accessorMin,max:this.accessorMax,name:this.accessorAttribute};switch(this.accessorAttribute){case"TEXCOORD_0":case"TEXCOORD_1":case"COLOR_0":case"WEIGHTS_0":switch(this.componentType){case n.DataType.UNSIGNED_BYTE:case n.DataType.UNSIGNED_SHORT:i.normalized=!0}}return this.accessorIndex=-1,this.accessorAttribute=null,this.accessorMin=null,this.accessorMax=null,i},r.finalize=function(){const e=this.bufferView;return new Promise((e=>{const t=this.buffer.getViewFinalizePromises(this);this.asyncWritePromise&&t.push(this.asyncWritePromise),e(s.eachAlways(t))})).then((()=>{this.isFinalized=!0,e.byteOffset=this.getByteOffset(),e.byteLength=this.dataSize,this.finalizedPromiseResolve&&this.finalizedPromiseResolve()}))},r._getElementSize=function(){return this._sizeComponentType()*this.numComponentsForDataType},r._sizeComponentType=function(){switch(this.componentType){case n.DataType.BYTE:case n.DataType.UNSIGNED_BYTE:return 1;case n.DataType.SHORT:case n.DataType.UNSIGNED_SHORT:return 2;case n.DataType.UNSIGNED_INT:case n.DataType.FLOAT:return 4}},r._calculateNumComponentsForDataType=function(){switch(this.dataType){case i.AttributeType.SCALAR:return 1;case i.AttributeType.VEC2:return 2;case i.AttributeType.VEC3:return 3;case i.AttributeType.VEC4:case i.AttributeType.MAT2:return 4;case i.AttributeType.MAT3:return 9;case i.AttributeType.MAT4:return 16}},t._createClass(e,[{key:"dataSize",get:function(){return this.data.length*this._sizeComponentType()}},{key:"byteSize",get:function(){function e(e,t){return t*Math.ceil(e/t)}return e(this.dataSize,4)}},{key:"byteOffset",get:function(){if(!this.isFinalized)throw new Error("Cannot get BufferView offset until it is finalized");return this.buffer.getByteOffset(this)}},{key:"finalized",get:function(){return this.finalizedPromise?this.finalizedPromise:this.isFinalized?this.finalizedPromise=Promise.resolve():this.finalizedPromise=new Promise((e=>this.finalizedPromiseResolve=e))}}]),e}();e.BufferView=r,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
