/*! ****************************************************************************
Copyright (c) Microblink. All rights reserved.

THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
***************************************************************************** */
var e;!function(e){e.Cloud="cloud",e.SelfHosted="self-hosted"}(e||(e={}));const t={GENERIC_ERROR:"There was an error during scan action.",UNKNOWN_ERROR:"Oops, something went wrong."};function s(e,...t){const s=function(e,t,s){e=e||"",t=t||"",s=s||"";for(;e.indexOf(t)>-1;)e=e.replace(t,s);return e}([...t].join("/"),"//","/");return(e.endsWith("/")?e.slice(0,-1):e)+(s.startsWith("/")?s:"/"+s)}function o(e,t,s,o){return new Promise(((a,n)=>{const r={method:e,headers:s};o&&(r.body=JSON.stringify(o)),window.fetch(t,r).then((e=>{const t=e.headers.entries(),s={};for(const e of t)s[e[0]]=e[1];const o={httpStatus:e.status,headers:s};e.text().then((e=>{try{const t=JSON.parse(e);o.data=t}catch(t){o.data=e}a(o)})).catch((e=>{o.data=e,n(o)}))})).catch((e=>{n({httpStatus:-1,headers:{},data:e})}))}))}const a={API_ROLE_IS_NOT_FOUND:"API role is not found",AUTHORIZATION_HEADER_IS_NOT_VALID:"Authorization header is not valid",BAD_REQUEST:"Bad request",FORBIDDEN_RECOGNIZER:"Forbidden recognizer",IMAGE_IS_NOT_ABLE_TO_CONVERT_TO_RAW_PIXELS:"Image is not able to convert to raw pixels",IMAGE_IS_NOT_VALID:"Image is not valid",IMAGE_IS_NOT_VALID_BASE64_STRING:"Image is not valid base64 string",IMAGE_SIZE_IS_TOO_BIG:"Image size is too big",INSUFFICIENT_BALANCE:"Insufficient balance",INTERNAL_SERVER_ERROR:"Internal server error",INVALID_LICENSE_KEY:"Invalid license key",NOT_ALLOWED_TO_EXECUTE_REQUESTED_RECOGNIZER:"Not allowed to execute requested recognizer",SERVER_CANCELED_REQUEST:"Server canceled request",SERVER_TOO_BUSY:"Server too busy"},n={Accept:"application/json","Content-Type":"application/json"};class r{constructor(e,t,s){this.apiLocation=e,this.healthcheckEndpoint=t,this.headers=Object.assign(n,s);for(const e in this.headers)e&&this.headers[e]||delete this.headers[e]}getHealthcheckResponse(){return o("GET",s(this.apiLocation,this.healthcheckEndpoint),this.headers)}recognize(e,t,a="POST"){return o(a,s(this.apiLocation,e),this.headers,t)}}const i={API_ROLE_IS_NOT_FOUND:"API role is not found",AUTHORIZATION_HEADER_IS_NOT_VALID:"Authorization header is not valid",BAD_REQUEST:"Bad request",FORBIDDEN_RECOGNIZER:"Forbidden recognizer",IMAGE_IS_NOT_ABLE_TO_CONVERT_TO_RAW_PIXELS:"Image is not able to convert to raw pixels",IMAGE_IS_NOT_VALID:"Image is not valid",IMAGE_IS_NOT_VALID_BASE64_STRING:"Image is not valid base64 string",IMAGE_SIZE_IS_TOO_BIG:"Image size is too big",INTERNAL_SERVER_ERROR:"Internal server error",INVALID_LICENSE_KEY:"Invalid license key",NOT_ALLOWED_TO_EXECUTE_REQUESTED_RECOGNIZER:"Not allowed to execute requested recognizer",SERVER_CANCELED_REQUEST:"Server canceled request",SERVER_TOO_BUSY:"Server too busy"},c={Accept:"application/json","Content-Type":"application/json"};class h{constructor(e,t,s){this.apiLocation=e,this.healthcheckEndpoint=t,this.headers=Object.assign(c,s);for(const e in this.headers)e&&this.headers[e]||delete this.headers[e]}getHealthcheckResponse(){return o("GET",s(this.apiLocation,this.healthcheckEndpoint),this.headers)}recognize(e,t,a="POST"){return o(a,s(this.apiLocation,e),this.headers,t)}}class E{constructor(s,o){if(!s)throw"API type is not provided!";const n=(null==o?void 0:o.headers)||{},c=(null==o?void 0:o.healthcheckEndpoint)||"/v1/hc",E=(null==o?void 0:o.apiLocation)||"";let _={};switch(s){case e.Cloud:_=a,this.service=new r(E||"http://localhost",c,n);break;case e.SelfHosted:_=i,this.service=new h(E||"http://localhost",c,n);break;default:throw"Unsupported API type!"}this.errorMessages=Object.assign(t,_,(null==o?void 0:o.messages)||{})}getHealthcheck(){return new Promise(((e,t)=>{this.service.getHealthcheckResponse().then((s=>{200!==s.httpStatus?t({status:!1,response:s}):e({status:!0})})).catch((e=>{t({status:!1,response:e})}))}))}recognize(e,t){return new Promise(((s,o)=>{this.service.recognize(e,t).then((e=>{if(200===e.httpStatus)return void s({status:!0,response:e});const t=e.data.code||"GENERIC_ERROR";o({status:!1,response:e,error:{code:t,message:this.errorMessages[t]}})})).catch((e=>{let t="UNKNOWN_ERROR";(-1===e.httpStatus||100>e.httpStatus%400)&&(t="GENERIC_ERROR");o({status:!1,response:e,error:{code:t,message:this.errorMessages[t]}})}))}))}imageDataToBase64(e){if(!e)throw"Image data is not provided!";if(!e.width||!e.height)throw"Image data is malformed!";return function(e){const t=document.createElement("canvas");t.width=e.width,t.height=e.height;const s=t.getContext("2d");return null==s||s.putImageData(e,0,0),t.toDataURL()}(e)}}export{e as ApiType,E as Client,t as GenericMessages};
