/* eslint-disable */
(function () {
  // 鸿蒙应用中的LightJSBridge 适配
  if (process.env.ONLY_FOR === 'harmony') {
    try {
      var url = window.location.href
      if (window.LightJSBridge) {
        hsbridge.log('has loaded LightJSBridge!')
        return
      } else {
        hsbridge.log('first load LightJSBridge!')
      }
      window.LightJSBridge = {
        call: call,
        callSync: callSync,
        callbackFromNative: callbackFromNative
      }
      var callbacks = {}
      var uniqueId = 1
      function call(action, param, callback, pageid) {
        var callbackId = 'hsbcb_' + uniqueId++
        callbacks[callbackId] = callback
        hsbridge.exec(url, action, callbackId, JSON.stringify(param), pageid)
      }
      function callSync(action, param, pageid) {
        var data = hsbridge.exec(url, action, 'type_sync', JSON.stringify(param), pageid)
        return data
      }
      function callbackFromNative(callbackid, result, keepalive) {
        if (callbackid) {
          var callback = callbacks[callbackid]
          if (!callback) {
            return
          }
          callback(result)
          if (!keepalive) {
            delete callbacks[callbackid]
          }
        }
      }
      function pageShouldBack() {
        return 'true'
      }
      function sendDeviceReadyEvent() {
        var event = document.createEvent('Events')
        event.initEvent('deviceready', false, false)
        document.dispatchEvent(event)
      }
      sendDeviceReadyEvent()
    } catch (e) {
      console.warn('当前环境不支持 hsbridge 接口调用')
    }
  }
})()
