import{d as c,e as m,f as p,g as h}from"./chunk-4MCMX46R.js";import{fa as d,g as u,h as l}from"./chunk-PVUKHE45.js";var y=["Shift","Enter","CapsLock","NumLock","F7","F8","]","ArrowDown","MediaTrackNext","Escape","Control"],w=(()=>{let o=class o{constructor(){this.barcodeValue=new u,this.barcodeValue$=this.barcodeValue.asObservable(),this.mark=new l(null),this.isProcessingBarcode=!1,this._firstKeyDownTime=null,this._pressedKeys=[]}initSubs(){this._listenToKeyboard(),this.barcodeValue$.subscribe(e=>{this._applyBarcodeLogic(e)})}testStringAgainstRegexes(e){let t=[],s=m;for(let[n,i]of Object.entries(s)){let r=e.match(i);r&&t.push({regexName:n,match:r,groups:r.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&&!y.includes(e.key)&&this._pressedKeys.push(e.key)}_resetScannerState(){this._firstKeyDownTime=null,this._pressedKeys=[]}_applyBarcodeLogic(e){if(this.isProcessingBarcode)return;this.isProcessingBarcode=!0,e=p(e),e=h(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 i=parseInt(s.getAttribute("data-barcode")||"0",10),r=parseInt(n.getAttribute("data-barcode")||"0",10);return i-r})[0];if(t){let s=t.querySelector("input");if(s){let n=document.activeElement===t||document.activeElement===s,i=this.testStringAgainstRegexes(e)?.groups?.productCode,r=i?c(i):null,f=r&&t.getAttribute("token")!=="matrix"?r:e;this.mark.next(i?e:null),s.focus(),setTimeout(()=>{s.value=f,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,i=n?c(n):null;if(!i)return;let r=i;this.mark.next(n?e:null),setTimeout(()=>{t.value=r,t.dispatchEvent(new Event("input")),t.dispatchEvent(new KeyboardEvent("keydown",{key:"Enter",code:"Enter",bubbles:!0,cancelable:!0}))},150)}}};o.\u0275fac=function(t){return new(t||o)},o.\u0275prov=d({token:o,factory:o.\u0275fac,providedIn:"root"});let a=o;return a})();export{w as a};
//# sourceMappingURL=chunk-WOMK4BL6.js.map
