import{d as a,e as d,f as m,g as p}from"./chunk-7YTVARFL.js";import{e as c,f as u,la as l}from"./chunk-ISMRT7VU.js";var f=["Shift","Enter","CapsLock","NumLock","F7","F8","]","ArrowDown","MediaTrackNext","Escape","Control"],g=(()=>{class o{constructor(){this.barcodeValue=new c,this.barcodeValue$=this.barcodeValue.asObservable(),this.mark=new u(null),this.isProcessingBarcode=!1,this._firstKeyDownTime=null,this._pressedKeys=[]}initSubs(){this._listenToKeyboard(),this.barcodeValue$.subscribe(e=>{this._applyBarcodeLogic(e)})}testStringAgainstRegexes(e){let t=[],s=d;for(let[n,r]of Object.entries(s)){let i=e.match(r);i&&t.push({regexName:n,match:i,groups:i.groups||{}})}return t.find(n=>n!==null)}_listenToKeyboard(){document.addEventListener("keydown",e=>this._handleKeyDown(e))}_handleKeyDown(e){if((this._firstKeyDownTime===null||performance.now()-this._firstKeyDownTime>700)&&(this._firstKeyDownTime=performance.now(),this._pressedKeys=[]),e.key==="Enter"){performance.now()-this._firstKeyDownTime<700&&this._pressedKeys.length>6&&this.barcodeValue.next(this._pressedKeys.join("")),this._resetScannerState();return}e.key&&!f.includes(e.key)&&this._pressedKeys.push(e.key)}_resetScannerState(){this._firstKeyDownTime=null,this._pressedKeys=[]}_applyBarcodeLogic(e){if(this.isProcessingBarcode)return;this.isProcessingBarcode=!0,e=m(e),e=p(e);let t=document.activeElement;t instanceof HTMLInputElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement?this._isFocused(e):this._isNotFocused(e),this.isProcessingBarcode=!1}_isNotFocused(e){let t=Array.from(document.querySelectorAll("[data-barcode]")).sort((s,n)=>{let r=parseInt(s.getAttribute("data-barcode")||"0",10),i=parseInt(n.getAttribute("data-barcode")||"0",10);return r-i})[0];if(t){let s=t.querySelector("input");if(s){let n=document.activeElement===t||document.activeElement===s,r=this.testStringAgainstRegexes(e)?.groups?.productCode,i=r?a(r):null,h=i&&t.getAttribute("token")!=="matrix"?i:e;this.mark.next(r?e:null),s.focus(),setTimeout(()=>{s.value=h,s.dispatchEvent(new Event("input")),n||(t.getAttribute("token")==="matrix"?setTimeout(()=>{s.dispatchEvent(new KeyboardEvent("keydown",{key:"Enter",code:"Enter",bubbles:!0,cancelable:!0}))},150):s.dispatchEvent(new KeyboardEvent("keydown",{key:"Enter",code:"Enter",bubbles:!0,cancelable:!0})))},150)}}}_isFocused(e){let t=document.activeElement,s=document.querySelector('input[data-token="matrix"]');if(t){if(s)return;let n=this.testStringAgainstRegexes(e)?.groups?.productCode,r=n?a(n):null;if(!r)return;let i=r;this.mark.next(n?e:null),setTimeout(()=>{t.value=i,t.dispatchEvent(new Event("input")),t.dispatchEvent(new KeyboardEvent("keydown",{key:"Enter",code:"Enter",bubbles:!0,cancelable:!0}))},150)}}static{this.\u0275fac=function(t){return new(t||o)}}static{this.\u0275prov=l({token:o,factory:o.\u0275fac,providedIn:"root"})}}return o})();export{g as a};
//# sourceMappingURL=chunk-NMEJ7FQX.js.map
