import { initEnv } from '../env/env.mjs';
import { GAME_MAP } from '../game/game.mjs';
import { getUrlPara } from '../url/url.mjs';
import { launchInWX, getWxGameCircleUrl } from './helper.mjs';
import '../constant/gid.mjs';
import '@babel/runtime/helpers/typeof';
import '@babel/runtime/helpers/defineProperty';
import '../loader/little-loader.mjs';
import '../msdk/msdk.mjs';

function ddzScheme(_ref) {
  var gameId = _ref.gameId,
    seriesId = _ref.seriesId,
    uin = _ref.uin,
    env = _ref.env;
  var schemeHost = env.isIOS ? 'wx76fc280041c16519' : 'qqgame.hlddz.scheme';
  var gameUrl = 'https://hlddz.huanle.qq.com/web/play.html?ADTAG=changshangsai&LoginType=Both&DefaultLoginType=WeChat&ParamType=OfflineMatch';
  if (gameId || seriesId) {
    gameUrl += "&SeriesID=".concat(seriesId, "&GameID=").concat(gameId);
  }
  var schemeParam = "a=1&Operation=OfflineMatch&SeriesID=".concat(seriesId, "&GameID=").concat(gameId);
  if (uin) {
    gameUrl += "&uin=".concat(uin);
    schemeParam += "&uin=".concat(uin);
  }
  var schemeUrl = "".concat(schemeHost, "://startapp?").concat(schemeParam);
  return {
    schemeUrl: schemeUrl,
    schemeParam: schemeParam,
    gameUrl: gameUrl
  };
}
/**
 * 拉起 DDZ
 * @param {object} params 拉起参数
 * @param {string} params.seriesId series id
 * @param {string} params.gameId game id
 * @param {string} params.uin uin
 * @param {object} [params.context] 上下文，可传入组件实例 this
 * @param {object} [params.qrCodeLib] qrcode
 * @param {object} [params.dialogHandler] 弹窗 handler
 * @param {object} [params.otherDialogParams] 弹窗的其他参数
 * @param {string} [params.wxJSLink] wx js link
 * @param {object} [params.env] 环境对象
 * @returns Promise<boolean | number>
 *
 * @example
 * ```ts
 * launchDDZGameRoom({
 *   seriesId: '12',
 *   gameId: '123',
 *   uin: '222',
 * })
 * ```
 */
var launchDDZGameRoom = function launchDDZGameRoom(_ref2) {
  var _ref2$seriesId = _ref2.seriesId,
    seriesId = _ref2$seriesId === void 0 ? '' : _ref2$seriesId,
    _ref2$gameId = _ref2.gameId,
    gameId = _ref2$gameId === void 0 ? '' : _ref2$gameId,
    _ref2$uin = _ref2.uin,
    uin = _ref2$uin === void 0 ? '' : _ref2$uin,
    context = _ref2.context,
    qrCodeLib = _ref2.qrCodeLib,
    dialogHandler = _ref2.dialogHandler,
    otherDialogParams = _ref2.otherDialogParams,
    _ref2$wxJSLink = _ref2.wxJSLink,
    wxJSLink = _ref2$wxJSLink === void 0 ? 'https://res2.wx.qq.com/open/js/jweixin-1.6.0.js' : _ref2$wxJSLink,
    _ref2$env = _ref2.env,
    env = _ref2$env === void 0 ? initEnv() : _ref2$env;
  var _ddzScheme = ddzScheme({
      gameId: gameId,
      seriesId: seriesId,
      uin: uin,
      env: env
    }),
    schemeUrl = _ddzScheme.schemeUrl,
    schemeParam = _ddzScheme.schemeParam,
    gameUrl = _ddzScheme.gameUrl;
  console.info('[launchDDZGameRoom] schemeUrl: ', schemeUrl);
  console.info('[launchDDZGameRoom] env: ', env);
  return new Promise(function (resolve, _reject) {
    if (env.isWeixin) {
      launchInWX({
        wxJSLink: wxJSLink,
        schemeUrl: schemeUrl,
        schemeParam: schemeParam,
        launchParams: {
          gameId: gameId,
          seriesId: seriesId,
          uin: uin
        },
        context: context,
        qrCodeLib: qrCodeLib,
        dialogHandler: dialogHandler,
        otherDialogParams: otherDialogParams,
        resolve: resolve,
        reject: function reject() {
          _reject();
          window.location.href = getWxGameCircleUrl(GAME_MAP.HLDDZ.GID);
        }
      });
      return;
    }
    var fakeLink = "fakelink://&Operation=OfflineMatch&SeriesID=".concat(seriesId, "&GameID=").concat(gameId);
    if (uin) {
      fakeLink += "&uin=".concat(uin);
    }
    var ddzInGame = window.sessionStorage.getItem('ddz_ingame');
    if (ddzInGame === '1' || !getUrlPara('msdkEncodeParam') && env.isMsdk) {
      console.info('[launchDDZGameRoom] fakeLink: ', fakeLink);
      if (env.isIOS) {
        // @ts-ignore
        window.location = fakeLink;
      } else {
        confirm(fakeLink);
      }
      return;
    }
    if (env.isInGame) {
      var js = "{\"MsdkMethod\":\"WGSendMessageToNative\",\"MsgData\":\"".concat(fakeLink, "\"}");
      setTimeout(function () {
        // 延迟关闭游戏webview
        if (env.isMsdk && typeof window.msdkShare === 'function' && typeof window.msdkCloseWebview === 'function') {
          window.msdkShare(js);
          window.msdkCloseWebview();
        }
      }, 1);
      return;
    }
    window.location.href = gameUrl;
    resolve(1);
  });
};

export { launchDDZGameRoom };
