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 { ServiceProvider } from "../index";
import { EVENT } from "../../../constant";
export var GlobalDataService = /*#__PURE__*/function () {
  function GlobalDataService() {
    _classCallCheck(this, GlobalDataService);
    _defineProperty(this, "globalData", new Map());
  }
  _createClass(GlobalDataService, [{
    key: "init",
    value: function init(initOptions, instanceId) {
      var _this = this;
      // add capability to EventCenter
      var eventCenter = ServiceProvider.getInstance(instanceId).getService('EventCenter');
      eventCenter.listen(EVENT.getGlobalData.name, function (data, payload) {
        var _payload$context, _payload$context2, _payload$context3, _payload$context4;
        if (!((_payload$context = payload.context) !== null && _payload$context !== void 0 && _payload$context.eventCallbackId) || !((_payload$context2 = payload.context) !== null && _payload$context2 !== void 0 && (_payload$context2 = _payload$context2.data) !== null && _payload$context2 !== void 0 && _payload$context2.dataKey)) {
          return;
        }
        var targetData = _this.globalData.get((_payload$context3 = payload.context) === null || _payload$context3 === void 0 || (_payload$context3 = _payload$context3.data) === null || _payload$context3 === void 0 ? void 0 : _payload$context3.dataKey);
        eventCenter.dispatchToApp({
          event: 'appEventCallback',
          eventCallbackId: (_payload$context4 = payload.context) === null || _payload$context4 === void 0 ? void 0 : _payload$context4.eventCallbackId,
          data: targetData
        });
      });
      eventCenter.listen(EVENT.setGlobalData.name, function (data) {
        if (!(data !== null && data !== void 0 && data.dataKey) || !(data !== null && data !== void 0 && data.data)) {
          return;
        }
        _this.globalData.set(data.dataKey, data.data);
      });

      // register
    }
  }, {
    key: "destroy",
    value: function destroy() {
      this.globalData.clear();
    }
  }, {
    key: "update",
    value: function update() {}
  }, {
    key: "getGlobalData",
    value: function getGlobalData(dataKey) {
      var targetData = this.globalData.get(dataKey);
      return targetData;
    }
  }, {
    key: "setGlobalData",
    value: function setGlobalData(dataKey, data) {
      this.globalData.set(dataKey, data);
    }
  }, {
    key: "clearGlobalData",
    value: function clearGlobalData() {
      this.globalData = new Map();
    }
  }]);
  return GlobalDataService;
}();