import{a as j,ag as g,$ as P,j as m,a4 as G,r as l,b as z,P as L,d as w,u as X,X as J}from"./index-DszW2bOJ.js";import{u as Q}from"./index-CdIMhveG.js";function W(e){const n=e+"CollectionProvider",[i,r]=j(n),[A,v]=i(n,{collectionRef:{current:null},itemMap:new Map}),I=f=>{const{scope:t,children:a}=f,d=g.useRef(null),o=g.useRef(new Map).current;return m.jsx(A,{scope:t,itemMap:o,collectionRef:d,children:a})};I.displayName=n;const R=e+"CollectionSlot",_=G(R),p=g.forwardRef((f,t)=>{const{scope:a,children:d}=f,o=v(R,a),c=P(t,o.collectionRef);return m.jsx(_,{ref:c,children:d})});p.displayName=R;const s=e+"CollectionItemSlot",C="data-radix-collection-item",E=G(s),b=g.forwardRef((f,t)=>{const{scope:a,children:d,...o}=f,c=g.useRef(null),T=P(t,c),x=v(s,a);return g.useEffect(()=>(x.itemMap.set(c,{ref:c,...o}),()=>void x.itemMap.delete(c))),m.jsx(E,{[C]:"",ref:T,children:d})});b.displayName=s;function F(f){const t=v(e+"CollectionConsumer",f);return g.useCallback(()=>{const d=t.collectionRef.current;if(!d)return[];const o=Array.from(d.querySelectorAll(`[${C}]`));return Array.from(t.itemMap.values()).sort((x,y)=>o.indexOf(x.ref.current)-o.indexOf(y.ref.current))},[t.collectionRef,t.itemMap])}return[{Provider:I,Slot:p,ItemSlot:b},F,r]}var N="rovingFocusGroup.onEntryFocus",Z={bubbles:!1,cancelable:!0},h="RovingFocusGroup",[O,K,ee]=W(h),[te,de]=j(h,[ee]),[oe,ne]=te(h),k=l.forwardRef((e,n)=>m.jsx(O.Provider,{scope:e.__scopeRovingFocusGroup,children:m.jsx(O.Slot,{scope:e.__scopeRovingFocusGroup,children:m.jsx(re,{...e,ref:n})})}));k.displayName=h;var re=l.forwardRef((e,n)=>{const{__scopeRovingFocusGroup:i,orientation:r,loop:A=!1,dir:v,currentTabStopId:I,defaultCurrentTabStopId:R,onCurrentTabStopIdChange:_,onEntryFocus:p,preventScrollOnEntryFocus:s=!1,...C}=e,E=l.useRef(null),b=P(n,E),F=Q(v),[f,t]=X({prop:I,defaultProp:R??null,onChange:_,caller:h}),[a,d]=l.useState(!1),o=J(p),c=K(i),T=l.useRef(!1),[x,y]=l.useState(0);return l.useEffect(()=>{const u=E.current;if(u)return u.addEventListener(N,o),()=>u.removeEventListener(N,o)},[o]),m.jsx(oe,{scope:i,orientation:r,dir:F,loop:A,currentTabStopId:f,onItemFocus:l.useCallback(u=>t(u),[t]),onItemShiftTab:l.useCallback(()=>d(!0),[]),onFocusableItemAdd:l.useCallback(()=>y(u=>u+1),[]),onFocusableItemRemove:l.useCallback(()=>y(u=>u-1),[]),children:m.jsx(L.div,{tabIndex:a||x===0?-1:0,"data-orientation":r,...C,ref:b,style:{outline:"none",...e.style},onMouseDown:w(e.onMouseDown,()=>{T.current=!0}),onFocus:w(e.onFocus,u=>{const Y=!T.current;if(u.target===u.currentTarget&&Y&&!a){const D=new CustomEvent(N,Z);if(u.currentTarget.dispatchEvent(D),!D.defaultPrevented){const M=c().filter(S=>S.focusable),H=M.find(S=>S.active),$=M.find(S=>S.id===f),q=[H,$,...M].filter(Boolean).map(S=>S.ref.current);V(q,s)}}T.current=!1}),onBlur:w(e.onBlur,()=>d(!1))})})}),U="RovingFocusGroupItem",B=l.forwardRef((e,n)=>{const{__scopeRovingFocusGroup:i,focusable:r=!0,active:A=!1,tabStopId:v,children:I,...R}=e,_=z(),p=v||_,s=ne(U,i),C=s.currentTabStopId===p,E=K(i),{onFocusableItemAdd:b,onFocusableItemRemove:F,currentTabStopId:f}=s;return l.useEffect(()=>{if(r)return b(),()=>F()},[r,b,F]),m.jsx(O.ItemSlot,{scope:i,id:p,focusable:r,active:A,children:m.jsx(L.span,{tabIndex:C?0:-1,"data-orientation":s.orientation,...R,ref:n,onMouseDown:w(e.onMouseDown,t=>{r?s.onItemFocus(p):t.preventDefault()}),onFocus:w(e.onFocus,()=>s.onItemFocus(p)),onKeyDown:w(e.onKeyDown,t=>{if(t.key==="Tab"&&t.shiftKey){s.onItemShiftTab();return}if(t.target!==t.currentTarget)return;const a=ae(t,s.orientation,s.dir);if(a!==void 0){if(t.metaKey||t.ctrlKey||t.altKey||t.shiftKey)return;t.preventDefault();let o=E().filter(c=>c.focusable).map(c=>c.ref.current);if(a==="last")o.reverse();else if(a==="prev"||a==="next"){a==="prev"&&o.reverse();const c=o.indexOf(t.currentTarget);o=s.loop?ie(o,c+1):o.slice(c+1)}setTimeout(()=>V(o))}}),children:typeof I=="function"?I({isCurrentTabStop:C,hasTabStop:f!=null}):I})})});B.displayName=U;var ce={ArrowLeft:"prev",ArrowUp:"prev",ArrowRight:"next",ArrowDown:"next",PageUp:"first",Home:"first",PageDown:"last",End:"last"};function se(e,n){return n!=="rtl"?e:e==="ArrowLeft"?"ArrowRight":e==="ArrowRight"?"ArrowLeft":e}function ae(e,n,i){const r=se(e.key,i);if(!(n==="vertical"&&["ArrowLeft","ArrowRight"].includes(r))&&!(n==="horizontal"&&["ArrowUp","ArrowDown"].includes(r)))return ce[r]}function V(e,n=!1){const i=document.activeElement;for(const r of e)if(r===i||(r.focus({preventScroll:n}),document.activeElement!==i))return}function ie(e,n){return e.map((i,r)=>e[(n+r)%e.length])}var me=k,pe=B;export{pe as I,me as R,W as a,de as c};
