import{r as d,j as R}from"./vendor-react-BUCujlKS.js";import{u as M}from"./vendor-radix-ui-react-compose-refs-DcAh5WdT.js";import{a as _}from"./vendor-radix-ui-react-primitive-DhVoX9SD.js";import{u as y}from"./vendor-radix-ui-react-use-callback-ref-BZPnuXAC.js";var F="focusScope.autoFocusOnMount",T="focusScope.autoFocusOnUnmount",N={bubbles:!1,cancelable:!0},x="FocusScope",K=d.forwardRef((e,t)=>{const{loop:n=!1,trapped:u=!1,onMountAutoFocus:p,onUnmountAutoFocus:L,...g}=e,[s,U]=d.useState(null),E=y(p),v=y(L),b=d.useRef(null),A=M(t,o=>U(o)),i=d.useRef({paused:!1,pause(){this.paused=!0},resume(){this.paused=!1}}).current;d.useEffect(()=>{if(u){let o=function(a){if(i.paused||!s)return;const c=a.target;s.contains(c)?b.current=c:f(b.current,{select:!0})},l=function(a){if(i.paused||!s)return;const c=a.relatedTarget;c!==null&&(s.contains(c)||f(b.current,{select:!0}))},r=function(a){if(document.activeElement===document.body)for(const h of a)h.removedNodes.length>0&&f(s)};document.addEventListener("focusin",o),document.addEventListener("focusout",l);const m=new MutationObserver(r);return s&&m.observe(s,{childList:!0,subtree:!0}),()=>{document.removeEventListener("focusin",o),document.removeEventListener("focusout",l),m.disconnect()}}},[u,s,i.paused]),d.useEffect(()=>{if(s){O.add(i);const o=document.activeElement;if(!s.contains(o)){const r=new CustomEvent(F,N);s.addEventListener(F,E),s.dispatchEvent(r),r.defaultPrevented||(k(V(I(s)),{select:!0}),document.activeElement===o&&f(s))}return()=>{s.removeEventListener(F,E),setTimeout(()=>{const r=new CustomEvent(T,N);s.addEventListener(T,v),s.dispatchEvent(r),r.defaultPrevented||f(o??document.body,{select:!0}),s.removeEventListener(T,v),O.remove(i)},0)}}},[s,E,v,i]);const P=d.useCallback(o=>{if(!n&&!u||i.paused)return;const l=o.key==="Tab"&&!o.altKey&&!o.ctrlKey&&!o.metaKey,r=document.activeElement;if(l&&r){const m=o.currentTarget,[a,c]=w(m);a&&c?!o.shiftKey&&r===c?(o.preventDefault(),n&&f(a,{select:!0})):o.shiftKey&&r===a&&(o.preventDefault(),n&&f(c,{select:!0})):r===m&&o.preventDefault()}},[n,u,i.paused]);return R.jsx(_.div,{tabIndex:-1,...g,ref:A,onKeyDown:P})});K.displayName=x;function k(e,{select:t=!1}={}){const n=document.activeElement;for(const u of e)if(f(u,{select:t}),document.activeElement!==n)return}function w(e){const t=I(e),n=S(t,e),u=S(t.reverse(),e);return[n,u]}function I(e){const t=[],n=document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT,{acceptNode:u=>{const p=u.tagName==="INPUT"&&u.type==="hidden";return u.disabled||u.hidden||p?NodeFilter.FILTER_SKIP:u.tabIndex>=0?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP}});for(;n.nextNode();)t.push(n.currentNode);return t}function S(e,t){for(const n of e)if(!D(n,{upTo:t}))return n}function D(e,{upTo:t}){if(getComputedStyle(e).visibility==="hidden")return!0;for(;e;){if(t!==void 0&&e===t)return!1;if(getComputedStyle(e).display==="none")return!0;e=e.parentElement}return!1}function H(e){return e instanceof HTMLInputElement&&"select"in e}function f(e,{select:t=!1}={}){if(e&&e.focus){const n=document.activeElement;e.focus({preventScroll:!0}),e!==n&&H(e)&&t&&e.select()}}var O=j();function j(){let e=[];return{add(t){const n=e[0];t!==n&&n?.pause(),e=C(e,t),e.unshift(t)},remove(t){e=C(e,t),e[0]?.resume()}}}function C(e,t){const n=[...e],u=n.indexOf(t);return u!==-1&&n.splice(u,1),n}function V(e){return e.filter(t=>t.tagName!=="A")}export{K as F};
