import{__awaiter,__generator}from"tslib";import{b as balBrowser}from"./browser-B1u-Mq75.js";import{o as addEventListener,p as removeEventListener}from"./helpers-BNj87EbG.js";var focusableQueryString='[tabindex]:not([tabindex^="-"]):not([hidden]):not([disabled]), input:not([type=hidden]):not([tabindex^="-"]):not([hidden]):not([disabled]), textarea:not([tabindex^="-"]):not([hidden]):not([disabled]), button:not([tabindex^="-"]):not([hidden]):not([disabled]), select:not([tabindex^="-"]):not([hidden]):not([disabled]), .bal-focusable:not([tabindex^="-"]):not([hidden]):not([disabled]), .bal-focusable[disabled="false"]:not([tabindex^="-"]):not([hidden])';var focusFirstDescendant=function(e,n){var t=e.querySelector(focusableQueryString);focusElementInContext(t,n!==null&&n!==void 0?n:e)};var focusLastDescendant=function(e,n){var t=Array.from(e.querySelectorAll(focusableQueryString));var r=t.length>0?t[t.length-1]:null;focusElementInContext(r,n!==null&&n!==void 0?n:e)};var focusElementInContext=function(e,n){var t=e;var r=e===null||e===void 0?void 0:e.shadowRoot;if(r){t=r.querySelector(focusableQueryString)||e}if(t){focusVisibleElement(t)}else{n.focus({preventScroll:true})}};var focusVisibleElement=function(e){e.focus({preventScroll:true});if(e.classList.contains("bal-focusable")){var n=e.closest("bal-app");if(n){n.setFocus([e])}}};var lastId=0;var BACKDROP="backdrop";var prepareOverlay=function(e){if(balBrowser.hasDocument){connectListeners(document)}var n=lastId++;e.overlayIndex=n;if(!e.el.hasAttribute("id")){e.el.id="bal-overlay-".concat(n)}};var getOverlays=function(e,n){if(n===undefined){n="bal-modal,bal-snackbar,bal-toast"}return Array.from(e.querySelectorAll(n)).filter((function(e){return e.overlayIndex>0}))};var getOverlay=function(e,n,t){var r=getOverlays(e,n);return t===undefined?r[r.length-1]:r.find((function(e){return e.id===t}))};var connectListeners=function(e){if(lastId===0){lastId=1;e.addEventListener("focus",(function(n){trapKeyboardFocus(n,e)}),true);e.addEventListener("keyup",(function(n){if(n.key==="Escape"){var t=getOverlay(e);if(t){if(t.isClosable===false){return}t.dismiss(undefined,BACKDROP)}}}))}};var getPresentedOverlay=function(e,n,t){var r=getPresentedOverlays(e,n);return r[r.length-1]};var getPresentedOverlays=function(e,n){return getOverlays(e,n).filter((function(e){return!isOverlayHidden(e)}))};var isOverlayHidden=function(e){return e.classList.contains("is-hidden")};var trapKeyboardFocus=function(e,n){var t=getPresentedOverlay(n,"bal-modal");var r=e.target;if(!t||!r){return}var a=function(){if(t===r){t.lastFocus=undefined}else{var e=t;var a=e.querySelector(".bal-modal__container");if(!a){return}if(a.contains(r)||r===e.querySelector("bal-modal__background")){t.lastFocus=r}else{var i=t.lastFocus;focusFirstDescendant(a,t);if(i===n.activeElement){focusLastDescendant(a,t)}t.lastFocus=n.activeElement}}};var i=function(){if(t.contains(r)){t.lastFocus=r}else if(r.tagName==="BAL-TOAST"){focusElementInOverlay(t.lastFocus,t)}else{var e=t.lastFocus;focusFirstDescendant(t);if(e===n.activeElement){focusLastDescendant(t)}t.lastFocus=n.activeElement}};if(t.shadowRoot){i()}else{a()}};var dismiss=function(e,n,t,r){return __awaiter(void 0,void 0,void 0,(function(){var a;return __generator(this,(function(i){switch(i.label){case 0:if(!e.presented){return[2,false]}i.label=1;case 1:i.trys.push([1,3,,4]);e.el.style.setProperty("pointer-events","none");e.willDismiss.emit({data:n,role:t});return[4,r()];case 2:i.sent();e.presented=false;e.didDismiss.emit({data:n,role:t});return[3,4];case 3:a=i.sent();e.presented=false;console.error(a);return[3,4];case 4:e.el.remove();return[2,true]}}))}))};var eventMethod=function(e,n){var t;var r=new Promise((function(e){return t=e}));onceEvent(e,n,(function(e){t(e.detail)}));return r};var onceEvent=function(e,n,t){var r=function(a){removeEventListener(e,n,r);t(a)};addEventListener(e,n,r)};var focusElementInOverlay=function(e,n){var t=e;var r=e===null||e===void 0?void 0:e.shadowRoot;if(r){t=r.querySelector(focusableQueryString)||e}if(t){focusVisibleElement(t)}else{n.focus()}};var FOCUS_TRAP_DISABLE_CLASS="bal-disable-focus-trap";export{FOCUS_TRAP_DISABLE_CLASS as F,getOverlay as a,dismiss as d,eventMethod as e,getOverlays as g,prepareOverlay as p};