function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { sdkVersion } from "../../config";
import { createPreloadIframe } from "../service/container/utils";
import { ProductSceneEnum } from "../../types";
import { getWebAppUrl, getWebAppV2Url } from "../utils/web_app_url_utils";
import { ProductInfoCacheUtil } from "../utils/payment_product_utils";
import { getWebAppVersion } from "./payment_context_utils";
import { getSecurityConfigStorageKey, getSecurityHost, getSecurityScene, Security } from "../../util/security";
import { getOrSetStorageId, safeJson } from "../../util";
var MAX_CLEAR_TIME = 10000;
var PreloadHelper = /*#__PURE__*/function () {
  function PreloadHelper() {
    _classCallCheck(this, PreloadHelper);
  }
  _createClass(PreloadHelper, null, [{
    key: "preloadWebApp",
    value: function preloadWebApp(productScene, paymentMethodCategoryType) {
      var environment = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'prod';
      var useV2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
      try {
        if (this.isWebAppPreloaded) {
          console.log('[web-sdk] web app already preloaded');
          return;
        }
        var _this$buildWebAppPrel = this.buildWebAppPreloadUrl(productScene, paymentMethodCategoryType, environment, useV2),
          preloadUrl = _this$buildWebAppPrel.preloadUrl,
          containerId = _this$buildWebAppPrel.containerId;
        var preloadIframe = createPreloadIframe(containerId);
        preloadIframe.src = preloadUrl;
        var body = document.body || null;
        if (body) {
          console.log('[web-sdk] preload web app, url: ', preloadUrl);
          body.appendChild(preloadIframe);
          this.isWebAppPreloaded = true;
        }
        setTimeout(function () {
          var container = document.getElementById(containerId);
          if (container) {
            console.log('[web-sdk] remove preload web app');
            document.body.removeChild(container);
          }
        }, MAX_CLEAR_TIME);
      } catch (error) {
        console.error('[web-sdk] preload web app failed', error);
      }
    }
  }, {
    key: "preloadSecuritySDK",
    value: function preloadSecuritySDK(product) {
      var _this = this;
      var securityRegion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'SG';
      try {
        if (this.isSecuritySDKPreloaded) {
          console.log('[web-sdk] security sdk is already loaded');
          return;
        }
        var storage = PreloadHelper.getSecurityConfigStorage(product);
        var scene = storage.scene || getSecurityScene(product);
        console.log('getSecurityScene', scene);
        var h5gateway = storage.h5gateway || getSecurityHost(securityRegion);
        console.log('preloadSecuritySDK', scene);
        var securitySdk = new Security({
          scene: scene,
          h5gateway: h5gateway
        });
        securitySdk.initSecurity(function () {
          console.log(_this.securitySdkMap, 'this.securitySdkMap');
          _this.securitySdkMap.set(scene, securitySdk);
          _this.isSecuritySDKPreloaded = true;
          console.log('[web-sdk] preload security sdk succeed');
        }, function () {
          console.error('[web-sdk] preload security sdk failed');
        });
      } catch (error) {
        console.error('[web-sdk] preload security sdk failed', error);
      }
    }
  }, {
    key: "getSecuritySdk",
    value: function getSecuritySdk(scene) {
      var result = this.securitySdkMap.get(scene);
      return result;
    }
  }, {
    key: "buildWebAppPreloadUrl",
    value: function buildWebAppPreloadUrl(productScene, paymentMethodCategoryType, environment) {
      var useV2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
      var productSceneVersion = productScene === ProductSceneEnum.EASY_PAY ? '2.0' : '1.0';
      var previousProductInfo = ProductInfoCacheUtil.getPreviousProductInfo(productScene);
      if (previousProductInfo) {
        productSceneVersion = previousProductInfo === null || previousProductInfo === void 0 ? void 0 : previousProductInfo.productSceneVersion;
      }
      var containerId = "ams-checkout-component-".concat(productScene, "_").concat(paymentMethodCategoryType);
      var sdkMetaData = {
        productScene: productScene,
        sdkVersion: sdkVersion,
        webAppVersion: ''
      };
      sdkMetaData.webAppVersion = getWebAppVersion(sdkMetaData);
      var preloadUrlParams = {
        environment: environment,
        sdkMetaData: sdkMetaData,
        paymentMethodCategoryType: paymentMethodCategoryType,
        isPreload: true,
        productScene: productScene,
        productSceneVersion: productSceneVersion,
        query: undefined
      };
      var preloadUrl = useV2 ? getWebAppV2Url(preloadUrlParams) : getWebAppUrl(preloadUrlParams);
      return {
        preloadUrl: preloadUrl,
        containerId: containerId
      };
    }

    /**
     * @description Obtain risk control configuration in local storage
     */
  }, {
    key: "getSecurityConfigStorage",
    value: function getSecurityConfigStorage(product) {
      var scene = '';
      var h5gateway = '';
      var securityConfigStorage = getOrSetStorageId(getSecurityConfigStorageKey(product));
      if (securityConfigStorage) {
        var config = safeJson(securityConfigStorage, {});
        scene = config === null || config === void 0 ? void 0 : config.appName;
        h5gateway = config === null || config === void 0 ? void 0 : config.h5gateway;
      }
      return {
        scene: scene,
        h5gateway: h5gateway
      };
    }
  }]);
  return PreloadHelper;
}();
_defineProperty(PreloadHelper, "isWebAppPreloaded", false);
_defineProperty(PreloadHelper, "isSecuritySDKPreloaded", false);
_defineProperty(PreloadHelper, "securitySdkMap", new Map());
export { PreloadHelper as default };