/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{set as t,invert as s,transpose as n,multiply as a}from"../../../core/libs/gl-matrix-2/math/mat3.js";import{create as r}from"../../../core/libs/gl-matrix-2/factories/mat3f64.js";import{transformMat3 as o}from"../../../core/libs/gl-matrix-2/math/vec2.js";import{expandPointInPlace as i}from"../aaBoundingRect.js";import{cloneCurve as c}from"./curveUtils.js";import{angularRangeWrap as h,angularRangeOriented as u,wrapAtan2 as e,angularRange as f,distance2 as m,intersectLineLine as M,findClosestAndFurthestCurveParametersToPoint as l}from"./mathUtils.js";class b{constructor(t,s,n,a,r,o,i,c,h){this.cx=t,this.cy=s,this.a=n,this.b=a,this.cosR=r,this.sinR=o,this.u1=i,this.u2=c,this.isInvalid=h}}function p(t,s){const[n,a,r,o,i,c,h]=s.a,[f,m]=t,[M,l]=n,[p,R]=a,d=c*h,x=Math.cos(i),q=Math.sin(i),[I,g]=u(e(1/d*(m-R)*x-1/d*(f-p)*q,1/c*(m-R)*q+1/c*(f-p)*x),Math.atan2(1/d*(l-R)*x-1/d*(M-p)*q,1/c*(l-R)*q+1/c*(M-p)*x),r,o);return new b(p,R,c,d,x,q,I,g,0===c||0===d)}function R(t,s){const{a:n,b:a,cosR:r,sinR:o,cx:i,cy:c}=t,h=Math.cos(s),u=Math.sin(s);return[n*h*r-a*u*o+i,n*h*o+a*u*r+c]}function d(t,s){const{u1:n,u2:a}=t;return R(t,n*(1-s)+a*s)}function x(t,s,n){if(n<0||n>1)return[];if(0===n||1===n)return[c(s)];const[a,[r,o],i,h,u,e,f]=s.a,{u1:m,u2:M}=t,l=m*(1-n)+M*n;return[{a:[[...d(t,n)],[r,o],Math.abs(l-m)<Math.PI?1:0,h,u,e,f]},{a:[[...a],[r,o],Math.abs(M-l)<Math.PI?1:0,h,u,e,f]}]}function q(t,s){const{a:n,b:a,cosR:r,sinR:o,u1:i,u2:c}=t,u=Math.PI/6,e=l(s,[1*u,2*u,4*u,5*u,7*u,8*u,10*u,11*u],s=>{const i=Math.cos(s),c=Math.sin(s),[h,u]=R(t,s);return[h,u,-n*r*c-a*o*i,-n*o*c+a*r*i,-n*r*i+a*o*c,-n*o*i-a*r*c]}),m=f(i,c),M=new Set(e.map(t=>h(m,t)).filter(t=>t>m.min&&t<m.max));M.add(i),M.add(c);let b,p,d=1/0;for(const h of M){const n=R(t,h),[a,r]=n,o=s[0]-a,u=s[1]-r,e=o*o+u*u;e<d&&(b=(h-i)/(c-i),p=n,d=e)}return{t:b,curvePoint:p,distance:Math.sqrt(d)}}function I(t,s,n){const a=p(s,n),{a:r,b:o,u1:c,u2:u,cosR:e,sinR:m,isInvalid:M}=a,l=n.a[0];if(i(t,s),i(t,l),M)return t;const b=Math.atan2(-o*m,r*e),d=b+Math.PI,x=Math.atan2(o*e,r*m),q=x+Math.PI,I=f(c,u);for(const f of[b,d,x,q].map(t=>h(I,t)))f>I.min&&f<I.max&&i(t,R(a,f));return t}function g(t,s,n){const[a,r,o,i,c,h,u]=s.a,e=p(t,s),{u1:f,u2:l,isInvalid:b}=e,x=l-f;if(b)return Math.sqrt(m(t,a));const q=14,I=[0,0];let g=0,j=[d(e,0),d(e,.25),d(e,.5),d(e,.75),d(e,1)];for(let p=0;p<q;p++){const t=j.length-1,s=x/(2*t),a=[];for(let n=0;n<t;n++)a.push(j[n]),a.push(R(e,f+(2*n+1)*s));a.push(j[j.length-1]),j=a,g=0;let o=0;for(let n=1;n<j.length;n++){const t=j[n-1],s=j[n];if(g+=Math.sqrt(m(t,s)),n>1){const a=j[n-2],i=M(I,a,t,r,s);o+=i?Math.sqrt(m(t,i)):Math.abs(s[0]-t[0])+Math.abs(s[1]-t[1])}else o+=g}if(o-g<n)return g}return g}const j=r(),v=r(),P=r(),y=r();function w(r,i,c){const[h,u,f,M,l,b,R]=i.a,d=p(r,i),{cosR:x,sinR:q,a:I,b:g,cx:w,cy:U}=d,B=o([0,0],h,c),S=o([0,0],u,c),k=c[0]*c[4]<0?1-M:M;{const s=I*I*q*q+g*g*x*x,n=2*(g*g-I*I)*q*x,a=I*I*x*x+g*g*q*q,r=-2*s*w-n*U,o=-n*w-2*a*U;t(j,s,n/2,r/2,n/2,a,o/2,r/2,o/2,s*w*w+n*w*U+a*U*U-I*I*g*g)}if(s(v,c),null==v){const t=o([0,0],r,c);return{a:[B,S,f,M,e(B[1],B[0]),Math.sqrt(m(B,t))/2,0]}}n(P,v),a(y,a(y,P,j),v);{const t=y[0],s=2*y[1],n=y[4],a=2*y[2],r=2*y[5],o=s*s-4*t*n,i=2*(t*r*r+n*a*a-s*a*r+o*y[8]),c=Math.sqrt((t-n)**2+s*s),h=-Math.sqrt(i*(t+n+c))/o,u=-Math.sqrt(i*(t+n-c))/o;return{a:[B,S,f,k,.5*e(-s,n-t),h,u/h]}}}export{b as DerivedEllipse,q as closestPointOnEllipse,p as deriveEllipse,I as ellipticArcExtent,g as ellipticArcLength,d as interpolateEllipse,x as splitEllipticArc7,w as transformEllipticArc};
