"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.off=exports.on=void 0;var weex_1=require("./weex"),globalEvent=weex_1.requireModule("globalEvent"),weexNbEvent={isInitEvent:!1,eventsMap:{},RequestIDCacheMap:[],dispatchEvent:function(e){if(e){var t={func:e.name,data:e.data,pageId:e.pageId||"",viewId:e.viewId||"",clientId:""};e.clientId&&(t.clientId=e.clientId),weexNbEvent.dispatchData(t)}},addEventListener:function(e,t){weexNbEvent.isInitEvent||(weexNbEvent.isInitEvent=!0,globalEvent.addEventListener("__nb_bridge__",function(e){if(e&&e.message)try{var t=parseJSON(e.message);weexNbEvent.dispatchData(t)}catch(e){console.error("__nb_bridge__ data parse error",e)}})),weexNbEvent.eventsMap[e]||(weexNbEvent.eventsMap[e]=[]),weexNbEvent.eventsMap[e].push(t)},removeEventListener:function(e,t){var n=weexNbEvent.eventsMap[e];if(n)for(var a=n.length-1;a>=0;a--)n[a]===t&&n.splice(a,1)},dispatchData:function(e){console.log("receive push data",e);var t={param:e.param,pageId:e.pageId,viewId:e.viewId,clientId:e.clientId};e&&e.func?(t.eventName=e.func,weexNbEvent.doEventCallback(t)):e&&e.beforeunload?(t.eventName="beforeunload",t.param={},weexNbEvent.doEventCallback(t)):e&&null!=e.requestId?isFunction(weexNbEvent.RequestIDCacheMap[e.requestId])?(weexNbEvent.RequestIDCacheMap[e.requestId](e.param),delete weexNbEvent.RequestIDCacheMap[e.requestId]):console.log("unknown requestId",e):console.error("unknown push data",e)},doEventCallback:function(e){var t=e.eventName;if(t){var n=weexNbEvent.eventsMap[t];if(n&&n.length>0){var a={name:t};isObject(e.param)&&"android"===weex.config.env.platform.toLowerCase()?Object.assign(a,e.param):a.data=e.param,a.pageId=e.pageId,a.viewId=e.viewId,a.clientId=e.clientId,n.map(function(e){if(isFunction(e))try{e(a)}catch(e){console.error(e)}})}}}};exports.on=function(e,t){weexNbEvent.addEventListener(e,t)},exports.off=function(e,t){weexNbEvent.removeEventListener(e,t)};var isObject=function(e){return e&&"object"==typeof e},isFunction=function(e){return"function"==typeof e},parseJSON=function(e){try{e=JSON.parse(e)}catch(e){}return e};