import {b}from'./chunk-ONDP3TB2.js';import {Logger,DataCaptureVersion,DataCaptureError}from'@scandit/web-datacapture-core';import {getLibraryLocationURIs}from'@scandit/web-datacapture-core/build/js/private/Helper.js';import {Deferred}from'@scandit/web-datacapture-core/build/js/private/utils/deferred.js';import {isWorkerScope}from'@scandit/web-datacapture-core/build/js/private/utils/isWorkerScope.js';import {instantiateWebAssembly}from'@scandit/web-datacapture-core/build/js/worker/helper.js';import {setWasmMetadata}from'@scandit/web-datacapture-core/build/js/worker/helperFetch.js';import {importWasmScript}from'@scandit/web-datacapture-core/build/js/worker/importWasmScript.js';var _=s=>s.result!=null&&s.error==null;function l(s,e){return {type:"djinniResult",payload:{id:s.id,className:s.className,methodName:s.methodName,instanceId:s.instanceId,result:e}}}function m(s,e){return {type:"parserLoaderResult",payload:{id:s.id,className:s.className,methodName:s.methodName,result:e}}}function k(s){try{return {type:"result",result:JSON.parse(s)}}catch(e){return {type:"error",error:e}}}var f=class{constructor(e,r){this.parsers=new Map;this.onMessageListener=this.onMessage.bind(this);this.setupPromise=null;this.workerSelf=e,this.moduleHandler=r,isWorkerScope(e)&&(this.workerFunctions={postMessage:this.workerSelf.postMessage.bind(this.workerSelf)},this.listenToMessages()),setWasmMetadata(b);}get Module(){return this.moduleHandler.get()}setLogLevel(e){Logger.setLevel(e.args[0]),this.workerFunctions.postMessage(m(e,{type:"result",result:true}));}async load(e){try{await this.setup(e.args[0]),this.workerFunctions.postMessage(m(e,{type:"result",result:true}));}catch(r){this.workerFunctions.postMessage(m(e,{type:"error",error:r}));}}listenToMessages(){this.workerSelf.addEventListener("message",this.onMessageListener),this.workerSelf.addEventListener("error",e=>{Logger.log(Logger.Level.Error,e);}),this.workerSelf.addEventListener("messageerror",e=>{Logger.log(Logger.Level.Error,e);});}onMessage(e){return e.data.className==="ParserLoader"&&e.data.methodName==="load"&&this.load(e.data),e.data.className==="ParserLoader"&&e.data.methodName==="setLogLevel"&&this.setLogLevel(e.data),e.data.className==="ParserInterface"&&this.performDjinniAction(e.data),true}async performDjinniAction(e){var n,i;let r;if(Logger.log(Logger.Level.Debug,`${e.className}::${e.methodName}(`,e.args,")"),e.methodName==="create"){let d=e.args[0].toString();if(this.parsers.has(d)){this.workerFunctions.postMessage(l(e,{type:"result",result:d}));return}r=this.createParser(e),Logger.log(Logger.Level.Debug,r),this.workerFunctions.postMessage(r);return}let o=this.parsers.get((n=e.instanceId)!=null?n:"");if(o==null){this.workerFunctions.postMessage(l(e,{type:"error",error:`parser is not defined: try to access instance with id ${(i=e.instanceId)!=null?i:""}`}));return}let a=o[e.methodName](...e.args);_(a)?(e.methodName.endsWith("ToJson")&&typeof a.result=="string"&&(a=k(a.result)),r=l(e,a)):r=l(e,{type:"error",error:a.error}),this.workerFunctions.postMessage(r);}async setup({libraryLocation:e,threadsSupport:r,simdSupport:o,verifyResponseHash:a,referredOrigin:n}){if(this.setupPromise!=null)return this.setupPromise;let{resolve:i,reject:d,promise:g}=new Deferred;this.setupPromise=g;let c=DataCaptureVersion.sdkVersion();if(c==="")throw new DataCaptureError({name:"Invalid library version",message:"Library version is not defined or empty, cannot generate proper path to library files."});let M=getLibraryLocationURIs(c,e,this.getWasmParserFileName(o,r)),{jsURI:p,wasmURI:h}=M;return this.moduleHandler.set({mainScriptUrlOrBlob:p,locateFile:u=>e+u,onMainComplete:()=>{},onRuntimeInitialized:()=>{i();},print:u=>{Logger.log(Logger.Level.Info,u);},instantiateWasm:(u,R)=>(instantiateWebAssembly({importObject:u,wasmURI:h,expectedHash:this.getWasmParserExpectedHash(o,r),verifyResponseHash:a,successCallback:R,progressCallback:()=>{},referredOrigin:n}),{})}),await importWasmScript(p,c)||d(`Couldn't retrieve Parser library at ${p}, did you configure the path for it correctly?`),g}createParser(e){var n,i;let r=this.Module.ParserInterface.create(e.args[0]);if(Logger.log(Logger.Level.Debug,`Parser:create returns ${r.type}`,r),r.type==="error")return l(e,r);let o=(i=(n=e.args[0])==null?void 0:n.toString())!=null?i:"",a=l(e,{type:"result",result:o});return this.parsers.set(o,r.result),a}getWasmParserExpectedHash(e,r){return r?e?"9b784e674f5e152921b98c29e1bbd6f0d2d5d0ac13ab716e7b8b965f78b0db92":"c02a00db41e66f41e9916dc09eba9fda1f90457f015af12120e9bd2b13f497a8":e?"ad3a6f8b84d79e1fdc442fc8b20284b8d28303a4efcb1f9338ca74fbe9e3fe56":"1be2ea6de3dfa0f540cf028e04afa727fe9ea41c83c65f3794ad4a92aca23ec1"}getWasmParserFileName(e,r){return r?e?"scandit-datacapture-sdk-parser-simd-pthreads.wasm":"scandit-datacapture-sdk-parser-pthreads.wasm":e?"scandit-datacapture-sdk-parser-simd.wasm":"scandit-datacapture-sdk-parser.wasm"}};export{f as a};