import {h}from'./chunk-DVWSNT7T.js';import {debounce}from'@scandit/web-datacapture-core/build/js/private/utils/debounce.js';var s=()=>{},d=r=>r instanceof HTMLInputElement||r instanceof HTMLTextAreaElement||r.tagName.toLowerCase()===h.tag,n=class extends CustomEvent{constructor(l,e=0,i=0){super("keyboard-visibility-changed",{detail:{isKeyboardVisible:l,keyboardHeight:e,keyboardTop:i},bubbles:true,composed:true});}},t=class t extends EventTarget{constructor(e){super();this._isKeyboardVisible=false;this._keyboardHeight=0;this._keyboardTop=0;this._isConnected=false;this._refCount=0;this._initialViewportHeight=0;this._handleFocusIn=this._onFocusIn.bind(this);this._handleFocusOut=this._onFocusOut.bind(this);this._handleVisualViewportResize=debounce(this._onVisualViewportResize.bind(this),t.VIEWPORT_RESIZE_DEBOUNCE);this._globals=e;}static getInstance(){var e,i;return t._instance||(t._instance=new t({document:window.document,window:window.window,visualViewport:(i=(e=window.window)==null?void 0:e.visualViewport)!=null?i:null})),t._instance}static create(e){return new t(e)}connect(){if(this._refCount++,this._isConnected){return}this._isConnected=true,this._refCount===1&&this._globals.visualViewport&&(this._initialViewportHeight=Math.max(this._globals.window.innerHeight,this._globals.visualViewport.height),s()),this._setupListeners();}disconnect(){this._refCount--,this._refCount<=0&&(this._refCount=0,this._isConnected=false,this._removeListeners());}_setupListeners(){this._globals.document.addEventListener("focusin",this._handleFocusIn,true),this._globals.document.addEventListener("focusout",this._handleFocusOut,true),this._globals.visualViewport?(this._globals.visualViewport.addEventListener("resize",this._handleVisualViewportResize),this._globals.visualViewport.addEventListener("scroll",this._handleVisualViewportResize),s()):s();}_removeListeners(){this._globals.document.removeEventListener("focusin",this._handleFocusIn,true),this._globals.document.removeEventListener("focusout",this._handleFocusOut,true),this._globals.visualViewport&&(this._globals.visualViewport.removeEventListener("resize",this._handleVisualViewportResize),this._globals.visualViewport.removeEventListener("scroll",this._handleVisualViewportResize));}_calculateKeyboardHeight(){if(!this._globals.visualViewport)return 0;let e=this._initialViewportHeight>0?this._initialViewportHeight:this._globals.window.innerHeight,i=Math.floor(e-this._globals.visualViewport.height);return i>=t.KEYBOARD_HEIGHT_THRESHOLD?i:0}_calculateKeyboardTop(){return this._globals.visualViewport?Math.floor(this._globals.visualViewport.offsetTop+this._globals.visualViewport.height):0}_onVisualViewportResize(){if(!this._globals.visualViewport)return;let e=this._calculateKeyboardHeight(),i=this._calculateKeyboardTop(),o=e>0,h=this._isKeyboardVisible!==o;h?(this._isKeyboardVisible=o,this._keyboardHeight=e,this._keyboardTop=i,this.dispatchEvent(new n(o,e,i))):o&&(this._keyboardHeight=e,this._keyboardTop=i);}_onFocusIn(e){let i=e.target;d(i)&&(this._handleVisualViewportResize());}_onFocusOut(e){let i=e.target;d(i)&&(this._handleVisualViewportResize());}isKeyboardVisible(){return this._isKeyboardVisible}getKeyboardHeight(){return this._keyboardHeight}getKeyboardTop(){return this._keyboardTop}addEventListener(e,i,o){super.addEventListener(e,i,o);}removeEventListener(e,i,o){super.removeEventListener(e,i,o);}reset(e=false){this._refCount=0,this._initialViewportHeight=0,this._isConnected&&(this._isConnected=false,this._removeListeners()),e&&(t._instance=null);}};t._instance=null,t.KEYBOARD_HEIGHT_THRESHOLD=150,t.VIEWPORT_RESIZE_DEBOUNCE=150;var b=t;export{n as a,b};