// p0 functions
/* global Service */
/* eslint-disable no-unused-vars */
import nativeApi from './nativeApi';
import { showSystemLog } from './nativeApi/util';

showSystemLog && console.log('bl app');

Service.callbacks = {};
Service.callbacks.lastCallback = {};

Service.invokeCallback = (result, callbackId) => {
    if (!callbackId) {
        return;
    }

    showSystemLog && console.log('invokeCallback', callbackId, result);
    const callbackFn = Service.callbacks[callbackId];
    let callbackBizFn = null;
    if (Service.bizCallback) callbackBizFn = Service.bizCallback[callbackId];
    const callbackResponseFn = Service.callbacks.lastCallback[callbackId];
    if (callbackFn) {
        callbackFn(result);
        delete Service.callbacks[callbackId];
    } else if (callbackBizFn){
        // 针对开放给内部特殊能力的回调,订阅事件
        callbackBizFn(result);
    } else if (callbackResponseFn) {
        // 使用最后一次callback。单独在callback中挂在口上一个变量
        callbackResponseFn(result);
        delete Service.callbacks.lastCallback[callbackId];
    }
};

const bl = nativeApi;

export default bl;
