import{n as e}from"./rolldown-runtime-C0FnF6B9.js";import{t}from"./react-C21x__mS.js";function n(e){if(typeof document>`u`)return null;let t=document.getElementById(e);if(t instanceof HTMLElement)return t;let n=document.createElement(`div`);return n.id=e,document.body.append(n),n}function r(e,t){(0,o.useEffect)(()=>{let n=n=>{if(Array.isArray(e)){let r=!0;for(let t of e)t.current&&t.current.contains(n.target)&&(r=!1);r&&t()}else e.current&&!e.current.contains(n.target)&&t()};return document.addEventListener(`mousedown`,n),()=>document.removeEventListener(`mousedown`,n)},[e,t])}function i({active:e,onDismiss:t,enabled:n=!0,documentTarget:r,containerRef:i}){let a=(0,o.useEffectEvent)(()=>t());(0,o.useEffect)(()=>{if(!e||!n)return;let t=r??(typeof document>`u`?null:document);if(!t)return;let o=e=>{if(e.key!==`Escape`)return;let n=t.activeElement,r=i?.current;r&&n instanceof Node&&n!==r&&!r.contains(n)||(e.preventDefault(),a())};return t.addEventListener(`keydown`,o,!0),()=>t.removeEventListener(`keydown`,o,!0)},[e,n,r,i])}function a({active:e,trapFocus:t=!1,restoreFocus:n=!0,initialFocus:r=`auto`,restoreFocusTarget:i,containerRef:a}){let c=(0,o.useRef)(null),l=a??c,u=(0,o.useRef)(null),d=(0,o.useEffectEvent)(e=>Array.from(e.querySelectorAll(s)).filter(e=>!e.hasAttribute(`aria-hidden`))),f=(0,o.useEffectEvent)(e=>{if(r===`container`)return e;if(typeof r==`function`)return r(e)??e;let t=d(e);return t.find(e=>e.hasAttribute(`autofocus`)||e.hasAttribute(`autoFocus`))??t[0]??e}),p=(0,o.useEffectEvent)(()=>i?.()??u.current);return(0,o.useEffect)(()=>{if(!e)return;u.current=document.activeElement instanceof HTMLElement?document.activeElement:null;let r=null,i=0,a=()=>{if(r=l.current,!r){i=window.requestAnimationFrame(a);return}let e=f(r);e instanceof HTMLElement&&e.isConnected&&e.focus(),t&&r.addEventListener(`keydown`,o)},o=e=>{if(!t||e.key!==`Tab`||!r)return;let n=d(r);if(n.length===0){e.preventDefault(),r.focus();return}let i=n[0],a=n[n.length-1],o=document.activeElement;if(e.shiftKey){(o===i||o===r)&&(e.preventDefault(),a.focus());return}o===a&&(e.preventDefault(),i.focus())};return a(),()=>{if(window.cancelAnimationFrame(i),t&&r&&r.removeEventListener(`keydown`,o),!n)return;let e=p();e?.isConnected&&e.focus()}},[e,t,n,l]),l}var o,s;function c(){return(c=e((()=>{o=t(),s=`button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"]):not([disabled])`})))()}export{a,r as i,i as n,c as r,n as t};