text: "use strict";function getTargetApiConfigVS(e,o){var i=e&&e.vs;return"object"==typeof i&&(i=i[o.platformSub]),i}Object.defineProperty(exports,"__esModule",{value:!0});var sdkLib_1=require("./sdkLib");exports.APP_TYPE=sdkLib_1.APP_TYPE,exports.LogLevel=sdkLib_1.LogLevel,exports.isFunction=sdkLib_1.isFunction,exports.compareVersion=sdkLib_1.compareVersion,exports.ENV_ENUM=sdkLib_1.ENV_ENUM,exports.ENV_ENUM_SUB=sdkLib_1.ENV_ENUM_SUB;var Sdk=function(){function e(e,o){var i=this;this.configJsApiList=[],this.hadConfig=!1,this.p={},this.config$=new Promise(function(e,o){i.p.reject=o,i.p.resolve=e}),this.logQueue=[],this.devConfig={debug:!1},this.platformConfigMap={},this.invokeAPIConfigMapByMethod={},this.isBridgeDrity=!0,this.getExportSdk=function(){return i.exportSdk},this.setAPI=function(e,o){i.invokeAPIConfigMapByMethod[e]=o},this.setPlatform=function(e){i.isBridgeDrity=!0,i.platformConfigMap[e.platform]=e,e.platform===i.env.platform&&e.bridgeInit().catch(function(e){i.customLog(sdkLib_1.LogLevel.WARNING,["auto bridgeInit error",e||""])})},this.getPlatformConfigMap=function(){return i.platformConfigMap},this.deleteApiConfig=function(e,o){var n=i.invokeAPIConfigMapByMethod[e];n&&delete n[o]},this.invokeAPI=function(e,o,n){void 0===o&&(o={}),void 0===n&&(n=!0),i.customLog(sdkLib_1.LogLevel.INFO,['==> "'+e+'" params: ',o]);var t=+new Date,r=t+"_"+Math.floor(1e3*Math.random());if(i.devConfig.onBeforeInvokeAPI)try{i.devConfig.onBeforeInvokeAPI({invokeId:r,method:e,params:o,startTime:t})}catch(e){i.customLog(sdkLib_1.LogLevel.ERROR,["call Hook:onBeforeInvokeAPI failed, reason:",e])}return!1===i.devConfig.isAuthApi&&(n=!1),i.bridgeInitFn().then(function(s){var a=i.invokeAPIConfigMapByMethod[e],f=i.devConfig.forceEnableDealApiFnMap&&i.devConfig.forceEnableDealApiFnMap[e]&&!0===i.devConfig.forceEnableDealApiFnMap[e](o),d=!f&&(!0===i.devConfig.isDisableDeal||i.devConfig.disbaleDealApiWhiteList&&-1!==i.devConfig.disbaleDealApiWhiteList.indexOf(e));if(a||!n){var c;if(a&&(c=a[i.env.platform]),c||!n){var u={};u=!d&&c&&c.paramsDeal&&sdkLib_1.isFunction(c.paramsDeal)?c.paramsDeal(o):Object.assign({},o);var g=function(e){return!d&&c&&c.resultDeal&&sdkLib_1.isFunction(c.resultDeal)?c.resultDeal(e):e};if(sdkLib_1.isFunction(u.onSuccess)){var l=u.onSuccess;u.onSuccess=function(e){l(g(e))}}return s(e,u).then(g,function(o){var t=i.hadConfig&&void 0===i.isReady&&-1!==i.configJsApiList.indexOf(e),r="object"==typeof o&&"string"==typeof o.errorCode&&o.errorCode===sdkLib_1.ERROR_CODE.no_permission,a="object"==typeof o&&"string"==typeof o.errorCode&&o.errorCode===sdkLib_1.ERROR_CODE.cancel,f=getTargetApiConfigVS(c,i.env),d=f&&i.env.version&&sdkLib_1.compareVersion(i.env.version,f),l=(i.env.platform===sdkLib_1.ENV_ENUM.ios||i.env.platform===sdkLib_1.ENV_ENUM.android)&&t&&r,v=i.env.platform===sdkLib_1.ENV_ENUM.pc&&t&&(d&&!a&&n||r);return l||v?i.config$.then(function(){return s(e,u).then(g)}):Promise.reject(o)}).then(function(n){if(i.devConfig.onAfterInvokeAPI)try{i.devConfig.onAfterInvokeAPI({invokeId:r,method:e,params:o,payload:n,isSuccess:!0,startTime:t,duration:+new Date-t})}catch(e){i.customLog(sdkLib_1.LogLevel.ERROR,["call Hook:onAfterInvokeAPI failed, reason:",e])}return i.customLog(sdkLib_1.LogLevel.INFO,['<== "'+e+'" success result: ',n]),n},function(n){if(i.devConfig.onAfterInvokeAPI)try{i.devConfig.onAfterInvokeAPI({invokeId:r,method:e,params:o,payload:n,startTime:t,duration:+new Date-t,isSuccess:!1})}catch(n){i.customLog(sdkLib_1.LogLevel.ERROR,["call Hook:onAfterInvokeAPI failed, reason:",n])}return i.customLog(sdkLib_1.LogLevel.WARNING,['<== "'+e+'" fail result: ',n]),Promise.reject(n)})}var v='"'+e+'" do not support the current platform ('+i.env.platform+")";return i.customLog(sdkLib_1.LogLevel.ERROR,[v]),Promise.reject({errorCode:sdkLib_1.ERROR_CODE.jsapi_internal_error,errorMessage:v})}var v="This API method is not configured for the platform ("+i.env.platform+")";return i.customLog(sdkLib_1.LogLevel.ERROR,[v]),Promise.reject({errorCode:sdkLib_1.ERROR_CODE.jsapi_internal_error,errorMessage:v})})},this.customLog=function(e,o){var n={level:e,text:o,time:new Date};if(!0===i.devConfig.debug)i.customLogInstance(n);else{i.logQueue.push(n);i.logQueue.length>10&&(i.logQueue=i.logQueue.slice(i.logQueue.length-10))}},this.clearLogQueue=function(){i.logQueue.forEach(function(e){i.customLogInstance(e)}),i.logQueue=[]},this.customLogInstance=o,this.env=e,this.bridgeInitFn=function(){if(i.bridgeInitFnPromise&&!i.isBridgeDrity)return i.bridgeInitFnPromise;i.isBridgeDrity=!1;var o=i.platformConfigMap[e.platform];if(o)i.bridgeInitFnPromise=o.bridgeInit().catch(function(e){return i.customLog(sdkLib_1.LogLevel.ERROR,["\b\b\b\b\bJsBridge initialization fails, jsapi will not work"]),Promise.reject(e)});else{var n="Do not support the current environment："+e.platform;i.customLog(sdkLib_1.LogLevel.WARNING,[n]),i.bridgeInitFnPromise=Promise.reject(new Error(n))}return i.bridgeInitFnPromise};var n=function(e){void 0===e&&(e={}),i.devConfig=Object.assign(i.devConfig,e),!0===e.debug&&i.clearLogQueue(),e.extraPlatform&&i.setPlatform(e.extraPlatform)};this.exportSdk={config:function(o){void 0===o&&(o={});var t=!0;Object.keys(o).forEach(function(e){-1===["debug","usePromise"].indexOf(e)&&(t=!1)}),t?(i.customLog(sdkLib_1.LogLevel.WARNING,["This is a deprecated feature, recommend use dd.devConfig"]),n(o)):i.hadConfig?i.customLog(sdkLib_1.LogLevel.WARNING,["Config has been executed"]):(o.jsApiList&&(i.configJsApiList=o.jsApiList),i.hadConfig=!0,i.bridgeInitFn().then(function(n){var t=i.platformConfigMap[e.platform],r=o;t.authParamsDeal&&(r=t.authParamsDeal(r)),n(t.authMethod,r).then(function(e){i.isReady=!0,i.p.resolve(e)}).catch(function(e){i.isReady=!1,i.p.reject(e)})},function(){i.customLog(sdkLib_1.LogLevel.ERROR,['\b\b\b\b\bJsBridge initialization failed and "dd.config" failed to call'])}))},devConfig:n,ready:function(e){!1===i.hadConfig?(i.customLog(sdkLib_1.LogLevel.WARNING,["You don 't use a dd.config, so you don't need to wrap dd.ready, recommend remove dd.ready"]),i.bridgeInitFn().then(function(){e()})):i.config$.then(function(o){e()})},error:function(e){i.config$.catch(function(o){e(o)})},on:function(o,n){i.bridgeInitFn().then(function(){i.platformConfigMap[e.platform].event.on(o,n)})},off:function(o,n){i.bridgeInitFn().then(function(){i.platformConfigMap[e.platform].event.off(o,n)})},env:e,checkJsApi:function(o){void 0===o&&(o={});var n={};return o.jsApiList&&o.jsApiList.forEach(function(o){var t=i.invokeAPIConfigMapByMethod[o];if(t){var r=t[e.platform],s=getTargetApiConfigVS(r,e);s&&e.version&&sdkLib_1.compareVersion(e.version,s)&&(n[o]=!0)}n[o]||(n[o]=!1)}),Promise.resolve(n)},_invoke:function(e,o){return void 0===o&&(o={}),i.invokeAPI(e,o,!1)}}}return e}();exports.Sdk=Sdk;