import*as e from"react";import{useComposedRefs as t}from"../../react-compose-refs/dist/index.js";import{Primitive as n}from"../../react-primitive/dist/index.js";import{useCallbackRef as o}from"../../react-use-callback-ref/dist/index.js";import{jsx as r}from"react/jsx-runtime";var u="focusScope.autoFocusOnMount",c="focusScope.autoFocusOnUnmount",s={bubbles:!1,cancelable:!0},i=e.forwardRef(((i,f)=>{const{loop:p=!1,trapped:v=!1,onMountAutoFocus:E,onUnmountAutoFocus:b,...y}=i,[L,T]=e.useState(null),h=o(E),N=o(b),F=e.useRef(null),S=t(f,(e=>T(e))),x=e.useRef({paused:!1,pause(){this.paused=!0},resume(){this.paused=!1}}).current;e.useEffect((()=>{if(v){let e=function(e){if(x.paused||!L)return;const t=e.target;L.contains(t)?F.current=t:l(F.current,{select:!0})},t=function(e){if(x.paused||!L)return;const t=e.relatedTarget;null!==t&&(L.contains(t)||l(F.current,{select:!0}))},n=function(e){if(document.activeElement===document.body)for(const t of e)t.removedNodes.length>0&&l(L)};document.addEventListener("focusin",e),document.addEventListener("focusout",t);const o=new MutationObserver(n);return L&&o.observe(L,{childList:!0,subtree:!0}),()=>{document.removeEventListener("focusin",e),document.removeEventListener("focusout",t),o.disconnect()}}}),[v,L,x.paused]),e.useEffect((()=>{if(L){m.add(x);const t=document.activeElement;if(!L.contains(t)){const n=new CustomEvent(u,s);L.addEventListener(u,h),L.dispatchEvent(n),n.defaultPrevented||(!function(e,{select:t=!1}={}){const n=document.activeElement;for(const o of e)if(l(o,{select:t}),document.activeElement!==n)return}((e=d(L),e.filter((e=>"A"!==e.tagName))),{select:!0}),document.activeElement===t&&l(L))}return()=>{L.removeEventListener(u,h),setTimeout((()=>{const e=new CustomEvent(c,s);L.addEventListener(c,N),L.dispatchEvent(e),e.defaultPrevented||l(t??document.body,{select:!0}),L.removeEventListener(c,N),m.remove(x)}),0)}}var e}),[L,h,N,x]);const I=e.useCallback((e=>{if(!p&&!v)return;if(x.paused)return;const t="Tab"===e.key&&!e.altKey&&!e.ctrlKey&&!e.metaKey,n=document.activeElement;if(t&&n){const t=e.currentTarget,[o,r]=function(e){const t=d(e),n=a(t,e),o=a(t.reverse(),e);return[n,o]}(t);o&&r?e.shiftKey||n!==r?e.shiftKey&&n===o&&(e.preventDefault(),p&&l(r,{select:!0})):(e.preventDefault(),p&&l(o,{select:!0})):n===t&&e.preventDefault()}}),[p,v,x.paused]);return r(n.div,{tabIndex:-1,...y,ref:S,onKeyDown:I})}));function d(e){const t=[],n=document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT,{acceptNode:e=>{const t="INPUT"===e.tagName&&"hidden"===e.type;return e.disabled||e.hidden||t?NodeFilter.FILTER_SKIP:e.tabIndex>=0?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP}});for(;n.nextNode();)t.push(n.currentNode);return t}function a(e,t){for(const n of e)if(!f(n,{upTo:t}))return n}function f(e,{upTo:t}){if("hidden"===getComputedStyle(e).visibility)return!0;for(;e;){if(void 0!==t&&e===t)return!1;if("none"===getComputedStyle(e).display)return!0;e=e.parentElement}return!1}function l(e,{select:t=!1}={}){if(e&&e.focus){const n=document.activeElement;e.focus({preventScroll:!0}),e!==n&&function(e){return e instanceof HTMLInputElement&&"select"in e}(e)&&t&&e.select()}}i.displayName="FocusScope";var m=function(){let e=[];return{add(t){const n=e[0];t!==n&&n?.pause(),e=p(e,t),e.unshift(t)},remove(t){e=p(e,t),e[0]?.resume()}}}();function p(e,t){const n=[...e],o=n.indexOf(t);return-1!==o&&n.splice(o,1),n}export{i as FocusScope};
//# sourceMappingURL=index.js.map
