import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { initEnv } from '../env/env.mjs';
import { GAME_MAP } from '../game/game.mjs';
import { loader } from '../loader/little-loader.mjs';
import { closeMsdkWebview } from '../msdk/msdk.mjs';
import { composeUrlQuery } from '../url/url.mjs';
import '../constant/gid.mjs';
import '@babel/runtime/helpers/typeof';

var LAUNCH_GP_SOURCE_MAP = {
  NORMAL: 6,
  SUBWAY_MODE: 7
};
var GAME_SCHEME_PREFIX_MAP = {
  GP: 'pubgmhd1106467070://?',
  GP_IN_HELPER: 'wxc4c0253df149f02d://?',
  GP_TRIAL: 'pubgmhd1107050784://?',
  GN: 'gnyx1110329808://?type=Contest&',
  MJ: 'happymjscheme://matchFrom=1?'
};
var DEFAULT_WX_JS_SDK = 'https://res.wx.qq.com/open/js/jweixin-1.4.0.js';
var gidAppIdMap = _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, GAME_MAP.HLDDZ.GID, 'wx76fc280041c16519'), GAME_MAP.MJ.GID, 'wx3bef52104e238bff'), GAME_MAP.NARUTO.GID, 'wx82dd7436af5db835'), GAME_MAP.GP.GID, 'wxc4c0253df149f02d');
function getWxGameCircleUrl(gid) {
  var appId = gidAppIdMap[gid];
  return " https://game.weixin.qq.com/cgi-bin/h5/static/gamecenter/detail.html?appid=".concat(appId, "&ssid=39&autoinstall=1&type=1#wechat_redirect");
}
function getGPSchemeParam(roomId, roomPwd, options) {
  var time = Math.floor(new Date().getTime() / 60000);
  var schemeParam = "rmid:".concat(roomId, ",rmpw:").concat(roomPwd, ",t:").concat(time);
  if (options === null || options === void 0 ? void 0 : options.source) {
    schemeParam += ",source:".concat((options === null || options === void 0 ? void 0 : options.source) || '');
  }
  return schemeParam;
}
function getQRcodeUrl(launchParams) {
  var qrCodeUrl = composeUrlQuery(window.location.href, Object.assign({
    enterGame: true
  }, launchParams || {}));
  return qrCodeUrl;
}
function launchInWX(_ref) {
  var wxJSLink = _ref.wxJSLink,
    schemeUrl = _ref.schemeUrl,
    schemeParam = _ref.schemeParam,
    _ref$launchParams = _ref.launchParams,
    launchParams = _ref$launchParams === void 0 ? {} : _ref$launchParams,
    context = _ref.context,
    qrCodeLib = _ref.qrCodeLib,
    dialogHandler = _ref.dialogHandler,
    otherDialogParams = _ref.otherDialogParams,
    resolve = _ref.resolve,
    reject = _ref.reject;
  loader(wxJSLink, function () {
    var invokeWXBridge = function invokeWXBridge() {
      window.WeixinJSBridge.invoke('launchApplication', {
        schemeUrl: schemeUrl,
        extInfo: schemeParam,
        parameter: schemeParam
      }, function (res) {
        console.info('[launchCore wx] res: ', res);
        if (res.err_msg.indexOf('ok') === -1) {
          if ((res.err_msg.indexOf('permission') > -1 || res.err_msg.indexOf('access_denied') > -1) && qrCodeLib && dialogHandler) {
            showQRcode({
              launchParams: launchParams,
              context: context,
              qrCodeLib: qrCodeLib,
              dialogHandler: dialogHandler,
              otherDialogParams: otherDialogParams
            });
          } else {
            reject(res);
          }
        } else {
          resolve(res);
        }
      });
    };
    // 检查 WeixinJSBridge 是否已就绪
    if (typeof window.WeixinJSBridge !== 'undefined' && typeof window.WeixinJSBridge.invoke === 'function') {
      invokeWXBridge();
    } else {
      // 等待 WeixinJSBridgeReady 事件
      document.addEventListener('WeixinJSBridgeReady', function () {
        invokeWXBridge();
      }, false);
    }
  });
}
function showQRcode(_ref2) {
  var launchParams = _ref2.launchParams,
    context = _ref2.context,
    qrCodeLib = _ref2.qrCodeLib,
    dialogHandler = _ref2.dialogHandler,
    _ref2$otherDialogPara = _ref2.otherDialogParams,
    otherDialogParams = _ref2$otherDialogPara === void 0 ? {} : _ref2$otherDialogPara;
  var qrCodeUrl = getQRcodeUrl(launchParams || {});
  console.info('[showQRcode] url', qrCodeUrl);
  if (!dialogHandler || !qrCodeLib) return;
  qrCodeLib.toDataURL(qrCodeUrl).then(function (url) {
    dialogHandler.show(Object.assign({
      context: context,
      title: '提示',
      content: '请长按二维码再次打开链接并进入比赛',
      confirmText: '关闭',
      cancelText: '',
      src: url
    }, otherDialogParams || {}));
    console.info(url);
  })["catch"](function (err) {
    console.error(err);
  });
}
function launchCore(_ref3) {
  var _ref3$launchParams = _ref3.launchParams,
    launchParams = _ref3$launchParams === void 0 ? {} : _ref3$launchParams,
    schemeParam = _ref3.schemeParam,
    context = _ref3.context,
    qrCodeLib = _ref3.qrCodeLib,
    dialogHandler = _ref3.dialogHandler,
    otherDialogParams = _ref3.otherDialogParams,
    _ref3$schemePrefix = _ref3.schemePrefix,
    schemePrefix = _ref3$schemePrefix === void 0 ? '' : _ref3$schemePrefix,
    _ref3$wxJSLink = _ref3.wxJSLink,
    wxJSLink = _ref3$wxJSLink === void 0 ? 'https://res.wx.qq.com/open/js/jweixin-1.4.0.js' : _ref3$wxJSLink,
    _ref3$env = _ref3.env,
    env = _ref3$env === void 0 ? initEnv() : _ref3$env;
  // const time = Math.floor(new Date().getTime() / 60000);
  // const schemeParam = `rmid:${roomId},rmpw:${roomPwd},t:${time}`;
  var schemeUrl = "".concat(schemePrefix).concat(schemeParam);
  console.info('[launchCore schemeUrl]', schemeUrl);
  return new Promise(function (resolve, reject) {
    if (env.isWeixin) {
      launchInWX({
        wxJSLink: wxJSLink,
        schemeUrl: schemeUrl,
        schemeParam: schemeParam,
        launchParams: launchParams,
        context: context,
        qrCodeLib: qrCodeLib,
        dialogHandler: dialogHandler,
        otherDialogParams: otherDialogParams,
        resolve: resolve,
        reject: reject
      });
      return;
    }
    if (env.isInGame) {
      window.location.href = schemeUrl;
      setTimeout(function () {
        closeMsdkWebview(env);
      }, 1000);
      return;
    }
    window.location.href = schemeUrl;
    resolve(1);
  });
}

export { DEFAULT_WX_JS_SDK, GAME_SCHEME_PREFIX_MAP, LAUNCH_GP_SOURCE_MAP, getGPSchemeParam, getWxGameCircleUrl, launchCore, launchInWX };
