import{d as l,e as f,f as y}from"./chunk-JV3IW4ED.js";import{Ga as h,ea as p,g as d,h as m}from"./chunk-DYUFV4HF.js";var E=["Shift","Enter","CapsLock","NumLock","F7"];var u=class{constructor(){this.barcodeValue=new d,this.barcodeValue$=this.barcodeValue.asObservable(),this.mark=new m(null),this.isProcessingBarcode=!1,this.firstKeyDownTime=null,this.pressedKeys=[]}initSubs(){this._listenToKeyboard(),this.barcodeValue$.subscribe(e=>{this._applyBarcodeLogic(e)})}testStringAgainstRegexes(e){let o=[],n=f;for(let[t,s]of Object.entries(n)){let r=e.match(s);r&&o.push({regexName:t,match:r,groups:r.groups||{}})}return o.find(t=>t!==null)}_listenToKeyboard(){document.addEventListener("keydown",e=>this._handleKeyDown(e))}_handleKeyDown(e){if((this.firstKeyDownTime===null||performance.now()-this.firstKeyDownTime>500)&&(this.firstKeyDownTime=performance.now(),this.pressedKeys=[]),e.key==="Enter"){performance.now()-this.firstKeyDownTime<500&&this.pressedKeys.length>6&&this.barcodeValue.next(this.pressedKeys.join("")),this._resetScannerState();return}e.key&&!E.includes(e.key)&&this.pressedKeys.push(e.key)}_resetScannerState(){this.firstKeyDownTime=null,this.pressedKeys=[]}_applyBarcodeLogic(e){if(this.isProcessingBarcode)return;this.isProcessingBarcode=!0,e=y(e);let o=document.activeElement;o instanceof HTMLInputElement||o instanceof HTMLTextAreaElement||o instanceof HTMLSelectElement?this.isFocused(e):this.isNotFocused(e),this.isProcessingBarcode=!1}};var A=(()=>{let e=class e extends u{isNotFocused(n){let t=Array.from(document.querySelectorAll("[data-barcode]")).sort((s,r)=>{let i=parseInt(s.getAttribute("data-barcode")||"0",10),a=parseInt(r.getAttribute("data-barcode")||"0",10);return i-a})[0];if(t){let s=t.querySelector("input");if(s){let r=document.activeElement===t||document.activeElement===s,i=this.testStringAgainstRegexes(n)?.groups?.productCode,a=i?l(i):null,b=a&&t.getAttribute("token")!=="matrix"?a:n;this.mark.next(i?n:null),s.focus(),setTimeout(()=>{s.value=b,s.dispatchEvent(new Event("input")),r||(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(n){let t=document.activeElement,s=document.querySelector('input[data-token="matrix"]');if(t){if(s)return;let r=this.testStringAgainstRegexes(n)?.groups?.productCode,i=r?l(r):null;if(!i)return;let a=i;this.mark.next(r?n:null),setTimeout(()=>{t.value=a,t.dispatchEvent(new Event("input")),t.dispatchEvent(new KeyboardEvent("keydown",{key:"Enter",code:"Enter",bubbles:!0,cancelable:!0}))},150)}}};e.\u0275fac=(()=>{let n;return function(s){return(n||(n=h(e)))(s||e)}})(),e.\u0275prov=p({token:e,factory:e.\u0275fac,providedIn:"root"});let c=e;return c})();export{A as a};
//# sourceMappingURL=chunk-NGMPQIOX.js.map
