// setupBridge(function(bridge) {

//     window["bridge"] = bridge

//     /*JS给ObjC提供公开的API，在ObjC端可以手动调用JS的这个API。接收ObjC传过来的参数，且可以回调ObjC*/
//     bridge.registerHandler('jsHandler', function(data, responseCallback) {

//         let json = JSON.parse(data)
//         let method = json['method']
//         let param = json['param']

//         let paramStr = JSON.stringify(param)
//         let evalMethod = method + "(paramStr)"

//         let result = eval(evalMethod);
//         responseCallback(result)

//     })

// })




// function setupBridge(callback) {

//     if (navigator.userAgent.indexOf('iPhone') > -1) {
//         setupIOSBridge(callback);
//     } else {
//         setupAndroidBridge(callback);
//     }
// }


// function setupAndroidBridge(callback) {
//     if (window.WebViewJavascriptBridge) {
//         callback(WebViewJavascriptBridge)
//     } else {
//         document.addEventListener(
//             'WebViewJavascriptBridgeReady',
//             function() {
//                 callback(WebViewJavascriptBridge)
//             },
//             false
//         );
//     }
// }


// function setupIOSBridge(callback) {
//     if (window.WebViewJavascriptBridge) {
//         return callback(WebViewJavascriptBridge);
//     }
//     if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
//     window.WVJBCallbacks = [callback];
//     var WVJBIframe = document.createElement('iframe');
//     WVJBIframe.style.display = 'none';
//     WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
//     document.documentElement.appendChild(WVJBIframe);
//     setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
// }



// function callHandler(param, method, callback) {
//     let bridge = window["bridge"]
//     let data = { 'method': method, 'param': param }

//     bridge.callHandler('apiHandler', data, function(responseData) {
//         callback(responseData)
//     })
// }

import device from './device'

function setupWebViewJavascriptBridge(callback) {
  if (window.WebViewJavascriptBridge) {
    return callback(window.WebViewJavascriptBridge);
  }
  if (!!device.android) {
    document.addEventListener(
      'WebViewJavascriptBridgeReady',
      function() {
          callback(WebViewJavascriptBridge)
      },
      false
    );
    return;
  }
  if (window.WVJBCallbacks) {
    return window.WVJBCallbacks.push(callback);
  }

  window.WVJBCallbacks = [callback];
  let WVJBIframe = document.createElement('iframe');
  WVJBIframe.style.display = 'none';
  WVJBIframe.src = 'https://__bridge_loaded__';
  document.documentElement.appendChild(WVJBIframe);
  setTimeout(() => {
    document.documentElement.removeChild(WVJBIframe);
  }, 0);
}

export default {
  callhandler(name, data, callback) {
    setupWebViewJavascriptBridge(function(bridge) {
      bridge.callHandler(name, data, callback);
    })
  },
  registerhandler(name, callback) {
    setupWebViewJavascriptBridge(function(bridge) {
      bridge.registerHandler(name, function(data, responseCallback) {
        callback(data, responseCallback);
      })
    })
  }
}
