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