"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");exports.__esModule=!0,exports.default=exports.PLATFORM_TYPE_ENUM=exports.Container_Type_Enum=exports.CONTINUOUS_EVENT_LIST=exports.BRIDGE_ERROR_CODE=exports.API_INVOKER_TYPE=void 0;var BRIDGE_ERROR_CODE,API_INVOKER_TYPE,PLATFORM_TYPE_ENUM,CONTINUOUS_EVENT_LIST,Container_Type_Enum,_regenerator=_interopRequireDefault(require("@babel/runtime/regenerator")),_extends2=_interopRequireDefault(require("@babel/runtime/helpers/extends")),_asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")),_common=require("./utils/common");exports.BRIDGE_ERROR_CODE=BRIDGE_ERROR_CODE,function(e){e.CANCEL="-1",e.SUCCESS="0",e.API_UNDEFINED="1",e.INVALID_PARAMS="2",e.UNKNOWN_ERROR="3",e.UNAUTHORIZED_CALL="4",e.WRONG_CORP_ID="5",e.CREATE_CHAT_FAILED="6",e.UNAUTHORIZED_API="7",e.INVALID_CORP_ID="8",e.SERVER_RESPONSE_ERROR="9",e.WRONG_DEVICE_INFO="10",e.UPLOAD_FAIL="11",e.PROCESS_FAIL="12",e.DUPLICATED_CALL="13",e.TOO_LARGE_PIC="14",e.REQUEST_REJECT_OR_INSECURE_REQUEST="15",e.PC_NOT_ALLOWED_TO_OPEN_SIDE_PANE_OR_MODAL="21",e.PC_CLOSE_SIDE_PANE_OR_MODAL="22",e.UNAUTHORIZED_PARAMS="23",e.GESTURE_PASSWORD_DOES_NOT_EXIST="24",e.NETWORK_ERROR="25"}(BRIDGE_ERROR_CODE||(exports.BRIDGE_ERROR_CODE=BRIDGE_ERROR_CODE={})),exports.API_INVOKER_TYPE=API_INVOKER_TYPE,function(e){e.MOBILE="mobile",e.PC="pc",e.MINI_APP="mini",e.UNKNOWN="unknown"}(API_INVOKER_TYPE||(exports.API_INVOKER_TYPE=API_INVOKER_TYPE={})),exports.PLATFORM_TYPE_ENUM=PLATFORM_TYPE_ENUM,function(e){e.ANDROID="android",e.IOS="ios",e.UNKNOW="unknow"}(PLATFORM_TYPE_ENUM||(exports.PLATFORM_TYPE_ENUM=PLATFORM_TYPE_ENUM={})),exports.CONTINUOUS_EVENT_LIST=CONTINUOUS_EVENT_LIST,function(e){e.UPDATE_NETWORK_STATUS="DINGGOV_ON_NETWORK_TYPE_CHANGED",e.UPDATE_LOCATION="DINGGOV_GEO_LOCATION_UPDATE",e.UPDATE_TRACE="DINGGOV_TRACE_UPDATE",e.ON_SHAKE="onShake"}(CONTINUOUS_EVENT_LIST||(exports.CONTINUOUS_EVENT_LIST=CONTINUOUS_EVENT_LIST={})),exports.Container_Type_Enum=Container_Type_Enum,function(e){e.isDingTalk="DingTalk",e.isMpaas="mPaaS",e.isUnknow="unknow"}(Container_Type_Enum||(exports.Container_Type_Enum=Container_Type_Enum={}));var ua=navigator&&(navigator.swuserAgent||navigator.userAgent)||"",Invoker=function(){function e(){this.readyFnStack=[],this.generalEventCallbackStack={},this.apiList={},this.continuousCallbackStack={},this.isH5Mobile=null,this.appType=null,this.platformType=null,this.aliBridge=window&&window.navigator&&window.AlipayJSBridge,this.isReady=!1,this.init(),console.warn("请将 gdt-jsapi 版本请升级到 1.9.24 版本以上的最新版本,谢谢")}var n=e.prototype;return n.h5AndroidbridgeInit=function(){var e=this;this.h5BridgeReadyPromise=new Promise((function(n,i){var t=function(){try{window.WebViewJavascriptBridgeAndroid=window.nuva&&window.nuva.require(),e.execReadyFn()}catch(e){}};window.nuva&&(void 0===window.nuva.isReady||window.nuva.isReady)?t():(document.addEventListener("runtimeready",(function(){t()}),!1),document.addEventListener("runtimefailed",(function(t){var r=t&&t.detail||{errorCode:BRIDGE_ERROR_CODE.INVALID_PARAMS,errorMessage:"unknown nuvajs bootstrap error"};e.handleBridgeResponse(r,n,i)}),!1))}))},n.h5IosBridgeInit=function(){var e=this;this.h5BridgeReadyPromise=new Promise((function(n,i){if("undefined"!=typeof WebViewJavascriptBridge)try{WebViewJavascriptBridge.init((function(e,n){})),e.execReadyFn()}catch(e){}else document.addEventListener("WebViewJavascriptBridgeReady",(function(){try{WebViewJavascriptBridge&&WebViewJavascriptBridge.init((function(e,n){})),e.execReadyFn()}catch(e){}}),!1)}))},n.init=function(){var e=this,n=this.getAppType(),i=this.getContainerType();if(n===API_INVOKER_TYPE.PC&&window.dingtalk&&!window.dingtalk.isRegister&&(window.dingtalk.isRegister=!0,window.dingtalk.callbackStack={},window.dingtalk.event.register((function(n,i){if(e.continuousCallbackStack[n])e.continuousCallbackStack[n](i);else if(i){var t=""+i.msgId;"openapi.event.emit"===n?(console.log("dingtalk receive event:",i,"identifer is",t),window.dingtalk.callbackStack[t]&&(window.dingtalk.callbackStack[t](i),delete window.dingtalk.callbackStack[t])):"im.fileTask.addNewTask"===n||"im.fileTask.updateTask"===n?(i.msgId||i.taskId)&&"function"==typeof e.continuousCallbackStack[i.msgId||i.taskId]&&e.continuousCallbackStack[i.msgId||i.taskId](n,i):e.generalEventCallbackStack[n]&&e.generalEventCallbackStack[n].forEach((function(n){n.call(e,i)}))}}))),n===API_INVOKER_TYPE.MOBILE){if(i===Container_Type_Enum.isDingTalk)this.platformType===PLATFORM_TYPE_ENUM.ANDROID?!this.h5BridgeReadyPromise&&this.h5AndroidbridgeInit():this.platformType===PLATFORM_TYPE_ENUM.IOS&&!this.h5BridgeReadyPromise&&this.h5IosBridgeInit();else if(i===Container_Type_Enum.isMpaas&&n===API_INVOKER_TYPE.MOBILE)if(window.AlipayJSBridge)this.execReadyFn();else{var t=setTimeout((function(){console.warn("window.AlipayJSBridge 未初始化完毕,走到兜底逻辑",e.isReady,window.AlipayJSBridge),e.isReady||e.execReadyFn.call(e)}),5200);document.addEventListener("AlipayJSBridgeReady",(function(){e.isReady||(clearTimeout(t),e.execReadyFn.call(e))}),!1)}}else setTimeout((function(){e.execReadyFn()}))},n.execReadyFn=function(){this.isReady=!0;for(var e=this.readyFnStack.shift();e;)e&&e(this),e=this.readyFnStack.shift()},n.onReady=function(e){this.isReady?e&&e(this):this.readyFnStack.push(e)},n.setCurrentInvoker=function(e){this.currentInvoker=e},n.getCurrentInvoker=function(){return this.currentInvoker},n.getBridge=function(){return this.aliBridge},n.getContainerType=function(){return/TaurusApp/g.test(ua)?/DingTalk/g.test(ua)?Container_Type_Enum.isDingTalk:Container_Type_Enum.isMpaas:/DingTalk/g.test(ua)?Container_Type_Enum.isDingTalk:/mPaaSClient/g.test(ua)||/Nebula/g.test(ua)?Container_Type_Enum.isMpaas:Container_Type_Enum.isUnknow},n.getAppType=function(){return this.appType||(this.isMobile()?this.appType=API_INVOKER_TYPE.MOBILE:window&&window.navigator&&window.navigator.userAgent&&window.navigator.userAgent.indexOf("dingtalk-win")>=0&&window.navigator.userAgent.indexOf("TaurusApp")>=0?this.appType=API_INVOKER_TYPE.PC:(0,_common.isMiniApp)()?this.appType=API_INVOKER_TYPE.MINI_APP:(console.warn("检测到页面在非专有钉钉客户端中打开,JSAPI 调用可能不会生效!"),this.appType=API_INVOKER_TYPE.UNKNOWN)),this.appType},n.isMobile=function(){var e=/iPhone|iPad|iPod|iOS/i.test(ua),n=/Android/i.test(ua),i=window&&window.navigator&&window.navigator.userAgent||"";return null!==this.isH5Mobile?this.isH5Mobile:i&&i.indexOf("dingtalk-win")>=0?(this.isH5Mobile=!1,!1):!(!i||!(i.includes("mPaaSClient")||i.includes("Nebula")||i.includes("DingTalk")))&&(this.isH5Mobile=!0,this.platformType=e?PLATFORM_TYPE_ENUM.IOS:n?PLATFORM_TYPE_ENUM.ANDROID:PLATFORM_TYPE_ENUM.UNKNOW,!0)},n.registerEvent=function(e,n){var i=this;if("function"==typeof n)return this.getAppType()===API_INVOKER_TYPE.PC?(this.generalEventCallbackStack[e]||(this.generalEventCallbackStack[e]=[]),this.generalEventCallbackStack[e].push(n),function(){var t=i.generalEventCallbackStack[e].findIndex((function(e){return e===n}));i.generalEventCallbackStack[e].splice(t,1)}):this.getAppType()===API_INVOKER_TYPE.MOBILE?(document.addEventListener(e,n,!1),function(){document.removeEventListener(e,n)}):void 0;console.error("callback 参数应该为函数")},n.registerClientAPI=function(e,n){this.apiList[e]=n},n.registerAPI=function(e,n){this.isMobile();if("object"==typeof n){var i=n,t=this.getAppType();this.registerClientAPI(e,i[t])}else this.registerClientAPI(e,n)},n.invokeMiniApp=function(){var e=(0,_asyncToGenerator2.default)(_regenerator.default.mark((function e(n,i){var t=this;return _regenerator.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return void 0===i&&(i={}),e.abrupt("return",new Promise((function(e,r){i=(0,_extends2.default)({_apiName:n},i);var a=t.apiList[n],o=t.getContainerType();if(!a)return console.warn("API: "+n+",未注册"),r("API: "+n+",未注册");if(o===Container_Type_Enum.isMpaas){if("function"==typeof a)return void a.call(null,i,{context:my,resolve:e,reject:r,methodName:n});my.call(n,i,(function(n){t.handleBridgeResponse(n,e,r)}))}else if(o===Container_Type_Enum.isDingTalk){if("function"==typeof a)return void a.call(null,i,{context:dd.dtBridge,resolve:e,reject:r,methodName:n,containerType:o,appType:API_INVOKER_TYPE.MINI_APP});dd.dtBridge({m:"taurus.common."+n,args:i,onSuccess:function(n){t.handleBridgeResponse(n,e,r)},onFail:function(n){t.handleBridgeResponse(n,e,r)}})}})));case 2:case"end":return e.stop()}}),e)})));return function(n,i){return e.apply(this,arguments)}}(),n.invokeMobile=function(){var e=(0,_asyncToGenerator2.default)(_regenerator.default.mark((function e(n,i,t){var r=this;return _regenerator.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return void 0===i&&(i={}),e.abrupt("return",new Promise((function(e,a){i=(0,_extends2.default)({_apiName:n},i);var o=r.apiList[n],s=r.getContainerType();if(!o)return console.warn("API: "+n+",未注册"),a("API: "+n+",未注册");if(s===Container_Type_Enum.isDingTalk){if(r.platformType===PLATFORM_TYPE_ENUM.IOS){var u=Object.assign({},i);if(!0===u.watch&&"undefined"!=typeof WebViewJavascriptBridge&&WebViewJavascriptBridge.registerHandler(null!=t&&t.dingTalkAPIName?null==t?void 0:t.dingTalkAPIName:"taurus.common."+n,(function(e,n){"function"==typeof i.onSuccess&&i.onSuccess.call(null,e),n&&n({errorCode:"0",errorMessage:"success"})})),"function"==typeof o)return void o.call(null,i,{context:window.WebViewJavascriptBridge,resolve:e,reject:a,methodName:n,containerType:s,appType:API_INVOKER_TYPE.MOBILE,platformType:PLATFORM_TYPE_ENUM.IOS,watch:u.watch});void 0!==window.WebViewJavascriptBridge&&window.WebViewJavascriptBridge.callHandler("taurus.common."+n,Object.assign({},u),(function(n){!u.watch&&r.handleBridgeResponse(n||{},e,a)}))}else if(r.platformType===PLATFORM_TYPE_ENUM.ANDROID){var c=n.split("."),d=c.pop()||"",l=c.join(".")||"taurus.common";if("function"==typeof o)return void o.call(null,i,{context:window.WebViewJavascriptBridgeAndroid,resolve:e,reject:a,methodName:n,containerType:s,appType:API_INVOKER_TYPE.MOBILE,platformType:PLATFORM_TYPE_ENUM.ANDROID});"function"==typeof window.WebViewJavascriptBridgeAndroid&&window.WebViewJavascriptBridgeAndroid((function(n){r.handleBridgeResponse(n,e,a)}),(function(n){r.handleBridgeResponse(n,e,a)}),l,d,i)}}else if(s===Container_Type_Enum.isMpaas){if("function"==typeof o)return void o.call(null,i,{context:AlipayJSBridge,resolve:e,reject:a,methodName:n});AlipayJSBridge.call(n,i,(function(n){r.handleBridgeResponse(n,e,a)}))}})));case 2:case"end":return e.stop()}}),e)})));return function(n,i,t){return e.apply(this,arguments)}}(),n.findFitMsgId=function(e){var n,i;return null!==(n=window.dingtalk)&&void 0!==n&&null!==(i=n.callbackStack)&&void 0!==i&&i[e]?this.findFitMsgId(e+1):e},n.invokePC=function(){var e=(0,_asyncToGenerator2.default)(_regenerator.default.mark((function e(n,i,t){var r=this;return _regenerator.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return void 0===i&&(i={}),void 0===t&&(t={msgId:1}),e.abrupt("return",new Promise((function(e,a){try{i=(0,_extends2.default)({_apiName:n},i);var o=r.findFitMsgId(Date.now()),s=t.pcClientAPIName||n;if(t.msgId=o,!window.dingtalk)return Promise.reject(new Error("请在钉钉容器内使用 JSAPI"));r.apiList[n]?r.apiList[n].call(null,i,t):(console.info("invoke bridge api:",s,o,i),window.dingtalk.platform.invokeAPI(o,s,i)),window.dingtalk&&window.dingtalk.isRegister&&!window.dingtalk.callbackStack&&(window.dingtalk.callbackStack={}),window.dingtalk.callbackStack[""+o]=function(n){var i=n;return i.body?e(i.body):e(i)}}catch(e){a(e)}})));case 3:case"end":return e.stop()}}),e)})));return function(n,i,t){return e.apply(this,arguments)}}(),n.handleBridgeResponse=function(e,n,i){e&&e.errorCode?e.errorCode===BRIDGE_ERROR_CODE.SUCCESS?n(e.result):(console.warn("API 调用失败",e),i(e)):e&&"false"===e.success?i(e):n(e)},n.invoke=function(){var e=(0,_asyncToGenerator2.default)(_regenerator.default.mark((function e(n,i,t){var r;return _regenerator.default.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(void 0===i&&(i={}),(r=this.getAppType())!==API_INVOKER_TYPE.MOBILE){e.next=8;break}if(this.isReady){e.next=5;break}return e.abrupt("return",Promise.reject("错误:请在 dd.ready() 回调中使用 JSAPI,当前调用函数:"+n));case 5:return e.abrupt("return",this.invokeMobile(n,i,t));case 8:if(r!==API_INVOKER_TYPE.PC){e.next=12;break}return e.abrupt("return",this.invokePC(n,i,t));case 12:if(r!==API_INVOKER_TYPE.MINI_APP){e.next=16;break}return e.abrupt("return",this.invokeMiniApp(n,i));case 16:return e.abrupt("return",Promise.reject("错误:未在钉钉运行环境下调用该 API,无效,请检查运行环境"));case 17:case"end":return e.stop()}}),e,this)})));return function(n,i,t){return e.apply(this,arguments)}}(),n.existEventListener=function(e){return!!this.continuousCallbackStack[e]},n.registerContinuesEvent=function(e,n){this.continuousCallbackStack[e]=n},n.removeContinuesEvent=function(e){this.existEventListener(e)&&(this.continuousCallbackStack[e](),delete this.continuousCallbackStack[e])},e}();(0,_common.isMiniApp)()||(window._invoker=window._invoker||new Invoker);var _default=(0,_common.isMiniApp)()?new Invoker:window._invoker;exports.default=_default;