import _typeof from '@babel/runtime/helpers/typeof';
import { initEnv, getEnvUAType } from '../env/env.mjs';
import { loader } from '../loader/little-loader.mjs';

/**
 * MSDK 浏览器中，向原生发送数据
 * @param {string} data 发送的数据
 * @example
 * ```ts
 * sendToMsdkNative('123');
 * sendToMsdkNative(JSON.stringify({ method: 'foo', data: 'bar' }));
 * ```
 */
function sendToMsdkNative() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var env = initEnv();
  if (env.isMsdk && !env.isMsdkV5) {
    if (typeof window.msdkCallNative === 'function') {
      window.msdkCallNative(data);
    } else if (typeof window.msdkCall === 'function') {
      window.msdkCall('WGSendMessageToNative', "{\"MsdkMethod\":\"WGSendMessageToNative\",\"MsgData\":\"".concat(data, "\"}"));
    }
  } else if (env.isAndroid) {
    confirm(data);
  } else if (env.isIOS) {
    window.location.href = encodeURIComponent(data);
  }
}
/**
 * 添加 MSDK 原生回调监听器
 * 用于监听原生层发送给 Web 层的消息
 * @param callback - 回调函数，接收原生层传递的数据
 * @example
 * ```ts
 * addMsdkNativeCallbackListener((data) => {
 *   console.log('收到原生消息:', data);
 * });
 * ```
 */
function addMsdkNativeCallbackListener(callback) {
  var env = initEnv();
  if (env.isMsdk && !env.isMsdkV5) {
    if (typeof window.setMsdkCallback === 'function') {
      window.setMsdkCallback(callback);
    } else if (typeof window.msdkAddNativeCallbackObserver === 'function') {
      window.msdkAddNativeCallbackObserver(callback);
    }
  }
}
/**
 * 移除 MSDK 原生回调监听器
 * 取消监听原生层发送给 Web 层的消息
 * @param callback - 要移除的回调函数
 * @example
 * ```ts
 * const callback = (data) => console.log(data);
 * addMsdkNativeCallbackListener(callback);
 * // 稍后移除
 * removeMsdkNativeCallbackListener(callback);
 * ```
 */
function removeMsdkNativeCallbackListener(callback) {
  var env = initEnv();
  if (env.isMsdk && !env.isMsdkV5) {
    if (typeof window.msdkRemoveNativeCallbackObserver === 'function') {
      window.msdkRemoveNativeCallbackObserver(callback);
    }
  }
}
/**
 * MSDK 浏览器中，关闭 webView
 * @example
 * ```ts
 * closeMsdkWebview()
 * ```
 */
function closeMsdkWebview(env) {
  if (!env) {
    env = initEnv();
  }
  console.info('[closeMsdkWebview] env', env);
  // ua需要先判断MSDK V5版本
  if (env.isMsdkV5 && typeof window.msdkCall === 'function') {
    var _closeWebView = '{"MsdkMethod":"closeWebView"}';
    window.msdkCall(_closeWebView);
  } else if (env.isMsdk) {
    if (typeof window.msdkCloseWebview === 'function') {
      if (!env.isIOS || env.isIOS && window.msdkiOSHandler) {
        window.msdkCloseWebview();
      }
    } else if (typeof window.msdkCallNative === 'function') {
      window.msdkCallNative('{"MsdkMethod":"CloseWebview"}');
    }
  } else if (env.isSlugSdk && window.customBrowserInterface) {
    window.customBrowserInterface.closeWebview();
  }
}
/**
 * 关闭 webView，包含 msdk 浏览器和其他浏览器
 * @example
 * ```ts
 * closeWebView()
 * ```
 */
function closeWebView() {
  console.log('[closeWebView] call close webview');
  var env = initEnv();
  if (env.isMsdk) {
    closeMsdkWebview();
  } else if (env.isSlugSdk) {
    if (window.customBrowserInterface) {
      window.customBrowserInterface.closeWebview();
    }
  } else {
    window.location.href = 'about:blank';
    console.log('[closeWebView] call close webview failed');
  }
}
/**
 * 添加游戏内浏览器jssdk
 * @example
 * ```ts
 * callJsBrowserAdapter();
 * ```
 */
