"use client";import F,{useRef as M}from"react";import{useDisposables as W}from'../../hooks/use-disposables.js';import{useEvent as O}from'../../hooks/use-event.js';import{useEventListener as K}from'../../hooks/use-event-listener.js';import{useIsMounted as P}from'../../hooks/use-is-mounted.js';import{useIsTopLayer as C}from'../../hooks/use-is-top-layer.js';import{useOnUnmount as q}from'../../hooks/use-on-unmount.js';import{useOwnerDocument as J}from'../../hooks/use-owner.js';import{useServerHandoffComplete as X}from'../../hooks/use-server-handoff-complete.js';import{useSyncRefs as z}from'../../hooks/use-sync-refs.js';import{Direction as y,useTabDirection as D}from'../../hooks/use-tab-direction.js';import{useWatch as R}from'../../hooks/use-watch.js';import{Hidden as _,HiddenFeatures as S}from'../../internal/hidden.js';import{history as H}from'../../utils/active-element-history.js';import*as i from'../../utils/dom.js';import{Focus as m,FocusResult as h,focusElement as p,focusIn as d}from'../../utils/focus-management.js';import{match as j}from'../../utils/match.js';import{microTask as U}from'../../utils/micro-task.js';import{isActiveElement as Q}from'../../utils/owner.js';import{forwardRefWithAs as Y,useRender as Z}from'../../utils/render.js';function x(o){if(!o)return new Set;if(typeof o=="function")return new Set(o());let t=new Set;for(let e of o.current)i.isElement(e.current)&&t.add(e.current);return t}let $="div";var G=(n=>(n[n.None=0]="None",n[n.InitialFocus=1]="InitialFocus",n[n.TabLock=2]="TabLock",n[n.FocusLock=4]="FocusLock",n[n.RestoreFocus=8]="RestoreFocus",n[n.AutoFocus=16]="AutoFocus",n))(G||{});function w(o,t){let e=M(null),r=z(e,t),{initialFocus:u,initialFocusFallback:a,containers:n,features:s=15,...f}=o;X()||(s=0);let l=J(e.current);re(s,{ownerDocument:l});let T=ne(s,{ownerDocument:l,container:e,initialFocus:u,initialFocusFallback:a});oe(s,{ownerDocument:l,container:e,containers:n,previousActiveElement:T});let g=D(),A=O(c=>{if(!i.isHTMLElement(e.current))return;let E=e.current;(V=>V())(()=>{j(g.current,{[y.Forwards]:()=>{d(E,m.First,{skipElements:[c.relatedTarget,a]})},[y.Backwards]:()=>{d(E,m.Last,{skipElements:[c.relatedTarget,a]})}})})}),v=C(!!(s&2),"focus-trap#tab-lock"),N=W(),b=M(!1),k={ref:r,onKeyDown(c){c.key=="Tab"&&(b.current=!0,N.requestAnimationFrame(()=>{b.current=!1}))},onBlur(c){if(!(s&4))return;let E=x(n);i.isHTMLElement(e.current)&&E.add(e.current);let L=c.relatedTarget;i.isHTMLorSVGElement(L)&&L.dataset.headlessuiFocusGuard!=="true"&&(I(E,L)||(b.current?d(e.current,j(g.current,{[y.Forwards]:()=>m.Next,[y.Backwards]:()=>m.Previous})|m.WrapAround,{relativeTo:c.target}):i.isHTMLorSVGElement(c.target)&&p(c.target)))}},B=Z();return F.createElement(F.Fragment,null,v&&F.createElement(_,{as:"button",type:"button","data-headlessui-focus-guard":!0,onFocus:A,features:S.Focusable}),B({ourProps:k,theirProps:f,defaultTag:$,name:"FocusTrap"}),v&&F.createElement(_,{as:"button",type:"button","data-headlessui-focus-guard":!0,onFocus:A,features:S.Focusable}))}let ee=Y(w),ge=Object.assign(ee,{features:G});function te(o=!0){let t=M(H.slice());return R(([e],[r])=>{r===!0&&e===!1&&U(()=>{t.current.splice(0)}),r===!1&&e===!0&&(t.current=H.slice())},[o,H,t]),O(()=>{var e;return(e=t.current.find(r=>r!=null&&r.isConnected))!=null?e:null})}function re(o,{ownerDocument:t}){let e=!!(o&8),r=te(e);R(()=>{e||Q(t==null?void 0:t.body)&&p(r())},[e]),q(()=>{e&&p(r())})}function ne(o,{ownerDocument:t,container:e,initialFocus:r,initialFocusFallback:u}){let a=M(null),n=C(!!(o&1),"focus-trap#initial-focus"),s=P();return R(()=>{if(o===0)return;if(!n){u!=null&&u.current&&p(u.current);return}let f=e.current;f&&U(()=>{if(!s.current)return;let l=t==null?void 0:t.activeElement;if(r!=null&&r.current){if((r==null?void 0:r.current)===l){a.current=l;return}}else if(f.contains(l)){a.current=l;return}if(r!=null&&r.current)p(r.current);else{if(o&16){if(d(f,m.First|m.AutoFocus)!==h.Error)return}else if(d(f,m.First)!==h.Error)return;if(u!=null&&u.current&&(p(u.current),(t==null?void 0:t.activeElement)===u.current))return;console.warn("There are no focusable elements inside the <FocusTrap />")}a.current=t==null?void 0:t.activeElement})},[u,n,o]),a}function oe(o,{ownerDocument:t,container:e,containers:r,previousActiveElement:u}){let a=P(),n=!!(o&4);K(t==null?void 0:t.defaultView,"focus",s=>{if(!n||!a.current)return;let f=x(r);i.isHTMLElement(e.current)&&f.add(e.current);let l=u.current;if(!l)return;let T=s.target;i.isHTMLElement(T)?I(f,T)?(u.current=T,p(T)):(s.preventDefault(),s.stopPropagation(),p(l)):p(u.current)},!0)}function I(o,t){for(let e of o)if(e.contains(t))return!0;return!1}export{ge as FocusTrap,G as FocusTrapFeatures};
