/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{pt2px as e}from"../../../../../../../core/screenUtils.js";import{GeometryCursor as t}from"../../../../../../../geometry/GeometryCursor.js";import{clipCursorToTileExtent as i}from"../../../../../../../symbols/cim/CIMEffects.js";import{maxTileCoordValue as s}from"../../../../../../../symbols/cim/constants.js";import{CIMMarkerPlacementHelper as r}from"../../../../../../../symbols/cim/placements/CIMMarkerPlacementHelper.js";import{spritePadding as n}from"../../../definitions.js";import{AAnimatedMeshWriter as o}from"./AnimatedMeshWriter.js";import{angle as a,sizing as c,animationPointerAndBaseSizeAndReferenceSize as l,uv as m,offset as d,pos as p,bitset as h,id as u,lineLength as f,value1Position2Value2 as x,zoomRange as y}from"./attributes.js";import{createComputedAnimatedMeshParams as P}from"./ComputedAnimatedParams.js";const M=64,g=64,v=2;class b extends o{get vertexSpec(){return{createComputedParams:P,optionalAttributes:{zoomRange:y,value1Position2Value2:x,lineLength:f},attributes:{id:u,bitset:h,pos:p,offset:d.marker,uv:m.marker,animationPointerAndBaseSizeAndReferenceSize:l,sizing:c,angle:a}}}_write(e,{target:i,effectGeometry:s}){const r=this.evaluatedMeshParams.sprite,{textureBinding:n}=r;e.recordStart(this.instanceId,this.attributeLayout,n);const o=i.getDisplayId();if(this.shift&&"esriGeometryPolyline"===i.geometryType){if(!s){const s=t.fromFeatureSetReaderCIM(i);s&&this._writeParticles(e,i,s)}}else if(null!=this.evaluatedMeshParams.placement)this._writePlacedMarkers(e,i);else if("esriGeometryPolygon"===i.geometryType){const t=i.readCentroidForDisplay();if(!t)return;const[s,r]=t.coords;this._writeQuad(e,o,s,r)}else if("esriGeometryPoint"===i.geometryType){const t=i.readXForDisplay(),s=i.readYForDisplay();this._writeQuad(e,o,t,s)}else{const t=i.readGeometryForDisplay();t&&t.forEachVertex((t,i)=>{this._writeQuad(e,o,t,i)})}e.recordEnd()}_writePlacedMarkers(i,n){const o=t.fromFeatureSetReaderCIM(n)?.clone();if(!o)return;const a=-1,c=r.getPlacement(o,a,this.evaluatedMeshParams.placement,e(1),i.id);if(!c)return;const l=n.getDisplayId();let m=c.next(),d=null;for(;null!=m;){const e=m.tx,t=-m.ty;if(Math.abs(e)>s||Math.abs(t)>s){m=c.next();continue}const r=-m.getAngle();i.recordBounds(e,t,M,g),this.shift?d&&this._writeQuad(i,l,d[0],d[1],void 0,r):this._writeQuad(i,l,e,t,void 0,r),d=[e,t],m=c.next()}}_writeParticles(e,t,s){const r=t.getDisplayId(),n=Math.max(this.evaluatedMeshParams.baseSize,this.evaluatedMeshParams.baseSize),o=i(s,2*n,!1);if(!o)return;const{placement:a}=this.evaluatedMeshParams,c=w(o,a?.placePerPart??!1);for(const{particles:i,distance:l}of c){const t=t=>{for(const s of i){const{a:i,b:n}=s;this._writeQuad(e,r,i.position[0],i.position[1],[i.distance-t,n.position[0],n.position[1],n.distance-t],this.evaluatedMeshParams.angleToLine?Math.atan2(i.direction[1],i.direction[0]):0,l,!0)}};if(!a||"placementTemplate"in a||"CIMMarkerPlacementOnVertices"===a.type){let e;if(a&&"CIMMarkerPlacementOnVertices"!==a.type)e=a.placementTemplate;else{e=[0];for(const t of i){const{a:i,b:s}=t,r=i.position[0]-s.position[0],n=i.position[1]-s.position[1],o=Math.sqrt(r*r+n*n);e.push(o)}}let s=-1*l;for(;s<(1+v/2)*l;)for(const i of e)s+=i,t(s)}else"CIMMarkerPlacementAtExtremities"===a.type?"JustBegin"===a.extremityPlacement?t(1):"JustEnd"===a.extremityPlacement?(t(l-1),t(-1)):"Both"===a.extremityPlacement&&(t(1),t(l-1)):"CIMMarkerPlacementOnLine"===a.type&&("LineBeginning"===a.relativeTo?t(1):"LineEnd"===a.relativeTo?(t(l-1),t(-1)):"LineMiddle"===a.relativeTo&&t(l/2))}}_writeQuad(e,t,i,s,r,o=0,a=0,c=!1){const l=this.evaluatedMeshParams.sprite,{rect:m}=l,d=m.x+n,p=m.y+n,h=m.x+m.width-n,u=m.y+m.height-n,f=e.vertexCount();c||e.recordBounds(i,s,M,M);const x={texXmin:d,texYmin:p,texXmax:h,texYmax:u,value1Position2Value2:r,angle:o,lineLength:a};for(let n=0;n<4;n++)this._writeVertex(e,t,i,s,x);e.indexEnsureSize(6),e.indexWrite(f),e.indexWrite(f+1),e.indexWrite(f+2),e.indexWrite(f+1),e.indexWrite(f+3),e.indexWrite(f+2)}}function w(e,t){const i=[];if(t)for(;e.nextPath();){const t=[];for(;e.nextPoint();)t.push([e.x,e.y]);const s=[];let r=0;for(let e=1;e<t.length;e++){const i=t[e-1],n=t[e],o=n[0]-i[0],a=n[1]-i[1],c=Math.sqrt(o*o+a*a),l=o/c,m=a/c;s.push({a:{position:i,distance:r,direction:[l,m]},b:{position:n,distance:r+c,direction:[l,m]}}),r+=c}s.length>0&&i.push({particles:s,distance:r})}else{const t=[];let s=0;for(;e.nextPath();){const i=[];for(;e.nextPoint();)i.push([e.x,e.y]);for(let e=1;e<i.length;e++){const r=i[e-1],n=i[e],o=n[0]-r[0],a=n[1]-r[1],c=Math.sqrt(o*o+a*a),l=o/c,m=a/c;t.push({a:{position:r,distance:s,direction:[l,m]},b:{position:n,distance:s+c,direction:[l,m]}}),s+=c}}t.length>0&&i.push({particles:t,distance:s})}return i}class C extends b{constructor(){super(...arguments),this.shift=!1}}class _ extends b{constructor(){super(...arguments),this.shift=!0}}export{C as AnimatedMarkerMeshWriter,_ as AnimatedMarkerShiftMeshWriter};
