import{__awaiter,__generator}from"tslib";import{b as balBrowser}from"./browser-a623d3a0.js";import{j as addEventListener,r as removeEventListener}from"./helpers-2253b115.js";var lastId=0;var BACKDROP="backdrop";var prepareOverlay=function(e){if(balBrowser.hasDocument){connectListeners(document)}var r=lastId++;e.overlayIndex=r;if(!e.el.hasAttribute("id")){e.el.id="bal-overlay-".concat(r)}};var getOverlays=function(e,r){if(r===undefined){r="bal-modal,bal-snackbar,bal-toast"}return Array.from(e.querySelectorAll(r)).filter((function(e){return e.overlayIndex>0}))};var getOverlay=function(e,r,n){var a=getOverlays(e,r);return n===undefined?a[a.length-1]:a.find((function(e){return e.id===n}))};var connectListeners=function(e){if(lastId===0){lastId=1;e.addEventListener("keyup",(function(r){if(r.key==="Escape"){var n=getOverlay(e);if(n){n.dismiss(undefined,BACKDROP)}}}))}};var dismiss=function(e,r,n,a){return __awaiter(void 0,void 0,void 0,(function(){var t;return __generator(this,(function(s){switch(s.label){case 0:if(!e.presented){return[2,false]}s.label=1;case 1:s.trys.push([1,3,,4]);e.el.style.setProperty("pointer-events","none");e.willDismiss.emit({data:r,role:n});return[4,a()];case 2:s.sent();e.presented=false;e.didDismiss.emit({data:r,role:n});return[3,4];case 3:t=s.sent();e.presented=false;console.error(t);return[3,4];case 4:e.el.remove();return[2,true]}}))}))};var eventMethod=function(e,r){var n;var a=new Promise((function(e){return n=e}));onceEvent(e,r,(function(e){n(e.detail)}));return a};var onceEvent=function(e,r,n){var a=function(t){removeEventListener(e,r,a);n(t)};addEventListener(e,r,a)};export{getOverlay as a,dismiss as d,eventMethod as e,getOverlays as g,prepareOverlay as p};