import{b as n}from"./p-B1u-Mq75.js";import{o as t,p as o}from"./p-Ba5qjFjW.js";const e='[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])',a=(n,t)=>{const o=n.querySelector(e);l(o,null!=t?t:n)},d=(n,t)=>{const o=Array.from(n.querySelectorAll(e));l(o.length>0?o[o.length-1]:null,null!=t?t:n)},l=(n,t)=>{let o=n;const a=null==n?void 0:n.shadowRoot;a&&(o=a.querySelector(e)||n),o?s(o):t.focus({preventScroll:!0})},s=n=>{if(n.focus({preventScroll:!0}),n.classList.contains("bal-focusable")){const t=n.closest("bal-app");t&&t.setFocus([n])}};let i=0;const r=t=>{n.hasDocument&&u(document);const o=i++;t.overlayIndex=o,t.el.hasAttribute("id")||(t.el.id=`bal-overlay-${o}`)},b=(n,t)=>(void 0===t&&(t="bal-modal,bal-snackbar,bal-toast"),Array.from(n.querySelectorAll(t)).filter((n=>n.overlayIndex>0))),c=(n,t,o)=>{const e=b(n,t);return void 0===o?e[e.length-1]:e.find((n=>n.id===o))},u=n=>{0===i&&(i=1,n.addEventListener("focus",(t=>{p(t,n)}),!0),n.addEventListener("keyup",(t=>{if("Escape"===t.key){const t=c(n);if(t){if(!1===t.isClosable)return;t.dismiss(void 0,"backdrop")}}})))},f=n=>n.classList.contains("is-hidden"),p=(n,t)=>{const o=(n=>{const t=((n,t)=>b(n,t).filter((n=>!f(n))))(n,"bal-modal");return t[t.length-1]})(t),e=n.target;o&&e&&(o.shadowRoot?(()=>{if(o.contains(e))o.lastFocus=e;else if("BAL-TOAST"===e.tagName)x(o.lastFocus,o);else{const n=o.lastFocus;a(o),n===t.activeElement&&d(o),o.lastFocus=t.activeElement}})():(()=>{if(o===e)o.lastFocus=void 0;else{const n=o,l=n.querySelector(".bal-modal__container");if(!l)return;if(l.contains(e)||e===n.querySelector("bal-modal__background"))o.lastFocus=e;else{const n=o.lastFocus;a(l,o),n===t.activeElement&&d(l,o),o.lastFocus=t.activeElement}}})())},h=async(n,t,o,e)=>{if(!n.presented)return!1;try{n.el.style.setProperty("pointer-events","none"),n.willDismiss.emit({data:t,role:o}),await e(),n.presented=!1,n.didDismiss.emit({data:t,role:o})}catch(t){n.presented=!1,console.error(t)}return n.el.remove(),!0},m=(n,t)=>{let o;const e=new Promise((n=>o=n));return v(n,t,(n=>{o(n.detail)})),e},v=(n,e,a)=>{const d=t=>{o(n,e,d),a(t)};t(n,e,d)},x=(n,t)=>{let o=n;const a=null==n?void 0:n.shadowRoot;a&&(o=a.querySelector(e)||n),o?s(o):t.focus()},y="bal-disable-focus-trap";export{y as F,c as a,h as d,m as e,b as g,r as p}