(function(){"use strict";try{if(typeof document<"u"){var o=document.createElement("style");o.appendChild(document.createTextNode(".arrow-point{cursor:pointer;border-radius:100px;background-color:#000;outline:none}.arrow-point:hover{background-color:var(--selection-color)}.arrow-path{pointer-events:stroke;cursor:pointer;outline:none}.arrow-path:hover,.arrow-path:active{stroke:var(--selection-color)}")),document.head.appendChild(o)}}catch(e){console.error("vite-plugin-css-injected-by-js",e)}})(); import{r as v,j as E}from"../../../jsx-runtime-2e2d9038.js";import{posWithinBase as R,angle as q,relativeTo as J,middle as T,minus as N,add as S,ratioWithinBase as _,distance as L,mul as D,middlePos as ot,norm as at}from"./Pos.ts-9fe07952.js";import{D as ut}from"../../../cjs-ff7eb0e1.js";import"../../../index-ca121481.js";const rt={width:3};function Z(r,n,t){const s=q(r,n);return{x:r.x-Math.sin(s)*t,y:r.y-Math.cos(s)*t}}function ht({area:r,startPos:n,segments:t,onSegmentsChanges:s,forceStraight:e,wavy:$,style:f,startRadius:y=0,endRadius:j=0,onDeleteRequested:u}){const C=v.useRef(null),b=v.useRef(null),m=v.useRef(null),k=f?.width??rt.width,g=v.useCallback(o=>o.map((l,p)=>p==0?{start:n,controlPoint:l.controlPoint??null,end:l.next}:{start:o[p-1].next,controlPoint:l.controlPoint??null,end:l.next}),[t,n]),[x,z]=v.useState(()=>g(t));v.useLayoutEffect(()=>{z(g(t))},[n,t,g]);const[U,it]=v.useState(!1),nt=v.useRef(null),et=v.useRef(null);function ct(o){return t.flatMap(({next:l,controlPoint:p},i)=>{const M=i==0?n:t[i-1].next,A=R(M,o),O=R(l,o),c=p||_(S(T(A,O),o),o),h=a=>{const d=t[i],w=t.toSpliced(i,1,{...d,controlPoint:a??void 0});s(w)};return[E.jsx(st,{className:"arrow-point-control",posRatio:c,parentBase:o,onPosValidated:h,onRemove:()=>h(null),onMoves:a=>{z(d=>d.toSpliced(i,1,{...d[i],controlPoint:a}))}},i),i!=t.length-1&&E.jsx(st,{className:"arrow-point-next",posRatio:l,parentBase:o,onPosValidated:a=>{const d=t[i],w=t.toSpliced(i,1,{...d,next:a});s(w)},onRemove:()=>{s(t.toSpliced(Math.max(i-1,0),1))},onMoves:a=>{z(d=>d.toSpliced(i,2,{...d[i],end:a},{...d[i+1],start:a}))}},i+"-2")]})}const W=v.useCallback(()=>{const o=r.current.getBoundingClientRect(),l=x[0]??null;if(l==null)throw new Error("segments might not be empty.");const p=x[x.length-1],i=R(n,o),M=R(p.end,o),A=l.controlPoint&&!e?R(l.controlPoint,o):R(l.end,o),O=e?i:p.controlPoint?R(p.controlPoint,o):R(p.start,o),c=Z(i,A,y),h=Z(M,O,j),a=Math.min(c.x,h.x),d=Math.min(c.y,h.y);Object.assign(et.current.style,{left:c.x+"px",top:c.y+"px",transformOrigin:"top center",transform:`translateX(-50%) rotate(${-q(c,A)*(180/Math.PI)}deg)`}),Object.assign(nt.current.style,{left:h.x+"px",top:h.y+"px",transformOrigin:"top center",transform:`translateX(-50%) rotate(${-q(h,O)*(180/Math.PI)}deg)`});const w={left:a+"px",top:d+"px"},V=(e?[{start:n,controlPoint:void 0,end:p.end}]:x).map(({start:I,controlPoint:B,end:G},Q)=>{const P={x:a,y:d},X=J(R(G,o),P),Y=J(R(I,o),P),K=B&&!e?J(R(B,o),P):T(Y,X);return{start:Y,end:X,cp:K}}),H=V.map(({start:I,cp:B,end:G},Q)=>{let P=G;Q==V.length-1&&(P=Z(P,B,j));const X=Q!=0?V[Q-1]:void 0,Y=N(I,X?.cp??T(I,P)),K=X?S(I,Y):B;return $?dt(I,K,B,P,10,10):e?`L${P.x} ${P.y}`:`C${K.x} ${K.y}, ${B.x} ${B.y}, ${P.x} ${P.y}`}).join(" "),F=`M${c.x-a} ${c.y-d} `+H;m.current.setAttribute("d",F),Object.assign(b.current.style,w)},[n,x,e,y,j,f]);v.useEffect(W,[W]),v.useEffect(()=>{const o=l=>{if(!(l.target instanceof Node))return;const p=C.current.contains(l.target);it(p)};return document.addEventListener("mousedown",o),window.addEventListener("resize",W),()=>{document.removeEventListener("mousedown",o),window.removeEventListener("resize",W)}},[W,C]);const lt=v.useCallback(o=>{if(e)return;const l=r.current.getBoundingClientRect(),p={x:o.pageX,y:o.pageY},i=_(p,l);let M=-1,A=!1;for(let c=0;c1?t[c-2].next:n,w=a?a.next:n,V=h.next,H=h.controlPoint?h.controlPoint:T(w,V),F=a?S(w,N(w,a.controlPoint??T(d,w))):H,I=xt(w,F,H,V,i,.05);if(I!=2){M=c,A=I==1;break}}if(M==-1)return;const O=t[M];s(t.toSpliced(M,1,{next:i,controlPoint:A?O.controlPoint:void 0},{next:O.next,controlPoint:A?void 0:O.controlPoint}))},[r,e,s,t,n]);return E.jsxs("div",{ref:C,style:{position:"absolute",top:0,left:0},children:[E.jsx("svg",{ref:b,style:{overflow:"visible",position:"absolute",pointerEvents:"none"},children:E.jsx("path",{className:"arrow-path",ref:m,stroke:"#000",strokeWidth:k,strokeDasharray:f?.dashArray??rt.dashArray,fill:"none",tabIndex:0,onDoubleClick:lt,onKeyUp:o=>{u&&o.key=="Delete"&&u()}})}),E.jsx("div",{className:"arrow-head",style:{position:"absolute",transformOrigin:"center"},ref:nt,children:f?.head?.call(f)}),E.jsx("div",{className:"arrow-tail",style:{position:"absolute",transformOrigin:"center"},ref:et,children:f?.tail?.call(f)}),!e&&U&&ct(r.current.getBoundingClientRect())]})}function dt(r,n,t,s,e,$){function f(m){const k=ft(r,n,t,s,m),g=at(k),x={x:k.y,y:-k.x};return{x:x.x/g*$,y:x.y/g*$}}let y="";const C=1/((L(r,n)+L(n,t)+L(t,s))/100*e*2);let b=0;for(let m=C;m<=1;){const k=tt(r,n,t,s,m),g=f(m);let x;b==1||b==3?x=k:b==0?x=S(k,g):x=N(k,g);const z=tt(r,n,t,s,m-C/2),U=b==0||b==1?S(z,g):N(z,g);y+=`Q${U.x} ${U.y} ${x.x} ${x.y}`,b=(b+1)%4,m+=C,m<1&&m>1-C&&(m=1)}return y}function ft(r,n,t,s,e){return S(S(D(N(n,r),3*(1-e)**2),D(N(t,n),6*(1-e)*e)),D(N(s,t),3*e**2))}function tt(r,n,t,s,e){return S(S(S(D(r,(1-e)**3),D(n,3*e*(1-e)**2)),D(t,3*e**2*(1-e))),D(s,e**3))}function xt(r,n,t,s,e,$){const y=1/((L(r,n)+L(n,t)+L(t,s))/$);function j(u){return L(tt(r,n,t,s,u),e)}for(let u=0;u<1;u+=y)if(j(u)<=$)return u>=.5?1:0;return 2}function st({className:r,posRatio:n,parentBase:t,onMoves:s,onPosValidated:e,onRemove:$,radius:f=7}){const y=v.useRef(null),j=R(n,t);return E.jsx(ut,{nodeRef:y,onStop:()=>{const u=ot(y.current.getBoundingClientRect());e(_(u,t))},onDrag:()=>{const u=ot(y.current.getBoundingClientRect());s(_(u,t))},position:{x:j.x-f,y:j.y-f},children:E.jsx("div",{ref:y,className:`arrow-point ${r}`,style:{position:"absolute",width:f*2,height:f*2},onKeyUp:u=>{u.key=="Delete"&&$()},tabIndex:0})})}export{ht as default};