/*
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/promiseUtils","./bufferview"],(function(e,i,t){"use strict";let f=function(){function e(e){this.gltf=e,this.bufferViews=[],this.isFinalized=!1,e.buffers||(e.buffers=[]),this.index=e.buffers.length;const i={byteLength:-1};e.buffers.push(i),this.buffer=i}var f=e.prototype;return f.addBufferView=function(e,i,f){if(this.finalizePromise)throw new Error("Cannot add buffer view after fiinalizing buffer");const r=new t.BufferView(this,this.gltf,e,i,f);return this.bufferViews.push(r),r},f.getByteOffset=function(e){let i=0;for(const t of this.bufferViews){if(t===e)return i;i+=t.byteSize}throw new Error("Given bufferView was not present in this buffer")},f.getViewFinalizePromises=function(e){const i=[];for(const t of this.bufferViews){if(e&&t===e)return i;i.push(t.finalized)}return i},f.getArrayBuffer=function(){if(!this.isFinalized)throw new Error("Cannot get ArrayBuffer from Buffer before it is finalized");const e=this._getTotalSize(),i=new ArrayBuffer(e);let t=0;for(const f of this.bufferViews)f.writeOutToBuffer(i,t),t+=f.byteSize;return i},f.finalize=function(){if(this.finalizePromise)throw new Error(`Buffer ${this.index} was already finalized`);return this.finalizePromise=new Promise((e=>{e(i.eachAlways(this.getViewFinalizePromises()))})).then((()=>{this.isFinalized=!0;const e=this.getArrayBuffer();this.buffer.byteLength=e.byteLength,this.buffer.uri=e})),this.gltf.extras.promises.push(this.finalizePromise),this.finalizePromise},f._getTotalSize=function(){let e=0;for(const i of this.bufferViews)e+=i.byteSize;return e},e}();e.Buffer=f,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
