/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../../../core/maybe","../../../../../../chunks/earcut","../../../../../../geometry/libtess","../../../../../../layers/graphics/OptimizedGeometry","../../definitions","../../Geometry","../../TileClipper","../bufcut"],(function(e,t,n,r,o,l,s,i,u){"use strict";const c=1e-5,f=new i.TileClipper(0,0,0,1,0),a=new i.TileClipper(0,0,0,1,0);function h(e,t,n){let r=0;for(let o=1;o<n;o++){const n=e[2*(t+o-1)],l=e[2*(t+o-1)+1];r+=(e[2*(t+o)]-n)*(e[2*(t+o)+1]+l)}return r}function g(e,t,n,r,o){let l=0;const s=2;for(let i=n;i<r;i+=3){const n=(e[i]-o)*s,r=(e[i+1]-o)*s,u=(e[i+2]-o)*s;l+=Math.abs((t[n]-t[u])*(t[r+1]-t[n+1])-(t[n]-t[r])*(t[u+1]-t[n+1]))}return l}function p(e,t,n,r,o){let l=0;const s=2;for(let i=n;i<r;i+=3){const n=(e.getValue(i)-o)*s,r=(e.getValue(i+1)-o)*s,u=(e.getValue(i+2)-o)*s;l+=Math.abs((t[n]-t[u])*(t[r+1]-t[n+1])-(t[n]-t[r])*(t[u+1]-t[n+1]))}return l}function d(e,t){const{coords:n,lengths:r,hasIndeterminateRingOrder:o}=t,l=0,s=e;if(o)return!1;let i=0;for(let f=0;f<r.length;){let e=f,t=r[f],o=h(n,i,t);const a=[];for(;++e<r.length;){const l=r[e],s=h(n,i+t,l);if(!(s>0))break;o+=s,a.push(i+t),t+=l}const p=s.length;u.bufcut(s,n,i,i+t,a,2,l);const d=g(s,n,p,s.length,l),b=Math.abs(o);if(Math.abs((d-b)/Math.max(1e-7,b))>c)return s.length=0,!1;f=e,i+=t}return!0}function b(e,t,r){const{coords:o,lengths:l,hasIndeterminateRingOrder:s}=t;if(s)return!1;let i=0;for(let u=0;u<l.length;){let s=u,c=l[u];const f=[];for(;++s<l.length;){const e=l[s];if(!(h(o,i+c,e)>0))break;f.push(i+c-i),c+=e}const a=i+c,g=t.coords.slice(2*i,2*a),p=n.earcut(g,f,2);for(const t of p)e.push(t+r+i);u=s,i+=c}return!0}function y(e){const{coords:t,lengths:n}=e,{buffer:o}=r.triangulate(t,n);return o}function T(e,t,n){let r=0;for(let o=0;o<e.lengths.length;o++){const l=e.lengths[o];for(let o=0;o<l;o++){const l=e.coords[2*(o+r)],s=e.coords[2*(o+r)+1];if(l<t||l>n||s<t||s>n)return!0}r+=l}return!1}function m(e,n){if(t.isNone(e))return null;if(!T(e,-128,l.TILE_SIZE+128))return e;f.setPixelMargin(n),f.reset(s.GeometryType.Polygon);let r=0;for(let t=0;t<e.lengths.length;t++){const n=e.lengths[t];let o=e.coords[2*(0+r)],l=e.coords[2*(0+r)+1];f.moveTo(o,l);for(let t=1;t<n;t++)o=e.coords[2*(t+r)],l=e.coords[2*(t+r)+1],f.lineTo(o,l);f.close(),r+=n}const i=f.result(!1);if(!i)return null;const u=[],c=[];for(const t of i){let e=0;for(const n of t)c.push(n.x),c.push(n.y),e++;u.push(e)}return new o(u,c)}function E(e,t){a.setPixelMargin(t);const n=a,r=-t,o=l.TILE_SIZE+t;let i=[],u=!1,c=0;for(;c<e.length;){const t=[],l=e[c];if(!l)return null;n.reset(s.GeometryType.LineString);let[f,a]=l[0];if(u)n.moveTo(f,a);else{if(f<r||f>o||a<r||a>o){u=!0;continue}t.push({x:f,y:a})}let h=!1;const g=l.length;for(let e=1;e<g;++e)if(f+=l[e][0],a+=l[e][1],u)n.lineTo(f,a);else{if(f<r||f>o||a<r||a>o){h=!0;break}t.push({x:f,y:a})}if(h)u=!0;else{if(u){const e=n.resultWithStarts();if(e)for(const t of e)i.push(t)}else i.push({line:t,start:0});c++,u=!1}}return i=i.filter((e=>e.line.length>1)),0===i.length?null:i}f.setExtent(l.TILE_SIZE),a.setExtent(l.TILE_SIZE),e.area=h,e.clipLinesMarshall=E,e.clipMarshall=m,e.triangleArea=p,e.triangleAreaArray=g,e.triangulate=d,e.triangulateEarcut=b,e.triangulateLibtess=y,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
