/*!
* Dynamsoft JavaScript Library
* @product Dynamsoft Code Parser JS Edition
* @website http://www.dynamsoft.com
* @copyright Copyright 2024, Dynamsoft Corporation
* @author Dynamsoft
* @version 2.4.20
* @fileoverview Dynamsoft JavaScript Library for Code Parser
* More info on dcp JS: https://www.dynamsoft.com/code-parser/docs/web/programming/javascript/
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("dynamsoft-core")):"function"==typeof define&&define.amd?define(["exports","dynamsoft-core"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).Dynamsoft=e.Dynamsoft||{},e.Dynamsoft.DCP={}),e.Dynamsoft.Core)}(this,(function(e,t){"use strict";var s,n;function i(e){delete e.moduleId;const t=JSON.parse(e.jsonString).ResultInfo,s=e.fullCodeString;e.getFieldValue=e=>"fullcodestring"===e.toLowerCase()?s:r(t,e),e.getFieldMappingStatus=e=>a(t,e),e.getFieldValidationStatus=e=>o(t,e),delete e.fullCodeString}function r(e,t){for(let s of e){if(s.FieldName===t)return s.Value;if(s.ChildFields&&s.ChildFields.length>0){let e;for(let n of s.ChildFields)e=r(n,t);if(void 0!==e)return e}}}function a(t,s){for(let n of t){if(n.FieldName===s)return n.MappingStatus?Number(e.EnumMappingStatus[n.MappingStatus]):e.EnumMappingStatus.MS_NONE;if(n.ChildFields&&n.ChildFields.length>0){let e;for(let t of n.ChildFields)e=a(t,s);if(void 0!==e)return e}}}function o(t,s){for(let n of t){if(n.FieldName===s&&n.ValidationStatus)return n.ValidationStatus?Number(e.EnumValidationStatus[n.ValidationStatus]):e.EnumValidationStatus.VS_NONE;if(n.ChildFields&&n.ChildFields.length>0){let e;for(let t of n.ChildFields)e=o(t,s);if(void 0!==e)return e}}}function c(e){if(e.disposed)throw new Error('"CodeParser" instance has been disposed')}e.EnumMappingStatus=void 0,(s=e.EnumMappingStatus||(e.EnumMappingStatus={}))[s.MS_NONE=0]="MS_NONE",s[s.MS_SUCCEEDED=1]="MS_SUCCEEDED",s[s.MS_FAILED=2]="MS_FAILED",e.EnumValidationStatus=void 0,(n=e.EnumValidationStatus||(e.EnumValidationStatus={}))[n.VS_NONE=0]="VS_NONE",n[n.VS_SUCCEEDED=1]="VS_SUCCEEDED",n[n.VS_FAILED=2]="VS_FAILED";const d=e=>e&&"object"==typeof e&&"function"==typeof e.then;class l extends Promise{get status(){return this._s}get isPending(){return"pending"===this._s}get isFulfilled(){return"fulfilled"===this._s}get isRejected(){return"rejected"===this._s}get task(){return this._task}set task(e){let t;this._task=e,d(e)?t=e:"function"==typeof e&&(t=new Promise(e)),t&&(async()=>{try{const s=await t;e===this._task&&this.resolve(s)}catch(t){e===this._task&&this.reject(t)}})()}get isEmpty(){return null==this._task}constructor(e){let t,s;super(((e,n)=>{t=e,s=n})),this._s="pending",this.resolve=e=>{this.isPending&&(d(e)?this.task=e:(this._s="fulfilled",t(e)))},this.reject=e=>{this.isPending&&(this._s="rejected",s(e))},this.task=e}}class u{constructor(){this._instanceID=void 0,this.bDestroyed=!1}static async createInstance(){if(!t.mapPackageRegister.license)throw Error("Module `license` is not existed.");await t.mapPackageRegister.license.dynamsoft(),await t.loadWasm("dcp");const e=new u,s=new l;let n=t.getNextTaskID();return t.mapTaskCallBack[n]=async t=>{if(t.success)e._instanceID=t.instanceID,s.resolve(e);else{const e=Error(t.message);t.stack&&(e.stack=t.stack),s.reject(e)}},t.worker.postMessage({type:"dcp_createInstance",id:n}),s}async dispose(){c(this);let e=t.getNextTaskID();this.bDestroyed=!0,t.mapTaskCallBack[e]=e=>{if(!e.success){let t=new Error(e.message);throw t.stack=e.stack+"\n"+t.stack,t}},t.worker.postMessage({type:"dcp_dispose",id:e,instanceID:this._instanceID})}get disposed(){return this.bDestroyed}async initSettings(e){return c(this),e&&["string","object"].includes(typeof e)?("string"==typeof e&&t.checkIsLink(e)&&(e=await t.requestResource(e,"text")),"object"==typeof e&&(e=JSON.stringify(e)),await new Promise(((s,n)=>{let i=t.getNextTaskID();t.mapTaskCallBack[i]=async e=>{if(e.success){const t=JSON.parse(e.response);if(0!==t.exception){let e=new Error(t.description?t.description:"Init Settings Failed.");return e.errorCode=t.exception,n(e)}return s(t)}{let t=new Error(e.message);return t.stack=e.stack+"\n"+t.stack,n(t)}},t.worker.postMessage({type:"dcp_initSettings",id:i,instanceID:this._instanceID,body:{settings:e}})}))):console.error("Invalid settings.")}async resetSettings(){return c(this),await new Promise(((e,s)=>{let n=t.getNextTaskID();t.mapTaskCallBack[n]=async t=>{if(t.success)return e();{let e=new Error(t.message);return e.stack=t.stack+"\n"+e.stack,s(e)}},t.worker.postMessage({type:"dcp_resetSettings",id:n,instanceID:this._instanceID})}))}async parse(e,s=""){if(c(this),!(e&&(e instanceof Array||e instanceof Uint8Array||"string"==typeof e)))throw new Error("`parse` must pass in an Array or Uint8Array or string");return await new Promise(((n,r)=>{let a=t.getNextTaskID();e instanceof Array&&(e=Uint8Array.from(e)),"string"==typeof e&&(e=Uint8Array.from(function(e){let t=[];for(let s=0;s<e.length;++s)t.push(e.charCodeAt(s));return t}(e))),t.mapTaskCallBack[a]=async e=>{if(e.success){let t=JSON.parse(e.parseResponse);return t.exception?r(new Error(t.description)):(i(t),n(t))}{let t=new Error(e.message);return t.stack=e.stack+"\n"+t.stack,r(t)}},t.worker.postMessage({type:"dcp_parse",id:a,instanceID:this._instanceID,body:{source:e,taskSettingName:s}})}))}}const p="undefined"==typeof self,f=(()=>{if(!p&&document.currentScript){let e=document.currentScript.src,t=e.indexOf("?");if(-1!=t)e=e.substring(0,t);else{let t=e.indexOf("#");-1!=t&&(e=e.substring(0,t))}return e.substring(0,e.lastIndexOf("/")+1)}return"./"})(),g=e=>{if(null==e&&(e="./"),p);else{let t=document.createElement("a");t.href=e,e=t.href}return e.endsWith("/")||(e+="/"),e};t.autoDiscoveryPaths.dcp={version:"2.4.20",path:f},t.workerAutoResources.dcp={js:!0,wasm:!0,deps:["license"]},t.mapPackageRegister.dcp={handleParsedResultItem:i};const h="1.4.10";"string"!=typeof t.autoDiscoveryPaths.std&&t.compareVersion(t.autoDiscoveryPaths.std.version,h)<0&&(t.autoDiscoveryPaths.std={version:h,path:g(f+`../../dynamsoft-capture-vision-std@${h}/dist/`)});e.CodeParser=u,e.CodeParserModule=class{static getVersion(){const e=t.innerVersions.dcp&&t.innerVersions.dcp.wasm,s=t.innerVersions.dcp&&t.innerVersions.dcp.worker;return`2.4.20(Worker: ${s||"Not Loaded"}, Wasm: ${e||"Not Loaded"})`}static async loadSpec(e,s){return await t.loadWasm("dcp"),await new Promise(((n,i)=>{let r=t.getNextTaskID();t.mapTaskCallBack[r]=async e=>{if(e.success)return n();{let t=new Error(e.message);return t.stack=e.stack+"\n"+t.stack,i(t)}},s&&!s.endsWith("/")&&(s+="/");const a=e instanceof Array?e:[e],o=t.handleEngineResourcePaths(t.CoreModule.engineResourcePaths,t.autoDiscoveryPaths);t.worker.postMessage({type:"dcp_appendResourceBuffer",id:r,body:{specificationPath:s||o.dcp+"specification/",specificationNames:a}})}))}}}));
