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