import{d as l,e as f,f as y}from"./chunk-32C5SJZL.js";import{Ia as h,g as d,ga as p,h as m}from"./chunk-I3FGJX2C.js";var E=["Shift","Enter","CapsLock","NumLock","F7","F8","(",")","ArrowDown","MediaTrackNext"];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=[],r=f;for(let[t,s]of Object.entries(r)){let n=e.match(s);n&&o.push({regexName:t,match:n,groups:n.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>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&&!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(r){let t=Array.from(document.querySelectorAll("[data-barcode]")).sort((s,n)=>{let i=parseInt(s.getAttribute("data-barcode")||"0",10),a=parseInt(n.getAttribute("data-barcode")||"0",10);return i-a})[0];if(t){let s=t.querySelector("input");if(s){let n=document.activeElement===t||document.activeElement===s,i=this.testStringAgainstRegexes(r)?.groups?.productCode,a=i?l(i):null,b=a&&t.getAttribute("token")!=="matrix"?a:r;this.mark.next(i?r:null),s.focus(),setTimeout(()=>{s.value=b,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(r){let t=document.activeElement,s=document.querySelector('input[data-token="matrix"]');if(t){if(s)return;let n=this.testStringAgainstRegexes(r)?.groups?.productCode,i=n?l(n):null;if(!i)return;let a=i;this.mark.next(n?r: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 r;return function(s){return(r||(r=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-NQKVPDT7.js.map
