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