import d,{useEffect as U,useRef as T}from"react";import{forwardRefWithAs as C,render as I}from'../../utils/render.js';import{useServerHandoffComplete as N}from'../../hooks/use-server-handoff-complete.js';import{useSyncRefs as x}from'../../hooks/use-sync-refs.js';import{Features as F,Hidden as v}from'../../internal/hidden.js';import{focusElement as f,focusIn as b,Focus as i,FocusResult as G}from'../../utils/focus-management.js';import{match as g}from'../../utils/match.js';import{useEvent as K}from'../../hooks/use-event.js';import{useTabDirection as V,Direction as M}from'../../hooks/use-tab-direction.js';import{useIsMounted as O}from'../../hooks/use-is-mounted.js';import{useOwnerDocument as W}from'../../hooks/use-owner.js';import{useEventListener as k}from'../../hooks/use-event-listener.js';import{microTask as y}from'../../utils/micro-task.js';import{useWatch as j}from'../../hooks/use-watch.js';import{useDisposables as q}from'../../hooks/use-disposables.js';let z="div";var A=(t=>(t[t.None=1]="None",t[t.InitialFocus=2]="InitialFocus",t[t.TabLock=4]="TabLock",t[t.FocusLock=8]="FocusLock",t[t.RestoreFocus=16]="RestoreFocus",t[t.All=30]="All",t))(A||{});let de=Object.assign(C(function(u,e){let l=T(null),a=x(l,e),{initialFocus:m,containers:t,features:n=30,...E}=u;N()||(n=1);let s=W(l);J({ownerDocument:s},Boolean(n&16));let S=Q({ownerDocument:s,container:l,initialFocus:m},Boolean(n&2));X({ownerDocument:s,container:l,containers:t,previousActiveElement:S},Boolean(n&8));let H=V(),R=K(o=>{let c=l.current;if(!c)return;(_=>_())(()=>{g(H.current,{[M.Forwards]:()=>b(c,i.First,{skipElements:[o.relatedTarget]}),[M.Backwards]:()=>b(c,i.Last,{skipElements:[o.relatedTarget]})})})}),B=q(),L=T(!1),P={ref:a,onKeyDown(o){o.key=="Tab"&&(L.current=!0,B.requestAnimationFrame(()=>{L.current=!1}))},onBlur(o){let c=new Set(t==null?void 0:t.current);c.add(l);let p=o.relatedTarget;!p||p.dataset.headlessuiFocusGuard!=="true"&&(h(c,p)||(L.current?b(l.current,g(H.current,{[M.Forwards]:()=>i.Next,[M.Backwards]:()=>i.Previous})|i.WrapAround,{relativeTo:o.target}):o.target instanceof HTMLElement&&f(o.target)))}};return d.createElement(d.Fragment,null,Boolean(n&4)&&d.createElement(v,{as:"button",type:"button","data-headlessui-focus-guard":!0,onFocus:R,features:F.Focusable}),I({ourProps:P,theirProps:E,defaultTag:z,name:"FocusTrap"}),Boolean(n&4)&&d.createElement(v,{as:"button",type:"button","data-headlessui-focus-guard":!0,onFocus:R,features:F.Focusable}))}),{features:A});function J({ownerDocument:r},u){let e=T(null);k(r==null?void 0:r.defaultView,"focusout",a=>{!u||e.current||(e.current=a.target)},!0),j(()=>{u||((r==null?void 0:r.activeElement)===(r==null?void 0:r.body)&&f(e.current),e.current=null)},[u]);let l=T(!1);U(()=>(l.current=!1,()=>{l.current=!0,y(()=>{!l.current||(f(e.current),e.current=null)})}),[])}function Q({ownerDocument:r,container:u,initialFocus:e},l){let a=T(null),m=O();return j(()=>{if(!l)return;let t=u.current;!t||y(()=>{if(!m.current)return;let n=r==null?void 0:r.activeElement;if(e!=null&&e.current){if((e==null?void 0:e.current)===n){a.current=n;return}}else if(t.contains(n)){a.current=n;return}e!=null&&e.current?f(e.current):b(t,i.First)===G.Error&&console.warn("There are no focusable elements inside the "),a.current=r==null?void 0:r.activeElement})},[l]),a}function X({ownerDocument:r,container:u,containers:e,previousActiveElement:l},a){let m=O();k(r==null?void 0:r.defaultView,"focus",t=>{if(!a||!m.current)return;let n=new Set(e==null?void 0:e.current);n.add(u);let E=l.current;if(!E)return;let s=t.target;s&&s instanceof HTMLElement?h(n,s)?(l.current=s,f(s)):(t.preventDefault(),t.stopPropagation(),f(E)):f(l.current)},!0)}function h(r,u){var e;for(let l of r)if((e=l.current)!=null&&e.contains(u))return!0;return!1}export{de as FocusTrap};