import{d as l,e as f,f as y}from"./chunk-YXUFZINL.js";import{Ia as p,g as d,ga as h,h as m}from"./chunk-I3FGJX2C.js";var E=["Shift","Enter","CapsLock","NumLock","F7","F8","ArrowDown","MediaTrackNext"],g=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},(a,e)=>(3100+e).toString())]);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 i=[],t=f;for(let[s,n]of Object.entries(t)){let r=e.match(n);r&&i.push({regexName:s,match:r,groups:r.groups||{}})}return i.find(s=>s!==null)}_stripAIBrackets(e){if(!e||!e.includes("("))return e;let i="",t=0;for(;t<e.length;){if(e[t]==="("){let s=e.indexOf(")",t+1);if(s!==-1&&s-t-1>=2&&s-t-1<=4){let n=e.substring(t+1,s);if(/^\d{2,4}$/.test(n)&&g.has(n)){i+=n,t=s+1;continue}}}i+=e[t],t++}return i}_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),e=this._stripAIBrackets(e);let i=document.activeElement;i instanceof HTMLInputElement||i instanceof HTMLTextAreaElement||i instanceof HTMLSelectElement?this.isFocused(e):this.isNotFocused(e),this.isProcessingBarcode=!1}};var B=(()=>{let e=class e extends u{isNotFocused(t){let s=Array.from(document.querySelectorAll("[data-barcode]")).sort((n,r)=>{let o=parseInt(n.getAttribute("data-barcode")||"0",10),c=parseInt(r.getAttribute("data-barcode")||"0",10);return o-c})[0];if(s){let n=s.querySelector("input");if(n){let r=document.activeElement===s||document.activeElement===n,o=this.testStringAgainstRegexes(t)?.groups?.productCode,c=o?l(o):null,b=c&&s.getAttribute("token")!=="matrix"?c:t;this.mark.next(o?t:null),n.focus(),setTimeout(()=>{n.value=b,n.dispatchEvent(new Event("input")),r||(s.getAttribute("token")==="matrix"?setTimeout(()=>{n.dispatchEvent(new KeyboardEvent("keydown",{key:"Enter",code:"Enter",bubbles:!0,cancelable:!0}))},150):n.dispatchEvent(new KeyboardEvent("keydown",{key:"Enter",code:"Enter",bubbles:!0,cancelable:!0})))},150)}}}isFocused(t){let s=document.activeElement,n=document.querySelector('input[data-token="matrix"]');if(s){if(n)return;let r=this.testStringAgainstRegexes(t)?.groups?.productCode,o=r?l(r):null;if(!o)return;let c=o;this.mark.next(r?t:null),setTimeout(()=>{s.value=c,s.dispatchEvent(new Event("input")),s.dispatchEvent(new KeyboardEvent("keydown",{key:"Enter",code:"Enter",bubbles:!0,cancelable:!0}))},150)}}};e.\u0275fac=(()=>{let t;return function(n){return(t||(t=p(e)))(n||e)}})(),e.\u0275prov=h({token:e,factory:e.\u0275fac,providedIn:"root"});let a=e;return a})();export{B as a};
//# sourceMappingURL=chunk-RN3VAOYT.js.map
