/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{__addDisposableResource as t,__disposeResources as e}from"tslib";import{G as s}from"./SimpleGeometryCursor.js";import{a as i,c as r,e as n,G as a,f as h,h as m,t as l}from"./Geometry.js";import{a as o,P as c,L as u}from"./MultiPathImpl.js";import{P as _}from"./Envelope.js";import{h as g,P as p,c as k}from"./Point2D.js";import{O as f}from"./OperatorDensify.js";class P{getOperatorType(){return 10204}supportsCurves(){return!0}accelerateGeometry(t,e,s){return!1}canAccelerateGeometry(t){return!1}executeMany(t,e,s,i){return new y(t,e,s,i)}execute(t,e,s,r){t||i("null param is not allowed.");return new y(null,e,s,r).generalize(t)}}class y extends s{constructor(t,e,s,i){super(),this.m_pline=null,this.m_point=new _,this.m_stack=[],this.m_resultstack=[],this.m_callCount=0,this.m_progressTracker=i,this.m_geoms=t,this.m_maxDeviation=e,this.m_bRemoveDegenerateParts=s}tock(){return!0}getRank(){return 1}next(){const t=this.m_geoms.next();return null===t?null:(r(t),this.generalize(t))}getGeometryID(){return this.m_geoms.getGeometryID()}generalize(s){const i=s.getGeometryType();if(n(i))return s;if(i===a.enumEnvelope){const t=new o({vd:s.getDescription()});return t.addEnvelope(s,!1),this.generalize(t)}if(h(i)){const t=new c({vd:s.getDescription()});return t.addSegment(s,!0),this.generalize(t)}if(m(i)||l(""),s.isEmpty()||this.m_maxDeviation<=0)return s;const r=(new f).execute(s,0,.05*this.m_maxDeviation,0,this.m_progressTracker);s.hasNonLinearSegments()&&(this.m_maxDeviation*=.95);const _=r,p=s.createInstance();if(p.getGeometryType()===a.enumPolygon){p.setFillRule(s.getFillRule())}this.m_xy=_.getAttributeStreamRef(0);{const s={stack:[],error:void 0,hasError:!1};try{const e=new u;this.m_pline=e,t(s,g(()=>{this.m_pline=null},!1),!1);for(let t=0,s=_.getPathCount();t<s;t++)this.generalizePath(_.getImpl(),t,p.getImpl())}catch(k){s.error=k,s.hasError=!0}finally{e(s)}}return this.m_resultstack.length=0,this.m_stack.length=0,p}generalizePath(t,e,s){if(t.getPathSize(e)<2)return;this.m_resultstack.length=0,this.m_stack.length=0;const i=t.getPathStart(e),r=t.getPathEnd(e)-1,n=t.isClosedPath(e),a=t.isClosedPathInXYPlane(e);let h=0,m=-1;this.m_stack.push(n?i:r),this.m_stack.push(i);let l=!1,o=!1;for(!this.m_bRemoveDegenerateParts&&a&&(l=!0,o=!0);this.m_stack.length>1;){const e=this.m_stack.at(-1);this.m_stack.pop();const s=this.m_stack.at(-1);let i=t.getXY(e);this.m_pline.setStartXY(i),i=t.getXY(s),this.m_pline.setEndXY(i);const n=[Number.NaN];let a=this.findGreatestDistance(e,s,r,n);a>=0&&(l?l=!1:(o&&n[0]>h&&(h=n[0],m=a),n[0]<=this.m_maxDeviation&&(a=-1))),a>=0?(this.m_stack.push(a),this.m_stack.push(e)):this.m_resultstack.push(e)}n||this.m_resultstack.push(this.m_stack[0]);const c=this.m_resultstack.length;if(c===t.getPathSize(e)&&c===this.m_stack.length)s.addPath(t,e,!0);else if(this.m_resultstack.length>0){if(this.m_bRemoveDegenerateParts&&this.m_resultstack.length<=2){if(n||1===this.m_resultstack.length)return;if(p.distance(t.getXY(this.m_resultstack[0]),t.getXY(this.m_resultstack[1]))<=this.m_maxDeviation)return}if(o&&m>=0&&h<=this.m_maxDeviation){const t=this.m_resultstack.at(-1)>m;this.m_resultstack.push(m),t&&(this.m_resultstack[this.m_resultstack.length-2]=k(this.m_resultstack[this.m_resultstack.length-1],this.m_resultstack[this.m_resultstack.length-1]=this.m_resultstack[this.m_resultstack.length-2]))}for(let e=0,i=this.m_resultstack.length;e<i;e++)t.getPointByVal(this.m_resultstack[e],this.m_point),0===e?s.startPathPoint(this.m_point):s.lineToPoint(this.m_point);if(n){for(let t=this.m_resultstack.length;t<3;t++)s.lineToPoint(this.m_point);s.closePathWithLine()}}}findGreatestDistance(t,e,s,i){let r=e-1;e<=t&&(r=s);let n=-1,a=0;const h=new p;for(let m=t+1;m<=r;m++){this.m_xy.queryPoint2D(2*m,h);const t=h.x,e=h.y,s=this.m_pline.getClosestCoordinate(h,!1);h.assign(this.m_pline.getCoord2D(s)),h.x-=t,h.y-=e;const i=h.length();i>a&&(n=m,a=i),this.m_callCount++}return i[0]=a,n}}export{P as O};
