/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import"../../../../../../../core/has.js";import{vertexLayoutHash as t}from"./utils.js";import{DataType as e}from"../../../../../../webgl/enums.js";function s(t){switch(t){case e.BYTE:case e.UNSIGNED_BYTE:return 1;case e.SHORT:case e.UNSIGNED_SHORT:case e.HALF_FLOAT:return 2;case e.FLOAT:case e.INT:case e.UNSIGNED_INT:return 4}}function o(t){const e=[],o=[],n=[];for(const i of t){const t=s(i.type)*i.count;switch(t%2||t%4||4){case 4:e.push(i);continue;case 2:o.push(i);continue;case 1:n.push(i);continue;default:throw new Error("Found unexpected dataType byte count")}}return e.push(...o),e.push(...n),e}class n{static fromVertexSpec(t,e){const{attributes:i,optionalAttributes:r}=t;let a,c,u;const p=[];for(const s in i){if(i[s].otherSource)continue;const t=i[s];"position"===t.pack?a={...t,name:s,offset:0}:"id"===t.pack?c={...t,name:s,offset:4}:"bitset"===s?u={...t,name:s,offset:7}:p.push({...t,name:s})}for(const s in r)if(!0===e[s]){const t=r[s];p.push({...t,name:s})}const h=o(p),f=[];let m=8,b=1;for(const o of h)f.push({...o,offset:m}),m+=s(o.type)*o.count,o.packAlternating&&(b=Math.max(o.packAlternating.count,b));const d=Uint32Array.BYTES_PER_ELEMENT,_=m%d;return new n(a,c,u,f,m+(_?d-_:0),b)}constructor(t,e,s,o,n,i){this.position=t,this.id=e,this.bitset=s,this.standardAttributes=o,this.stride=n,this.packVertexCount=i,o.push(s),this._attributes=[t,e,s,...o]}get attributeLayout(){if(!this._attributeLayout){const e=t(this._attributes),s=this._attributes.map(t=>({name:t.name,count:t.count,offset:t.offset,type:t.type,packPrecisionFactor:t.packPrecisionFactor,normalized:t.normalized??!1}));this._attributeLayout={attributes:s,hash:e,stride:this.stride}}return this._attributeLayout}}export{n as AlignedVertexSpec,o as sortByAlignment};
