/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{estimateNumberArrayMemory as e}from"../../../../../core/memoryEstimations.js";import{getFeatureIdInfoFieldNames as t}from"../../../../../layers/graphics/data/FeatureIdInfo.js";import s from"../../../../../layers/support/FieldsIndex.js";import{parseField as r,parseTransform as o}from"../../../../../rest/query/operations/pbfFeatureServiceParser.js";const n=268435455;class i{constructor(){this.hasFeatures=!1,this.exceededTransferLimit=!1,this.fieldCount=0,this.featureCount=0,this.idFieldIndices=[],this.vertexCount=0,this.offsets={attributes:new Array,geometry:new Array},this.centroid=new Array}get usedMemory(){let t=0;return t+=e(this.idFieldIndices),t+=e(this.offsets.attributes),t+=e(this.offsets.geometry),t+=e(this.centroid),this.displayIds&&(t+=e(this.displayIds)),this.groupIds&&(t+=e(this.groupIds)),t}}function a(e,a,c,f=!1){const d=9,u=12,p=13,g=15,h=e.asUnsafe(),l=h.pos(),m=new i;let y=0,I=0;const w=1,b=2,k=4,x=3;let A=null,L=!1;const F=[];for(;h.next();)switch(h.tag()){case u:A=h.processMessage(o);break;case d:if(m.exceededTransferLimit=h.getBool(),m.exceededTransferLimit){m.offsets.geometry=f?new Float64Array(8e3):new Int32Array(8e3),m.centroid=f?new Float64Array(16e3):new Int32Array(16e3);for(let e=0;e<m.centroid.length;e++)m.centroid[e]=n}break;case p:{const e=h.processMessage(r);e.index=y++,F.push(e);break}case g:{const e=h.getLength(),t=h.pos()+e;if(!m.exceededTransferLimit){const e=m.offsets.geometry,t=m.centroid;e.push(0),t.push(n),t.push(n)}!L&&m.exceededTransferLimit&&(L=!0,m.offsets.attributes=f?new Float64Array(8e3*y):new Uint32Array(8e3*y));let s=I*y;for(;h.pos()<t&&h.next();)switch(h.tag()){case w:{if(L)m.offsets.attributes[s++]=h.pos();else{m.offsets.attributes.push(h.pos())}const e=h.getLength();h.skipLen(e);break}case b:if(a){const e=h.getLength(),t=h.pos()+e;for(;h.pos()<t&&h.next();)switch(h.tag()){case x:{h.getUInt32();const e=h.getSInt64(),t=h.getSInt64();m.centroid[2*I]=e,m.centroid[2*I+1]=t;break}default:h.skip()}}else{m.offsets.geometry[I]=h.pos();const e=h.getLength();m.vertexCount+=e,h.skipLen(e)}break;case k:{const e=h.getLength(),t=h.pos()+e;for(;h.pos()<t&&h.next();)switch(h.tag()){case x:{h.getUInt32();const e=h.getSInt64(),t=h.getSInt64();m.centroid[2*I]=e,m.centroid[2*I+1]=t;break}default:h.skip()}break}default:h.skip()}I++,m.hasFeatures=!0;break}default:h.skip()}m.fields=new s(F),m.featureCount=I,m.fieldCount=y;const C=t(c);return m.idFieldIndices=Array.from(C,e=>m.fields.get(e)?.index),m.transform=A,m.displayIds=new Uint32Array(m.featureCount),m.groupIds=new Uint16Array(m.featureCount),h.move(l),m}export{i as FeatureSetHeader,a as parseHeader};
