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