import React,{useCallback}from"react";import{useState,useEffect,useRef}from"react";import{renderFocus}from"./common.js";import{CssClasses}from"./constants.js";import FormElementOuter from"./FormElementOuter.js";import{getKeyString}from"./keyboard.js";import{isScanRef,makeSafeClassName}from"./utils.js";import{renderRemove,renderScanBarcode,renderScanQrcode,renderScanMulti}from"./assets/stock.js";function renderScanner(e,a){return React.createElement(Scanner,{component:e,element:a})}var ScanType,EntryMethod;!function(e){e.BarCode="barcode",e.QrCode="qrcode"}(ScanType=ScanType||{});const TEXT_AREA_DEFAULT_ROWS=5,CAMERA_READY_TIMEOUT_MS=1500;!function(e){e.Camera="camera",e.Manual="manual"}(EntryMethod=EntryMethod||{});const Scanner=e=>{const[t,o]=useState(),[a,r]=useState(""),[n,c]=useState(!1),[s,l]=useState(!0),m=useRef(),{component:d,element:i}=e,{host:u,name:p}=d.props;var E="barcode"!==i.scanType;let y=i.value;useEffect(()=>(c(!0),()=>c(!1)),[]),useEffect(()=>{n&&i.enabled&&i.autoActivate&&(i.autoActivate=!1,C())},[n]),useEffect(()=>{const e=()=>{l(!1),setTimeout(()=>{n&&l(!0)},CAMERA_READY_TIMEOUT_MS)};return t&&e(),()=>{t&&(t.reset(),e())}},[t]);var S=useCallback(e=>{renderFocus(e,p,u)},[]);y&&(isScanRef(y)&&y.value||(i.value=y=void 0));var v=u.renderText(i.tooltip);const T=(e,a,t)=>{e&&(y&&y.value===e||(a={refValueType:"scan",value:e,scanType:t,entryMethod:a},i.value=a,r(e),u.post("changed",{name:d.props.name,value:a})))},R=async e=>{var a=await import("@zxing/library");let t=void 0;return e===ScanType.QrCode?t=[a.BarcodeFormat.QR_CODE,a.BarcodeFormat.DATA_MATRIX,a.BarcodeFormat.AZTEC,a.BarcodeFormat.PDF_417,a.BarcodeFormat.MAXICODE]:e===ScanType.BarCode&&(t=[a.BarcodeFormat.CODABAR,a.BarcodeFormat.CODE_39,a.BarcodeFormat.CODE_93,a.BarcodeFormat.CODE_128,a.BarcodeFormat.EAN_8,a.BarcodeFormat.EAN_13,a.BarcodeFormat.ITF,a.BarcodeFormat.RSS_14,a.BarcodeFormat.RSS_EXPANDED,a.BarcodeFormat.UPC_A,a.BarcodeFormat.UPC_E]),t},C=async()=>{if(t)return o(null),void u.post("cancel",{name:d.props.name,type:"cancel"});var e;const a=await import("@zxing/library");n&&(e=await(async e=>{var a=await import("@zxing/library"),e=await R(e);const t=new Map;return t.set(a.DecodeHintType.POSSIBLE_FORMATS,e),t})(i.scanType),e=new a.BrowserMultiFormatReader(e),o(e),await(async e=>{try{var a=await e.listVideoInputDevices();if(!a||0==a.length)throw new DOMException("No video input devices detected.");const r=await e.decodeOnceFromVideoDevice(void 0,m.current),n=await R(ScanType.QrCode);var t=n.indexOf(r.getBarcodeFormat())<0?ScanType.BarCode:ScanType.QrCode;T(r.toString(),EntryMethod.Camera,t),o(null)}catch(e){e instanceof DOMException&&(i.error=e.message,o(null))}})(e))};var f=e=>{t&&o(null),T(e.currentTarget.value,EntryMethod.Manual)},e=e=>{r(e.currentTarget.value)};const F=[i.type,makeSafeClassName(p)];return React.createElement(FormElementOuter,{className:F.join(" "),component:d,element:i,errorClassName:CssClasses.ERROR},React.createElement("div",{className:"items"},React.createElement("div",{className:"scanner-container"},React.createElement("video",{className:"video-element",hidden:!t,ref:m}),React.createElement("div",{className:"user-controls"},E?React.createElement("textarea",{className:"manual-entry",disabled:!(i.enabled&&i.manualEntry),onBlur:f,onChange:e,onKeyDown:e=>e.stopPropagation(),ref:S,rows:i.size||TEXT_AREA_DEFAULT_ROWS,spellCheck:!1,title:v,value:a}):React.createElement("input",{className:"manual-entry",disabled:!(i.enabled&&i.manualEntry),onBlur:f,onChange:e,onKeyDown:e=>{var a=e.currentTarget;"enter"===getKeyString(e)&&(e.stopPropagation(),t&&o(null),T(a.value,EntryMethod.Manual))},ref:S,title:v,type:"text",value:a}),React.createElement("button",{className:t?"scan activated":"scan",disabled:!(i.enabled&&s),title:v,onClick:e=>C(),ref:S},(t?renderRemove:i.scanType===ScanType.BarCode?renderScanBarcode:i.scanType===ScanType.QrCode?renderScanQrcode:renderScanMulti)())))))};export{renderScanner,ScanType,TEXT_AREA_DEFAULT_ROWS};