function callJsBrowserAdapter() {
  return new Promise(function (resolve) {
    var env = initEnv();
    if (env.isMsdkV5) {
      loader('https://image-1251917893.file.myqcloud.com/igame/common/js/msdkJsAdapterV5.js', function () {
        resolve(true);
      });
    } else if (env.isMsdk) {
      if (env.isMsdkX) {
        loader('https://image-1251917893.file.myqcloud.com/igame/common/js/msdkJsAdapterV3_embedded.js', function () {
          resolve(true);
        });
      } else {
        loader('https://image-1251917893.file.myqcloud.com/igame/common/js/msdkJsAdapterV3.js', function () {
          resolve(true);
        });
      }
    } else if (env.isSlugSdk) {
      loader('https://image-1251917893.file.myqcloud.com/igame/common/js/browser_adapt_20240702.js', function () {
        resolve(true);
      });
    }
  });
}
/**
 * 设置 MSDK 浏览器退出全屏，需提前加载 sdk
 * @example
 * ```ts
 * callJsReSetFullScreen();
 * ```
 */
var callJsReSetFullScreen = function callJsReSetFullScreen() {
  callJsSetFullScreen(false);
};
/**
 * 设置 MSDK 浏览器全屏，需提前加载 sdk
 * @param isFullScreen 是否全屏
 * @example
 * ```ts
 * callJsSetFullScreen();
 * callJsSetFullScreen(false);
 * ```
 */
var callJsSetFullScreen = function callJsSetFullScreen() {
  var isFullScreen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  var _a, _b;
  var _getEnvUAType = getEnvUAType(),
    isSlugSdk = _getEnvUAType.isSlugSdk,
    isMsdkV5 = _getEnvUAType.isMsdkV5,
    isMsdk = _getEnvUAType.isMsdk,
    isMsdkX = _getEnvUAType.isMsdkX,
    isAndroid = _getEnvUAType.isAndroid;
  if (isSlugSdk) {
    if (_typeof(window.customBrowserInterface) === 'object') {
      var method = isFullScreen ? 'hideUi' : 'showUi';
      window.customBrowserInterface[method]();
    }
  } else if (isMsdkV5) {
    var setFullScreenStr = JSON.stringify({
      MsdkMethod: 'setFullScreen',
      isFullScreen: !!isFullScreen
    });
    // 延时设置全屏
    setTimeout(function () {
      var _a, _b;
      if (isAndroid) {
        (_a = window.msdkCall) === null || _a === void 0 ? void 0 : _a.call(window, setFullScreenStr);
      } else {
        if (window.WebViewJavascriptBridge) {
          (_b = window.msdkCall) === null || _b === void 0 ? void 0 : _b.call(window, setFullScreenStr);
        } else {
          document.addEventListener('WebViewJavascriptBridgeReady', function () {
            var _a;
            (_a = window.msdkCall) === null || _a === void 0 ? void 0 : _a.call(window, setFullScreenStr);
          }, false);
        }
      }
    }, 100);
  } else if (isMsdk && !isMsdkX) {
    var _setFullScreenStr = JSON.stringify({
      MsdkMethod: 'WGSetFullScreen',
      isFullScreen: !!isFullScreen
    });
    if (isAndroid) {
      (_a = window.msdkCall) === null || _a === void 0 ? void 0 : _a.call(window, 'WGSetFullScreen', _setFullScreenStr);
    } else {
      if (window.WebViewJavascriptBridge) {
        (_b = window.msdkCall) === null || _b === void 0 ? void 0 : _b.call(window, 'WGSetFullScreen', _setFullScreenStr);
      } else {
        document.addEventListener('WebViewJavascriptBridgeReady', function () {
          var _a;
          (_a = window.msdkCall) === null || _a === void 0 ? void 0 : _a.call(window, 'WGSetFullScreen', _setFullScreenStr);
        }, false);
      }
    }
  }
};

export { addMsdkNativeCallbackListener, callJsBrowserAdapter, callJsReSetFullScreen, callJsSetFullScreen, closeMsdkWebview, closeWebView, removeMsdkNativeCallbackListener, sendToMsdkNative };
