/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{generateUID as t}from"../../../core/uid.js";import e from"../../../geometry/SpatialReference.js";import{zScale as s}from"../../../geometry/support/zscale.js";import{DehydratedFeatureSetClass as i,DehydratedFeatureClass as r,removeLargeStringAttributes as a}from"../../../layers/graphics/dehydratedFeatures.js";import n from"../../../layers/support/Field.js";import{makeVertexTransformer as h,makeAttributesClass as o}from"./pbfFeatureSetUtils.js";class p{constructor(t){this._options=t,this.featureSet=new i,this.usesShortGeometryTypes=!0,this._queryGeometryType=null,this._queryGeometry=null,this.parseOnly=null,this.missingAttributes=[],this._transformInPlace=null,this._attributesClass=null,this._previousX=0,this._previousY=0,this._point=null,this._lengths=null,this._vertex=[],this._part=null,this._parts=null,this._i=0,this._finishVertex=p._finishVertexNonPoint}queryGeometryType(){return this._queryGeometryType}queryGeometry(){return this._queryGeometry}idField(t){"string"==typeof t&&(this.featureSet.objectIdFieldName=t)}finish(){this._options.applyTransform&&(this.featureSet.transform=null);let{spatialReference:t,features:s,fields:i}=this.featureSet;if(!t||t instanceof e||(this.featureSet.spatialReference=t=e.fromJSON(t)),t)for(const{geometry:e}of s)e&&(e.spatialReference=t);i.forEach((t,e)=>{t instanceof n||(i[e]=n.fromJSON(t))})}_setVertexTransformer(){const{sourceSpatialReference:t}=this._options,{spatialReference:e,hasZ:i}=this.featureSet,r=i?s(t,e):1;this._transformInPlace??=h(this.featureSet,r)}feature(){this._attributesClass??=o(this.featureSet.fields?.map(t=>t.name)??[]),this.featureSet.features.push(new r(t(),null,new this._attributesClass)),this._options.applyTransform&&this._setVertexTransformer()}finishFeature(){const{maxStringAttributeLength:t,maxStringAttributeFields:e}=this._options,{attributes:s}=this.featureSet.features.at(-1),i=s[this.featureSet.objectIdFieldName];a(s,e,t,t=>{null!=i&&this.missingAttributes.push({objectId:i,attribute:t})})}_createPoint(){const{hasZ:t,hasM:e,spatialReference:s}=this.featureSet,i={type:"point",x:0,y:0,spatialReference:s,hasZ:t,hasM:e};return t&&(i.z=0),e&&(i.m=0),i}centroid(t){const{hasZ:e,hasM:s}=this.featureSet,i=this._createPoint();[i.x,i.y]=t,e&&(i.z=t[2]),s&&(i.m=t.at(-1)),this.featureSet.features.at(-1).centroid=i}geometry(t,e,s){this._previousX=this._previousY=0,this._lengths=s;let i,{spatialReference:r,hasZ:a,hasM:n}=this.featureSet;switch(a??=!1,n??=!1,this._vertex=[0,0],a&&this._vertex.push(0),n&&this._vertex.push(0),this._finishVertex=p._finishVertexNonPoint,e){case"esriGeometryPoint":this._point=this._createPoint(),i=this._point,this._finishVertex=a?n?p._finishVertexPointXYZM:p._finishVertexPointXYZ:n?p._finishVertexPointXYM:p._finishVertexPointXY;break;case"esriGeometryMultipoint":i={type:"multipoint",points:[],spatialReference:r,hasZ:a,hasM:n},this._part=i.points,this._parts=[];break;case"esriGeometryPolyline":i={type:"polyline",paths:[],spatialReference:r,hasZ:a,hasM:n},this._parts=i.paths,this._part=[];break;case"esriGeometryPolygon":i={type:"polygon",rings:[],spatialReference:r,hasZ:a,hasM:n},this._parts=i.rings,this._part=[]}1===t?(this._queryGeometry=i,this._queryGeometryType=e,this._options.applyTransform||this._setVertexTransformer()):this.featureSet.features.at(-1).geometry=i}coord(t){const e=this._vertex;e[this._i++]=t,this._i<e.length||(this._i=0,this._transformInPlace&&(e[0]+=this._previousX,e[1]+=this._previousY,[this._previousX,this._previousY]=e,this._transformInPlace(e)),this._finishVertex(this))}static _finishVertexPointXY({_point:t,_vertex:e}){[t.x,t.y]=e}static _finishVertexPointXYZ({_point:t,_vertex:e}){[t.x,t.y,t.z]=e}static _finishVertexPointXYM({_point:t,_vertex:e}){[t.x,t.y,t.m]=e}static _finishVertexPointXYZM({_point:t,_vertex:e}){[t.x,t.y,t.z,t.m]=e}static _finishVertexNonPoint(t){const{_part:e,_parts:s,_lengths:i,_vertex:r}=t;e.push([...r]),e.length<i[s.length]||(t._previousX=t._previousY=0,t._part=[],s.push(e))}}export{p as DehydratedFeatureSetParserContext};
