/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{zScale as t}from"../../../geometry/support/zscale.js";import{makeAttributesClass as e,makeVertexTransformer as s}from"./pbfFeatureSetUtils.js";class r{constructor(t){this._options=t,this.featureSet={features:[]},this.parseOnly=null,this._transformInPlace=null,this._transformInPlaceXY=null,this._finishVertex=r._finishVertexNonPoint,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}queryGeometryType(){return this.featureSet.queryGeometryType}queryGeometry(){return this.featureSet.queryGeometry}idField(t){"string"==typeof t&&(this.featureSet.objectIdFieldName=t)}finish(){this._options.applyTransform&&(this.featureSet.transform=null);const{spatialReference:t,features:e}=this.featureSet;if(t)for(const{geometry:s}of e)s&&(s.spatialReference=t)}feature(){this._attributesClass??=e(this.featureSet.fields?.map(t=>t.name)??[]),this.featureSet.features.push({attributes:new this._attributesClass}),this._options.applyTransform&&this._setVertexTransformer()}centroid([t,e]){this.featureSet.features.at(-1).centroid={x:t,y:e}}geometry(t,e,s){this._previousX=this._previousY=0,this._lengths=s;let i,{hasZ:a,hasM:h}=this.featureSet;switch(a??=!1,h??=!1,this._i=0,this._vertex=[0,0],a&&this._vertex.push(0),h&&this._vertex.push(0),this._finishVertex=r._finishVertexNonPoint,e){case"esriGeometryPoint":this._point={x:0,y:0},i=this._point,this._finishVertex=a?h?r._finishVertexPointXYZM:r._finishVertexPointXYZ:h?r._finishVertexPointXYM:r._finishVertexPointXY;break;case"esriGeometryMultipoint":i={points:[],hasZ:a,hasM:h},this._part=i.points,this._parts=[];break;case"esriGeometryPolyline":i={paths:[],hasZ:a,hasM:h},this._parts=i.paths,this._part=[];break;case"esriGeometryPolygon":i={rings:[],hasZ:a,hasM:h},this._parts=i.rings,this._part=[]}1===t?(this.featureSet.queryGeometry=i,this.featureSet.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))}curvedGeometry(t,e,s,r){let i,{hasZ:a,hasM:h}=this.featureSet;switch(a??=!1,h??=!1,t){case"esriGeometryPoint":case"esriGeometryMultipoint":throw new Error(`Curved ${t} does not make sense`);case"esriGeometryPolyline":i={paths:[],curvePaths:[],hasZ:a,hasM:h},this._parts=i.curvePaths,this._part=[];break;case"esriGeometryPolygon":i={rings:[],curveRings:[],hasZ:a,hasM:h},this._parts=i.curveRings,this._part=[]}this.featureSet.features.at(-1).geometry=i}startCurvedPart(t){this._part.push(this._transform(t))}finishCurvedPart(t){this._parts.push(this._part),this._part=[]}lineSegment(t,e){this._part.push(this._transform(e))}bezierSegment(t,e,s,r){this._part.push({b:[this._transform(e),this._transformXY(s),this._transformXY(r)]})}circularArcSegment(t,e,s){this._part.push({c:[this._transform(e),this._transformXY(s)]})}ellipticArcSegment(t,e,s,r,i,a,h,n){this._part.push({a:[this._transform(e),this._transformXY(s),r,i,a,h,n]})}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:r,_vertex:i}=t;e.push([...i]),e.length<r[s.length]||(t._previousX=t._previousY=0,t._part=[],s.push(e))}_setVertexTransformer(){const{sourceSpatialReference:e}=this._options,{spatialReference:r,hasZ:i,transform:a}=this.featureSet,h=i?t(e,r):1;this._transformInPlace??=s(this.featureSet,h),this._transformInPlaceXY??=s({transform:a})}_transform(t){const e=[...t];return this._transformInPlace?.(e),e}_transformXY(t){const e=[t[0],t[1]];return this._transformInPlaceXY?.(e),e}}export{r as JSONFeatureSetParserContext};
