/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{pt2px as t}from"../../../../../../../core/screenUtils.js";import{GeometryCursor as e}from"../../../../../../../geometry/GeometryCursor.js";import{thinLineHalfWidthThreshold as s}from"../../../definitions.js";import{LineTessellation as i}from"../../../TurboLine.js";import{clipLinesMarshall as r}from"../../../mesh/templates/templateUtils.js";import{processLineCapInput as o,processLineJoinInput as n,packRounded as a,processColorInput as h,getMinMaxZoom as c}from"../fill/meshWriterUtils.js";import{MeshWriter as l}from"../mesh/MeshWriter.js";import{getLineClippingMargin as m}from"../mesh/utils.js";import{DataType as u}from"../../../../../../webgl/enums.js";class p{constructor(){this.extrusionOffsetX=0,this.extrusionOffsetY=0,this.normalX=0,this.normalY=0,this.directionX=0,this.directionY=0,this.distance=0,this.pathLength=0,this.distanceOffset=0,this.lineLength=0}}const d={createComputedParams:t=>t,optionalAttributes:{zoomRange:{type:u.HALF_FLOAT,count:2,pack:({scaleInfo:t},{tileInfo:e})=>c(t,e)}},attributes:{id:{type:u.UNSIGNED_BYTE,count:3,pack:"id"},pos:{type:u.SHORT,count:2,pack:"position",packPrecisionFactor:10},bitset:{type:u.UNSIGNED_BYTE,count:1},color:{type:u.UNSIGNED_BYTE,count:4,normalized:!0,pack:({color:t})=>h(t)},offset:{type:u.BYTE,count:2,packPrecisionFactor:16,packTessellation:({extrusionOffsetX:t,extrusionOffsetY:e})=>[a(t,16),a(e,16)]},normal:{type:u.BYTE,count:2,packPrecisionFactor:16,packTessellation:({normalX:t,normalY:e})=>[a(t,16),a(e,16)]},halfWidth:{type:u.HALF_FLOAT,count:1,pack:({width:e})=>t(.5*e)},referenceHalfWidth:{type:u.HALF_FLOAT,count:1,pack:({referenceWidth:e})=>t(.5*e)}}};class f{constructor(){this.id=0,this.bitset=0,this.indexCount=0,this.vertexCount=0,this.vertexFrom=0,this.vertexBounds=0,this.pathLength=0,this.distanceOffset=0}}const _=65535;class x extends l{constructor(t,e,s,i){super(t,e,s,i),this.vertexSpec=d,this._currentWrite=new f,this._tessellationOptions={halfWidth:0,pixelCoordRatio:1,offset:0,wrapDistance:_,textured:!1},this._tessParams=new p,this._initializeTessellator()}writeLineVertices(t,e,s){const i=this._getLines(e);null!=i&&this._writeVertices(t,s,i)}_initializeTessellator(){this._lineTessellator=new i(this._writeTesselatedVertex.bind(this),this._writeTriangle.bind(this),!0)}_write(t,{target:s,effectGeometry:i}){const r=i??e.fromFeatureSetReaderCIM(s);r&&this._writeGeometry(t,s,r)}_writeGeometry(t,e,s,i){t.recordStart(this.instanceId,this.attributeLayout,i),this.writeLineVertices(t,s,e),t.recordEnd()}_getLines(t){return r(t,m(this.evaluatedMeshParams))}_writeVertices(e,i,r){const{_currentWrite:a,_tessellationOptions:h,evaluatedMeshParams:c}=this,{width:l,capType:m,joinType:u,miterLimit:p,hasSizeVV:d}=c,f=t(.5*l);h.halfWidth=f,h.capType=o(m),h.joinType=n(u),h.miterLimit=p;const x=!d;a.out=e,a.id=i.getDisplayId(),a.vertexCount=0,a.indexCount=0,a.vertexFrom=e.vertexCount(),a.vertexBounds=x&&f<s?0:1;for(const{line:t,start:s,pathLength:o}of r)h.initialDistance=s%_,a.pathLength=o,a.distanceOffset=Math.floor(s/_)*_,this._lineTessellator.tessellate(t,h,x)}_writeTesselatedVertex(t,e,s,i,r,o,n,a,h,c,l){const{out:m,id:u,vertexBounds:p,pathLength:d,distanceOffset:f}=this._currentWrite;return this.hasEffects&&m.recordBounds(t,e,p,p),this._tessParams.extrusionOffsetX=n,this._tessParams.extrusionOffsetY=a,this._tessParams.normalX=h,this._tessParams.normalY=c,this._tessParams.directionX=r,this._tessParams.directionY=o,this._tessParams.distance=l,this._tessParams.pathLength=d,this._tessParams.distanceOffset=f,this._writeVertex(m,u,t,e,this._tessParams),this._currentWrite.vertexFrom+this._currentWrite.vertexCount++}_writeTriangle(t,e,s){const{out:i}=this._currentWrite;i.indexEnsureSize(3),i.indexWrite(t),i.indexWrite(e),i.indexWrite(s),this._currentWrite.indexCount+=3}}export{x as LineMeshWriter,p as LineTessellationParams,d as LineVertexSpec};
