import*as e from"react";import{composeEventHandlers as t}from"../../primitive/dist/index.js";import{createCollection as o}from"../../react-collection/dist/index.js";import{useComposedRefs as r}from"../../react-compose-refs/dist/index.js";import{createContextScope as n}from"../../react-context/dist/index.js";import{useId as i}from"../../react-id/dist/index.js";import{Primitive as s}from"../../react-primitive/dist/index.js";import{useCallbackRef as c}from"../../react-use-callback-ref/dist/index.js";import{useControllableState as a}from"../../react-use-controllable-state/dist/index.js";import{useDirection as u}from"../../react-direction/dist/index.js";import{jsx as l}from"react/jsx-runtime";var f="rovingFocusGroup.onEntryFocus",d={bubbles:!1,cancelable:!0},p="RovingFocusGroup",[m,v,b]=o(p),[w,g]=n(p,[b]),[F,I]=w(p),x=e.forwardRef(((e,t)=>l(m.Provider,{scope:e.__scopeRovingFocusGroup,children:l(m.Slot,{scope:e.__scopeRovingFocusGroup,children:l(h,{...e,ref:t})})})));x.displayName=p;var h=e.forwardRef(((o,n)=>{const{__scopeRovingFocusGroup:i,orientation:p,loop:m=!1,dir:b,currentTabStopId:w,defaultCurrentTabStopId:g,onCurrentTabStopIdChange:I,onEntryFocus:x,preventScrollOnEntryFocus:h=!1,...R}=o,y=e.useRef(null),A=r(n,y),T=u(b),[E=null,D]=a({prop:w,defaultProp:g,onChange:I}),[j,C]=e.useState(!1),_=c(x),k=v(i),G=e.useRef(!1),[K,L]=e.useState(0);return e.useEffect((()=>{const e=y.current;if(e)return e.addEventListener(f,_),()=>e.removeEventListener(f,_)}),[_]),l(F,{scope:i,orientation:p,dir:T,loop:m,currentTabStopId:E,onItemFocus:e.useCallback((e=>D(e)),[D]),onItemShiftTab:e.useCallback((()=>C(!0)),[]),onFocusableItemAdd:e.useCallback((()=>L((e=>e+1))),[]),onFocusableItemRemove:e.useCallback((()=>L((e=>e-1))),[]),children:l(s.div,{tabIndex:j||0===K?-1:0,"data-orientation":p,...R,ref:A,style:{outline:"none",...o.style},onMouseDown:t(o.onMouseDown,(()=>{G.current=!0})),onFocus:t(o.onFocus,(e=>{const t=!G.current;if(e.target===e.currentTarget&&t&&!j){const t=new CustomEvent(f,d);if(e.currentTarget.dispatchEvent(t),!t.defaultPrevented){const e=k().filter((e=>e.focusable));S([e.find((e=>e.active)),e.find((e=>e.id===E)),...e].filter(Boolean).map((e=>e.ref.current)),h)}}G.current=!1})),onBlur:t(o.onBlur,(()=>C(!1)))})})})),R="RovingFocusGroupItem",y=e.forwardRef(((o,r)=>{const{__scopeRovingFocusGroup:n,focusable:c=!0,active:a=!1,tabStopId:u,...f}=o,d=i(),p=u||d,b=I(R,n),w=b.currentTabStopId===p,g=v(n),{onFocusableItemAdd:F,onFocusableItemRemove:x}=b;return e.useEffect((()=>{if(c)return F(),()=>x()}),[c,F,x]),l(m.ItemSlot,{scope:n,id:p,focusable:c,active:a,children:l(s.span,{tabIndex:w?0:-1,"data-orientation":b.orientation,...f,ref:r,onMouseDown:t(o.onMouseDown,(e=>{c?b.onItemFocus(p):e.preventDefault()})),onFocus:t(o.onFocus,(()=>b.onItemFocus(p))),onKeyDown:t(o.onKeyDown,(e=>{if("Tab"===e.key&&e.shiftKey)return void b.onItemShiftTab();if(e.target!==e.currentTarget)return;const t=function(e,t,o){const r=function(e,t){return"rtl"!==t?e:"ArrowLeft"===e?"ArrowRight":"ArrowRight"===e?"ArrowLeft":e}(e.key,o);return"vertical"===t&&["ArrowLeft","ArrowRight"].includes(r)||"horizontal"===t&&["ArrowUp","ArrowDown"].includes(r)?void 0:A[r]}(e,b.orientation,b.dir);if(void 0!==t){if(e.metaKey||e.ctrlKey||e.altKey||e.shiftKey)return;e.preventDefault();let n=g().filter((e=>e.focusable)).map((e=>e.ref.current));if("last"===t)n.reverse();else if("prev"===t||"next"===t){"prev"===t&&n.reverse();const i=n.indexOf(e.currentTarget);n=b.loop?(r=i+1,(o=n).map(((e,t)=>o[(r+t)%o.length]))):n.slice(i+1)}setTimeout((()=>S(n)))}var o,r}))})})}));y.displayName=R;var A={ArrowLeft:"prev",ArrowUp:"prev",ArrowRight:"next",ArrowDown:"next",PageUp:"first",Home:"first",PageDown:"last",End:"last"};function S(e,t=!1){const o=document.activeElement;for(const r of e){if(r===o)return;if(r.focus({preventScroll:t}),document.activeElement!==o)return}}var T=x,E=y;export{E as Item,T as Root,x as RovingFocusGroup,y as RovingFocusGroupItem,g as createRovingFocusGroupScope};
//# sourceMappingURL=index.js.map
