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 { EVENT } from "../../../../constant";
import { ServiceProvider } from "./../../../../foundation/service";
import { isElementPC, isElementPad } from "../util";
import { DisplayTypeEnum, PlatformEnum, ProductSceneEnum } from "./../../../../types";
export var IContainerStatus = /*#__PURE__*/function (IContainerStatus) {
  IContainerStatus["UNINIT"] = "UNINIT";
  IContainerStatus["INITING"] = "INITING";
  IContainerStatus["READY"] = "READY";
  IContainerStatus["IN_BIZ_FLOW"] = "IN_BIZ_FLOW";
  IContainerStatus["DESTROYED"] = "DESTROYED";
  return IContainerStatus;
}({});
export var ContainerController = /*#__PURE__*/function () {
  function ContainerController(_ref) {
    var _options$debugProps, _options$debugProps2;
    var type = _ref.type,
      instanceId = _ref.instanceId,
      selector = _ref.selector,
      processor = _ref.processor,
      options = _ref.options;
    _classCallCheck(this, ContainerController);
    _defineProperty(this, "containerStatus", void 0);
    _defineProperty(this, "instanceId", void 0);
    _defineProperty(this, "containerService", void 0);
    _defineProperty(this, "eventService", void 0);
    _defineProperty(this, "containerElement", void 0);
    _defineProperty(this, "_globalData", void 0);
    this._globalData = new Map();
    this.switchContainerStatus(IContainerStatus.INITING);
    this.instanceId = "".concat(instanceId, "-").concat(type);
    this.containerService = ServiceProvider.getInstance(this.instanceId).getService('Container');
    ServiceProvider.getInstance(this.instanceId).init({}, {
      productScene: ProductSceneEnum.ELEMENT_PAYMENT,
      sdkVersion: '',
      webAppVersion: ''
    });
    var iframesSrcLink = options !== null && options !== void 0 && (_options$debugProps = options.debugProps) !== null && _options$debugProps !== void 0 && _options$debugProps.isDebug ? (options === null || options === void 0 || (_options$debugProps2 = options.debugProps) === null || _options$debugProps2 === void 0 ? void 0 : _options$debugProps2.localLink) || '' : '';
    this.containerElement = this.containerService.load({
      type: DisplayTypeEnum.inline,
      platform: isElementPad() ? PlatformEnum.desktop : isElementPC() ? PlatformEnum.desktop : PlatformEnum.mobile,
      isRetention: false,
      selector: selector,
      appendAliasContainerId: true,
      iframeStyle: {
        display: 'block'
      }
    }, processor.generateIframeSrc({
      link: iframesSrcLink,
      instanceId: this.instanceId
    }));
    this.eventService = ServiceProvider.getInstance(this.instanceId).getService('EventCenter');
    this.addEventListener();
  }
  _createClass(ContainerController, [{
    key: "addEventListener",
    value: function addEventListener() {
      var _this = this;
      this.eventService.listen(EVENT.setGlobalData.name, function (result) {
        _this._globalData.set(result === null || result === void 0 ? void 0 : result.dataKey, result === null || result === void 0 ? void 0 : result.data);
      });
      this.eventService.listen(EVENT.getGlobalData.name, function (data, result) {
        var context = result.context;
        if (context !== null && context !== void 0 && context.eventCallbackId) {
          var _context$data;
          var targetData = _this._globalData.get((_context$data = context.data) === null || _context$data === void 0 ? void 0 : _context$data.dataKey);
          _this.eventService.dispatchToApp({
            event: 'appEventCallback',
            eventCallbackId: context === null || context === void 0 ? void 0 : context.eventCallbackId,
            data: targetData
          });
        }
      });
    }
  }, {
    key: "getContainerElement",
    value: function getContainerElement() {
      return this.containerElement;
    }
  }, {
    key: "createWebLaunchPromise",
    value: function createWebLaunchPromise() {
      var _this2 = this;
      return new Promise(function (resolve) {
        _this2.eventService.listen('onLaunch', function (res) {
          resolve(res);
        });
      });
    }
  }, {
    key: "switchContainerStatus",
    value: function switchContainerStatus(status) {
      this.containerStatus = status;
    }
  }, {
    key: "getContainerStatus",
    value: function getContainerStatus() {
      return this.containerStatus;
    }
  }, {
    key: "getEventService",
    value: function getEventService() {
      return this.eventService;
    }
  }, {
    key: "destroy",
    value: function destroy() {
      this.eventService.destroy();
      this.containerElement.remove();
    }
  }, {
    key: "getContainerService",
    value: function getContainerService() {
      return this.containerService;
    }
  }, {
    key: "getInstanceId",
    value: function getInstanceId() {
      return this.instanceId;
    }
  }]);
  return ContainerController;
}();