/* 
    注册移动端通用服务
*/
(function (e) {
    var i = {}, t = {}, n = 0;
    e.rocNative = {
        __nativeCall: function (e, t, n) {
            var a = i[e];
            delete i[e], "function" == typeof a && a(t, n)
        }, 
        __callNative: function (t, a, o) {
            "function" == typeof o && (n++ , i[n] = o);
            var r = JSON.stringify({
                name: t,
                callbackId: "function" == typeof o ? n : -1,
                param: a === undefined ? {} : a
            });
            if (e.__rocAndroid) {
                e.__rocAndroid.postMessage(r)
                return true;
            }
            if (e.webkit && window.webkit.messageHandlers.rociOS) {
                window.webkit.messageHandlers.rociOS.postMessage(r)
                return true;
            }
            return false;
        },
        __onEvent: function (e, i) {
            if (e && i && "function" == typeof i) {
                (t[e] || (t[e] = [])).push(i)
            }
        },
        __clearEvent: function (e) {
            e && delete t[e]
        },
        __fireEvent: function (e, i) {
            var n = t[e];
            n && n.forEach(function (e) {
                e(i)
            })
        }, 
        on: function (e, i) {
            rocNative.__onEvent(e, i)
        }, 
        off: function (e) {
            rocNative.__clearEvent(e)
        }
    }, function () {
        Array.prototype.slice.apply(arguments).forEach(function (i) {
            e.rocNative[i] = function (t) {
                return new Promise(function (n, a) {
                    e.rocNative.__callNative(i, t, function (e, i) {
                        null !== e ? a({ error: e, result: i }) : n(i)
                    }) ? void 0 : n({})
                })
            }
        })
    }("toast", "pushView", "goBack", "appVersion", "appInit", "heartBeat", "getAppSetting", "setAppSetting")
})(window)
