import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
export var PROFILER_KEY = '__editorRenderCountProfiler';
export var RenderCountProfiler = /*#__PURE__*/function () {
  /**
   * The singleton/cached instance of RenderCountProfiler that will be shared
   * betweenRenderCountProfiler.getInstance() calls
   */

  function RenderCountProfiler(_ref) {
    var store = _ref.store;
    _classCallCheck(this, RenderCountProfiler);
    this.store = store;
  }

  /**
   * Returns the singleton/cached instance of RenderCountProfiler that
   * currently exists. If it hasn't been instantiated yet, the singleton
   * instance will be created using the given params. Returns the latest
   * singleton/instance.
   */
  _createClass(RenderCountProfiler, [{
    key: "getData",
    value: function getData(profilerKey) {
      var _this$store;
      return (_this$store = this.store) === null || _this$store === void 0 ? void 0 : _this$store[profilerKey];
    }
  }, {
    key: "enable",
    value: function enable() {
      this.store[PROFILER_KEY] = _objectSpread(_objectSpread({}, this.store[PROFILER_KEY]), {}, {
        enabled: true
      });
    }
  }, {
    key: "remove",
    value: function remove() {
      delete this.store[PROFILER_KEY];
    }
  }, {
    key: "isEnabled",
    value: function isEnabled() {
      var _this$store2;
      return Boolean((_this$store2 = this.store) === null || _this$store2 === void 0 || (_this$store2 = _this$store2[PROFILER_KEY]) === null || _this$store2 === void 0 ? void 0 : _this$store2.enabled);
    }
  }, {
    key: "setRenderCount",
    value: function setRenderCount(_ref2) {
      var componentId = _ref2.componentId,
        renderCount = _ref2.renderCount,
        instanceId = _ref2.instanceId;
      var profilerData = this.store[PROFILER_KEY];
      var instance = {
        count: renderCount
      };
      var existingComponents = profilerData === null || profilerData === void 0 ? void 0 : profilerData.components;
      var existingInstances = existingComponents === null || existingComponents === void 0 ? void 0 : existingComponents[componentId];
      var updatedComponent = _objectSpread(_objectSpread({}, existingInstances), {}, _defineProperty({}, instanceId, instance));
      this.store[PROFILER_KEY] = _objectSpread(_objectSpread({}, profilerData), {}, {
        components: _objectSpread(_objectSpread({}, existingComponents), {}, _defineProperty({}, componentId, updatedComponent))
      });
    }
  }, {
    key: "getInstanceRenderCounters",
    value: function getInstanceRenderCounters(_ref3) {
      var _this$store$PROFILER_, _this$store3;
      var componentId = _ref3.componentId;
      var component = (_this$store$PROFILER_ = (_this$store3 = this.store) === null || _this$store3 === void 0 || (_this$store3 = _this$store3[PROFILER_KEY]) === null || _this$store3 === void 0 || (_this$store3 = _this$store3.components) === null || _this$store3 === void 0 ? void 0 : _this$store3[componentId]) !== null && _this$store$PROFILER_ !== void 0 ? _this$store$PROFILER_ : {};
      var counters = [];
      for (var _instanceId in component) {
        var counter = {
          instanceId: _instanceId,
          count: component[_instanceId].count
        };
        counters.push(counter);
      }
      return counters;
    }
  }, {
    key: "getRenderCount",
    value: function getRenderCount(_ref4) {
      var _this$store$PROFILER_2, _this$store4;
      var componentId = _ref4.componentId;
      var component = (_this$store$PROFILER_2 = (_this$store4 = this.store) === null || _this$store4 === void 0 || (_this$store4 = _this$store4[PROFILER_KEY]) === null || _this$store4 === void 0 || (_this$store4 = _this$store4.components) === null || _this$store4 === void 0 ? void 0 : _this$store4[componentId]) !== null && _this$store$PROFILER_2 !== void 0 ? _this$store$PROFILER_2 : {};
      var total = 0;
      for (var _instanceId2 in component) {
        total += component[_instanceId2].count;
      }
      return total;
    }
  }], [{
    key: "getInstance",
    value: function getInstance(params) {
      if (!RenderCountProfiler.instance) {
        RenderCountProfiler.instance = new RenderCountProfiler({
          store: params.store
        });
      }
      return RenderCountProfiler.instance;
    }
  }]);
  return RenderCountProfiler;
}();