/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{a as e,t,G as r}from"./Geometry.js";import{Envelope2D as s}from"./Envelope2D.js";import{m as n,a as o,c as i}from"./SideCalculator2D-DX0718xq.js";import{d as a}from"./Envelope.js";import{P as u}from"./Point2D.js";import{g as c}from"./SpatialReference.js";class m{getOperatorType(){return 10500}accelerateGeometry(e,t,r){return!1}canAccelerateGeometry(e){return!1}supportsCurves(){return!0}getNearestCoordinate(s,o,i,a){if(o.isNAN()&&e("NAN xy coordinates are not allowed"),s.isEmpty())return n();switch(s.getGeometryType()){case r.enumPoint:return this.pointGetNearestVertex(s,o);case r.enumMultiPoint:return this.multiVertexGetNearestVertex(s,o);case r.enumPolyline:case r.enumPolygon:return this.multiPathGetNearestCoordinate(s,o,i,a);default:t("")}}getNearestVertex(s,o){if(o.isNAN()&&e("NAN xy coordinates are not allowed"),s.isEmpty())return n();switch(s.getGeometryType()){case r.enumPoint:return this.pointGetNearestVertex(s,o);case r.enumMultiPoint:case r.enumPolyline:case r.enumPolygon:return this.multiVertexGetNearestVertex(s,o);default:t("")}}getNearestVertices(s,n,o,i){if(0===i&&e(""),n.isNAN()&&e("NAN xy coordinates are not allowed"),s.isEmpty())return[];switch(s.getGeometryType()){case r.enumPoint:return this.pointGetNearestVertices(s,n,o,i);case r.enumMultiPoint:case r.enumPolyline:case r.enumPolygon:return this.multiVertexGetNearestVertices(s,n,o,i);default:t("")}}multiPathGetNearestCoordinate(t,n,m,l){if(t.getGeometryType()===r.enumPolygon&&m){const e=new s;t.queryEnvelope(e);const r=a(null,e,!1);let i;if(i=c(t,n,l?0:r),0!==i){const e=o(n,-1,0);return l&&(e.m_bRightSide=!0),e}}const g=t.querySegmentIterator(),y=new u;let p=-1,N=-1,f=Number.MAX_VALUE,d=0;for(;g.nextPath();)for(;g.hasNextSegment();){const e=g.nextSegment(),t=e.getClosestCoordinate(n,!1),r=e.getCoord2D(t),s=u.sqrDistance(r,n);s<f?(d=1,y.assign(r),p=g.getStartPointIndex(),N=g.getPathIndex(),f=s):s===f&&d++}-1===p&&e("");const h=o(y,p,Math.sqrt(f));if(l)if(t.getGeometryType()!==r.enumPolygon){let e=!1;if(d>1){const r=i(t,n,p,N);e=0!==r&&3!==r}else{g.resetToVertex(p,N);const t=g.nextSegment();e=0!==i(t,n)}h.m_bRightSide=e}else if(!m){0!==c(t,n,0)&&(h.m_bRightSide=!0)}return h}pointGetNearestVertex(e,t){const r=e.getXY(),s=u.distance(r,t);return o(r,0,s)}multiVertexGetNearestVertex(e,t){const r=e.getAttributeStreamRef(0),s=e.getPointCount();let n=-1;const i=new u;let a=Number.MAX_VALUE;const c=new u;for(let o=0;o<s;o++){r.queryPoint2D(2*o,c);const e=u.sqrDistance(c,t);e<a&&(i.assign(c),n=o,a=e)}return o(i,n,Math.sqrt(a))}pointGetNearestVertices(e,t,r,s){const n=[];if(0!==s){const s=r*r,i=e.getXY(),a=u.sqrDistance(i,t);a<=s&&n.push(o(i,0,Math.sqrt(a)))}return n}multiVertexGetNearestVertices(e,t,r,s){const n=[];if(0!==s){const i=e.getAttributeStreamRef(0),a=e.getPointCount();n.length=s+1;const c=r*r;for(let e=0;e<a;e++){const r=i.read(2*e),s=i.read(2*e+1),a=t.x-r,m=t.y-s,l=a*a+m*m;l<=c&&n.push(o(u.construct(r,s),e,Math.sqrt(l)))}n.sort((e,t)=>e.m_distance-t.m_distance)}return n.slice(0,s)}}export{m as O};
