import{__awaiter,__extends,__generator}from"tslib";import{L as ListenerAbstract,a as SingleSubject}from"./listener-CClDIhvK.js";import{b as balBrowser}from"./browser-B1u-Mq75.js";import{b as waitAfterIdleCallback,i as isDescendant,o as addEventListener,p as removeEventListener}from"./helpers-BNj87EbG.js";var BalFocusListener=function(t){__extends(i,t);function i(){var i=t.apply(this,arguments)||this;i.onFocusin=function(t){i.notify({ev:t,isFocusIn:true,isFocusOut:false})};i.onFocusout=function(t){return __awaiter(i,void 0,void 0,(function(){var i;return __generator(this,(function(s){switch(s.label){case 0:return[4,waitAfterIdleCallback()];case 1:s.sent();if(balBrowser.hasDocument&&this.el){i=document.activeElement;if(i&&!isDescendant(this.el,i)){this.notify({ev:t,isFocusIn:false,isFocusOut:true})}}return[2]}}))}))};return i}i.prototype.connect=function(i){t.prototype.connect.call(this,i);if(this.el){addEventListener(this.el,"focusin",this.onFocusin);addEventListener(this.el,"focusout",this.onFocusout)}};i.prototype.disconnect=function(){t.prototype.disconnect.call(this);if(this.el){removeEventListener(this.el,"focusin",this.onFocusin);removeEventListener(this.el,"focusout",this.onFocusout)}};return i}(ListenerAbstract);var BalFocusSubject=function(t){__extends(i,t);function i(){var i=t.call(this,(function(t,i){if(i){if(i.isFocusIn){if(t.hasFocus===false){t.focusInListener(i.ev)}t.hasFocus=true}if(i.isFocusOut){t.focusOutListener(i.ev);t.hasFocus=false}}}))||this;i.listener=new BalFocusListener;return i}i.prototype.attach=function(i){var s=this;t.prototype.attach.call(this,i);this.listener.connect(i.el);this.listener.add((function(i){return t.prototype.notify.call(s,i)}))};i.prototype.detach=function(){t.prototype.detach.call(this);this.listener.disconnect()};return i}(SingleSubject);function ListenToFocus(){return function(t,i,s){var e=t.connectedCallback,n=t.disconnectedCallback;t.connectedCallback=function(){if(!this._balFocusSubject){this._balFocusSubject=new BalFocusSubject;this._balFocusSubject.attach(this)}return e&&e.call(this)};t.disconnectedCallback=function(){if(this._balFocusSubject){this._balFocusSubject.detach();this._balFocusSubject=undefined}return n&&n.call(this)}}}export{ListenToFocus as L};