import { ERRORMESSAGE } from "../constant";
import CallApp from "./intl-callapp/es/main";
import { terminalTypeEnum } from "../types";
import { device } from ".";
export function redirect(data) {
  return new Promise(function (resolve, reject) {
    var terminalType = device.isMobile ? terminalTypeEnum.WAP : terminalTypeEnum.WEB;
    var isWeb = terminalType === terminalTypeEnum.WEB;
    var normalUrl = (data === null || data === void 0 ? void 0 : data.normalUrl) || '';
    var schemeUrl = (data === null || data === void 0 ? void 0 : data.schemeUrl) || '';
    var applinkUrl = (data === null || data === void 0 ? void 0 : data.applinkUrl) || '';
    var callAppJudgeTime = (data === null || data === void 0 ? void 0 : data.callAppJudgeTime) || '';
    var jumpFunc = function jumpFunc() {
      if (normalUrl) {
        resolve();
        window.location.href = normalUrl;
        return;
      }
      reject(ERRORMESSAGE.SDK_LAUNCH_PAYMENT_APP_ERROR.CALL_APP_URL_ERROR);
    };
    var callAppFunc = function callAppFunc() {
      if (!schemeUrl && !applinkUrl) {
        jumpFunc();
        return;
      }
      var config = {
        fallback: function fallback() {
          jumpFunc();
        },
        successCb: function successCb() {
          resolve();
        },
        delay: 0
      };
      if (callAppJudgeTime) {
        config['resultJudgmentTime'] = callAppJudgeTime;
      }
      try {
        var callLib = new CallApp(config);
        callLib.open({
          scheme: schemeUrl,
          link: applinkUrl
        });
      } catch (error) {
        jumpFunc();
      }
    };
    if (isWeb) {
      jumpFunc();
    } else {
      callAppFunc();
    }
  });
}