"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./disposables.js"),t=require("./dom.js"),o=require("./match.js"),r=require("./owner.js");let n=["[contentEditable=true]","[tabindex]","a[href]","area[href]","button:not([disabled])","iframe","input:not([disabled])","select:not([disabled])","details>summary","textarea:not([disabled])"].map((e=>`${e}:not([tabindex='-1'])`)).join(","),u=["[data-autofocus]"].map((e=>`${e}:not([tabindex='-1'])`)).join(",");var l,s,i,a=((i=a||{})[i.First=1]="First",i[i.Previous=2]="Previous",i[i.Next=4]="Next",i[i.Last=8]="Last",i[i.WrapAround=16]="WrapAround",i[i.NoScroll=32]="NoScroll",i[i.AutoFocus=64]="AutoFocus",i),d=((s=d||{})[s.Error=0]="Error",s[s.Overflow=1]="Overflow",s[s.Success=2]="Success",s[s.Underflow=3]="Underflow",s),c=((l=c||{})[l.Previous=-1]="Previous",l[l.Next=1]="Next",l);function m(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:document.body;return null==e?[]:Array.from(e.querySelectorAll(n)).sort(((e,t)=>Math.sign((e.tabIndex||Number.MAX_SAFE_INTEGER)-(t.tabIndex||Number.MAX_SAFE_INTEGER))))}function f(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:document.body;return null==e?[]:Array.from(e.querySelectorAll(u)).sort(((e,t)=>Math.sign((e.tabIndex||Number.MAX_SAFE_INTEGER)-(t.tabIndex||Number.MAX_SAFE_INTEGER))))}var E=(e=>(e[e.Strict=0]="Strict",e[e.Loose=1]="Loose",e))(E||{});function v(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;var u;return e!==(null==(u=r.getOwnerDocument(e))?void 0:u.body)&&o.match(t,{0:()=>e.matches(n),1(){let t=e;for(;null!==t;){if(t.matches(n))return!0;t=t.parentElement}return!1}})}var b=(e=>(e[e.Keyboard=0]="Keyboard",e[e.Mouse=1]="Mouse",e))(b||{});function x(e){null==e||e.focus({preventScroll:!0})}"undefined"!=typeof window&&"undefined"!=typeof document&&(document.addEventListener("keydown",(e=>{e.metaKey||e.altKey||e.ctrlKey||(document.documentElement.dataset.headlessuiFocusVisible="")}),!0),document.addEventListener("click",(e=>{1===e.detail?delete document.documentElement.dataset.headlessuiFocusVisible:0===e.detail&&(document.documentElement.dataset.headlessuiFocusVisible="")}),!0));let F=["textarea","input"].join(",");function p(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:e=>e;return e.slice().sort(((e,o)=>{let r=t(e),n=t(o);if(null===r||null===n)return 0;let u=r.compareDocumentPosition(n);return u&Node.DOCUMENT_POSITION_FOLLOWING?-1:u&Node.DOCUMENT_POSITION_PRECEDING?1:0}))}function N(e,t){let{sorted:o=!0,relativeTo:n=null,skipElements:u=[]}=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},l=Array.isArray(e)?e.length>0?r.getRootNode(e[0]):document:r.getRootNode(e),s=Array.isArray(e)?o?p(e):e:64&t?f(e):m(e);u.length>0&&s.length>1&&(s=s.filter((e=>!u.some((t=>null!=t&&"current"in t?(null==t?void 0:t.current)===e:t===e))))),n=null!=n?n:null==l?void 0:l.activeElement;let i,a=(()=>{if(5&t)return 1;if(10&t)return-1;throw new Error("Missing Focus.First, Focus.Previous, Focus.Next or Focus.Last")})(),d=(()=>{if(1&t)return 0;if(2&t)return Math.max(0,s.indexOf(n))-1;if(4&t)return Math.max(0,s.indexOf(n))+1;if(8&t)return s.length-1;throw new Error("Missing Focus.First, Focus.Previous, Focus.Next or Focus.Last")})(),c=32&t?{preventScroll:!0}:{},E=0,v=s.length;do{if(E>=v||E+v<=0)return 0;let e=d+E;if(16&t)e=(e+v)%v;else{if(e<0)return 3;if(e>=v)return 1}i=s[e],null==i||i.focus(c),E+=a}while(i!==r.getActiveElement(i));return 6&t&&function(e){var t,o;return null!=(o=null==(t=null==e?void 0:e.matches)?void 0:t.call(e,F))&&o}(i)&&i.select(),2}exports.Focus=a,exports.FocusResult=d,exports.FocusableMode=E,exports.focusElement=x,exports.focusFrom=function(e,t){return N(m(arguments.length>2&&void 0!==arguments[2]?arguments[2]:null===e?document.body:r.getRootNode(e)),t,{relativeTo:e})},exports.focusIn=N,exports.focusableSelector=n,exports.getAutoFocusableElements=f,exports.getFocusableElements=m,exports.isFocusableElement=v,exports.restoreFocusIfNecessary=function(o){e.disposables().nextFrame((()=>{let e=r.getActiveElement(o);e&&t.isHTMLorSVGElement(e)&&!v(e,0)&&x(o)}))},exports.sortByDomNode=p;
//# sourceMappingURL=focus-management.js.map
