/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../../../../../core/Error.js";import t from"../../../../../../core/pbf.js";import{create as r}from"../../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{BBox as s}from"../../../../../../core/libs/rbush/PooledRBush.js";import{fromValues as n}from"../../../../../../geometry/support/aaBoundingBox.js";import{normalizeTransform as o}from"../../../../../../geometry/support/quantizationUtils.js";import i from"../../../../../../layers/graphics/OptimizedGeometry.js";import a from"../../../../../../layers/support/FieldsIndex.js";import{parseField as d,parseTransform as c}from"../../../../../../rest/query/operations/pbfFeatureServiceParser.js";class u{constructor(e){this._reader=new t(new Uint8Array(e),new DataView(e)),this._index=g(this._reader)}get featureCount(){return this._index.featureIndices.length}get exceededTransferLimit(){return this._index.exceededTransferLimit}get usedMemory(){return this._reader.usedMemory}getObjectId(e){return this.getAttribute(e,this._index.objectIdFieldName)}getAttribute(e,t){const{_index:{fieldsIndex:r,attributeIndices:s}}=this,n=r.get(t)?.index;if(null==n)return;const o=s[e*r.fields.length+n],i=this._reader;return i.move(o),m(i)}getAttributeAsTimestamp(e,t){const r=this.getAttribute(e,t);return"string"==typeof r?new Date(r).getTime():"number"==typeof r||null==r?r:null}getAttributes(e){const{_index:{fieldsIndex:t,attributeIndices:r}}=this,s=e*t.fields.length,n=this._reader,o={};for(const i of t.fields){const e=r[s+i.index];n.move(e),o[i.name]=m(n)}return o}getCoordinates(e,t,r=0){const s=this._reader,{transform:n,featureIndices:o}=this._index,{scale:i,translate:a}=n;s.move(o[e]),this._readCoordinates(i,a,t,r)}getOptimizedGeometry(e){const t=r();return this.getCoordinates(e,t),new i([],t,!0,!1)}getCentroid(e,{hasZ:t,hasM:r}){this.getCoordinates(e,p);const[s,n,o]=p,a=[s,n];return t&&(a[3]=o),r&&(a[t?4:3]=0),new i([],a,t,r)}getBounds(e){this.getCoordinates(e,p);const[t,r]=p,n=new s;return n.minX=t,n.minY=r,n.maxX=t,n.maxY=r,n}getBoundingBox(e){this.getCoordinates(e,p);const[t,r,s]=p;return n(t,r,s,t,r,s)}getObjectIdsArray(e,t=this._allFeatureIndices(),r=0){const s=this._reader,{objectIdFieldName:n,attributeIndices:o,fieldsIndex:i}=this._index,a=i.get(n).index,d=i.fields.length;for(const c of t){const t=o[c*d+a];s.move(t),e[r++]=m(s)}return r}getCoordinatesArray(e,t=this._allFeatureIndices(),r=0){const s=this._reader,{transform:n,featureIndices:o}=this._index,{scale:i,translate:a}=n;for(const d of t){const t=o[d];s.move(t),r=this._readCoordinates(i,a,e,r)}return r}*objectIds(e=this._allFeatureIndices()){const t=this._reader,{objectIdFieldName:r,attributeIndices:s,fieldsIndex:n}=this._index,o=n.get(r).index,i=n.fields.length;for(const a of e){const e=s[a*i+o];t.move(e),yield m(t)}}*_allFeatureIndices(){const{featureCount:e}=this;for(let t=0;t<e;++t)yield t}_readCoordinates([e,t,r],[s,n,o],i,a){const d=2,c=3,u=this._reader,g=u.getLength(),l=u.pos()+g;for(;u.pos()<l&&u.next();)switch(u.tag()){case d:{const d=u.getLength(),g=u.pos()+d;for(;u.pos()<g&&u.next();)if(u.tag()===c)u.getUInt32(),i[a++]=s+e*u.getSInt64(),i[a++]=n+t*u.getSInt64(),i[a++]=o+r*u.getSInt64();else u.skip();break}default:u.skip()}return a}}function g(e){const t=2;for(;e.next();){if(e.tag()===t)return l(e.getMessage());e.skip()}h()}function l(e){const t=1;for(;e.next();){if(e.tag()===t)return f(e.getMessage());e.skip()}h()}function f(e){const t=9,r=12,s=13,n=15,i=7,u=0,g=10,l=1,f=1;let m,p,x=!1,I=!1,b=0;const _=new Array,w=new Array,y=new Array;for(;e.next();)switch(e.tag()){case f:p=e.getString();break;case i:e.getEnum()!==u&&h();break;case t:x=e.getBool()??!1;break;case r:m=o(e.processMessage(c));break;case s:{const t=e.processMessage(d);t.index=b++,_.push(t);break}case n:{w.push(e.pos());const t=e.getUInt32(),r=e.pos()+t;for(;e.pos()<r&&e.next();)if(e.tag()===l)y.push(e.pos()),e.skip();else e.skip();break}case g:I=e.getBool()??!1;break;default:e.skip()}const j=new a(_);return null!=m&&I&&null!=p&&j.has(p)||h(),{transform:m,exceededTransferLimit:x,fieldsIndex:j,objectIdFieldName:p,featureIndices:w,attributeIndices:y}}function h(){const t=new e("pbf-parsing-failed","Error while parsing PBF",new Error);throw console.error(t),t}function m(e){const t=1,r=2,s=3,n=4,o=5,i=6,a=7,d=8,c=9,u=e.getLength(),g=e.pos()+u;for(;e.pos()<g&&e.next();)switch(e.tag()){case t:return e.getString();case r:return e.getFloat();case s:return e.getDouble();case n:return e.getSInt32();case o:return e.getUInt32();case i:return e.getInt64();case a:return e.getUInt64();case d:return e.getSInt64();case c:return e.getBool();default:return e.skip(),null}return null}const p=r();export{u as PBFPointFeatureSetView};
