import{d as a,e as m,f as h}from"./chunk-SDLOETO3.js";import{g as u,ga as d,h as l}from"./chunk-I3FGJX2C.js";var f=["Shift","Enter","CapsLock","NumLock","F7","F8","]","ArrowDown","MediaTrackNext","Escape","Control"],y=new Set(["00","01","02","10","11","12","13","15","16","17","20","21","22","30","37","90","91","92","93","94","95","96","97","98","99","240","241","242","243","250","251","253","254","255","400","401","402","403","410","411","412","413","414","415","416","417","420","421","422","423","424","425","426","427","710","711","712","713","714","723","7001","7002","7003","7004","8001","8002","8003","8004","8005","8006","8007","8008","8009","8010","8011","8012","8013","8017","8018","8019","8020","8026","8110","8111","8112","8200",...Array.from({length:60},(c,o)=>(3100+o).toString())]),_=(()=>{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&&!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=h(e),e=this._stripAIBrackets(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?a(i):null,p=r&&t.getAttribute("token")!=="matrix"?r:e;this.mark.next(i?e:null),s.focus(),setTimeout(()=>{s.value=p,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?a(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)}}_stripAIBrackets(e){if(!e||!e.includes("("))return e;let t="",s=0;for(;s<e.length;){if(e[s]==="("){let n=e.indexOf(")",s+1);if(n!==-1&&n-s-1>=2&&n-s-1<=4){let i=e.substring(s+1,n);if(/^\d{2,4}$/.test(i)&&y.has(i)){t+=i,s=n+1;continue}}}t+=e[s],s++}return t}};o.\u0275fac=function(t){return new(t||o)},o.\u0275prov=d({token:o,factory:o.\u0275fac,providedIn:"root"});let c=o;return c})();export{_ as a};
//# sourceMappingURL=chunk-LPSZ2ZXX.js.map
