/*!
* 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/
*/
import{mapPackageRegister as e,loadWasm as t,getNextTaskID as s,mapTaskCallBack as i,worker as n,checkIsLink as r,requestResource as a,autoDiscoveryPaths as o,workerAutoResources as c,compareVersion as d,innerVersions as l,handleEngineResourcePaths as u,CoreModule as f}from"dynamsoft-core";var p,h;function g(e){delete e.moduleId;const t=JSON.parse(e.jsonString).ResultInfo,s=e.fullCodeString;e.getFieldValue=e=>"fullcodestring"===e.toLowerCase()?s:y(t,e),e.getFieldMappingStatus=e=>S(t,e),e.getFieldValidationStatus=e=>_(t,e),delete e.fullCodeString}function y(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 i of s.ChildFields)e=y(i,t);if(void 0!==e)return e}}}function S(e,t){for(let s of e){if(s.FieldName===t)return s.MappingStatus?Number(p[s.MappingStatus]):p.MS_NONE;if(s.ChildFields&&s.ChildFields.length>0){let e;for(let i of s.ChildFields)e=S(i,t);if(void 0!==e)return e}}}function _(e,t){for(let s of e){if(s.FieldName===t&&s.ValidationStatus)return s.ValidationStatus?Number(h[s.ValidationStatus]):h.VS_NONE;if(s.ChildFields&&s.ChildFields.length>0){let e;for(let i of s.ChildFields)e=_(i,t);if(void 0!==e)return e}}}function m(e){if(e.disposed)throw new Error('"CodeParser" instance has been disposed')}!function(e){e[e.MS_NONE=0]="MS_NONE",e[e.MS_SUCCEEDED=1]="MS_SUCCEEDED",e[e.MS_FAILED=2]="MS_FAILED"}(p||(p={})),function(e){e[e.VS_NONE=0]="VS_NONE",e[e.VS_SUCCEEDED=1]="VS_SUCCEEDED",e[e.VS_FAILED=2]="VS_FAILED"}(h||(h={}));const E=e=>e&&"object"==typeof e&&"function"==typeof e.then;class w 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,E(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,i)=>{t=e,s=i})),this._s="pending",this.resolve=e=>{this.isPending&&(E(e)?this.task=e:(this._s="fulfilled",t(e)))},this.reject=e=>{this.isPending&&(this._s="rejected",s(e))},this.task=e}}class k{constructor(){this._instanceID=void 0,this.bDestroyed=!1}static async createInstance(){if(!e.license)throw Error("Module `license` is not existed.");await e.license.dynamsoft(),await t("dcp");const r=new k,a=new w;let o=s();return i[o]=async e=>{if(e.success)r._instanceID=e.instanceID,a.resolve(r);else{const t=Error(e.message);e.stack&&(t.stack=e.stack),a.reject(t)}},n.postMessage({type:"dcp_createInstance",id:o}),a}async dispose(){m(this);let e=s();this.bDestroyed=!0,i[e]=e=>{if(!e.success){let t=new Error(e.message);throw t.stack=e.stack+"\n"+t.stack,t}},n.postMessage({type:"dcp_dispose",id:e,instanceID:this._instanceID})}get disposed(){return this.bDestroyed}async initSettings(e){return m(this),e&&["string","object"].includes(typeof e)?("string"==typeof e&&r(e)&&(e=await a(e,"text")),"object"==typeof e&&(e=JSON.stringify(e)),await new Promise(((t,r)=>{let a=s();i[a]=async e=>{if(e.success){const s=JSON.parse(e.response);if(0!==s.exception){let e=new Error(s.description?s.description:"Init Settings Failed.");return e.errorCode=s.exception,r(e)}return t(s)}{let t=new Error(e.message);return t.stack=e.stack+"\n"+t.stack,r(t)}},n.postMessage({type:"dcp_initSettings",id:a,instanceID:this._instanceID,body:{settings:e}})}))):console.error("Invalid settings.")}async resetSettings(){return m(this),await new Promise(((e,t)=>{let r=s();i[r]=async s=>{if(s.success)return e();{let e=new Error(s.message);return e.stack=s.stack+"\n"+e.stack,t(e)}},n.postMessage({type:"dcp_resetSettings",id:r,instanceID:this._instanceID})}))}async parse(e,t=""){if(m(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(((r,a)=>{let o=s();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))),i[o]=async e=>{if(e.success){let t=JSON.parse(e.parseResponse);return t.exception?a(new Error(t.description)):(g(t),r(t))}{let t=new Error(e.message);return t.stack=e.stack+"\n"+t.stack,a(t)}},n.postMessage({type:"dcp_parse",id:o,instanceID:this._instanceID,body:{source:e,taskSettingName:t}})}))}}const D="undefined"==typeof self,N=(()=>{if(!D&&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"./"})(),C=e=>{if(null==e&&(e="./"),D);else{let t=document.createElement("a");t.href=e,e=t.href}return e.endsWith("/")||(e+="/"),e};o.dcp={version:"2.4.20",path:N},c.dcp={js:!0,wasm:!0,deps:["license"]},e.dcp={handleParsedResultItem:g};const I="1.4.10";"string"!=typeof o.std&&d(o.std.version,I)<0&&(o.std={version:I,path:C(N+`../../dynamsoft-capture-vision-std@${I}/dist/`)});class F{static getVersion(){const e=l.dcp&&l.dcp.wasm,t=l.dcp&&l.dcp.worker;return`2.4.20(Worker: ${t||"Not Loaded"}, Wasm: ${e||"Not Loaded"})`}static async loadSpec(e,r){return await t("dcp"),await new Promise(((t,a)=>{let c=s();i[c]=async e=>{if(e.success)return t();{let t=new Error(e.message);return t.stack=e.stack+"\n"+t.stack,a(t)}},r&&!r.endsWith("/")&&(r+="/");const d=e instanceof Array?e:[e],l=u(f.engineResourcePaths,o);n.postMessage({type:"dcp_appendResourceBuffer",id:c,body:{specificationPath:r||l.dcp+"specification/",specificationNames:d}})}))}}export{k as CodeParser,F as CodeParserModule,p as EnumMappingStatus,h as EnumValidationStatus};
