export const PROFILER_KEY = '__editorRenderCountProfiler';
export class RenderCountProfiler {
  /**
   * The singleton/cached instance of RenderCountProfiler that will be shared
   * betweenRenderCountProfiler.getInstance() calls
   */

  constructor({
    store
  }) {
    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.
   */
  static getInstance(params) {
    if (!RenderCountProfiler.instance) {
      RenderCountProfiler.instance = new RenderCountProfiler({
        store: params.store
      });
    }
    return RenderCountProfiler.instance;
  }
  getData(profilerKey) {
    var _this$store;
    return (_this$store = this.store) === null || _this$store === void 0 ? void 0 : _this$store[profilerKey];
  }
  enable() {
    this.store[PROFILER_KEY] = {
      ...this.store[PROFILER_KEY],
      enabled: true
    };
  }
  remove() {
    delete this.store[PROFILER_KEY];
  }
  isEnabled() {
    var _this$store2, _this$store2$PROFILER;
    return Boolean((_this$store2 = this.store) === null || _this$store2 === void 0 ? void 0 : (_this$store2$PROFILER = _this$store2[PROFILER_KEY]) === null || _this$store2$PROFILER === void 0 ? void 0 : _this$store2$PROFILER.enabled);
  }
  setRenderCount({
    componentId,
    renderCount,
    instanceId
  }) {
    const profilerData = this.store[PROFILER_KEY];
    const instance = {
      count: renderCount
    };
    const existingComponents = profilerData === null || profilerData === void 0 ? void 0 : profilerData.components;
    const existingInstances = existingComponents === null || existingComponents === void 0 ? void 0 : existingComponents[componentId];
    const updatedComponent = {
      ...existingInstances,
      [instanceId]: instance
    };
    this.store[PROFILER_KEY] = {
      ...profilerData,
      components: {
        ...existingComponents,
        [componentId]: updatedComponent
      }
    };
  }
  getInstanceRenderCounters({
    componentId
  }) {
    var _this$store$PROFILER_, _this$store3, _this$store3$PROFILER, _this$store3$PROFILER2;
    const component = (_this$store$PROFILER_ = (_this$store3 = this.store) === null || _this$store3 === void 0 ? void 0 : (_this$store3$PROFILER = _this$store3[PROFILER_KEY]) === null || _this$store3$PROFILER === void 0 ? void 0 : (_this$store3$PROFILER2 = _this$store3$PROFILER.components) === null || _this$store3$PROFILER2 === void 0 ? void 0 : _this$store3$PROFILER2[componentId]) !== null && _this$store$PROFILER_ !== void 0 ? _this$store$PROFILER_ : {};
    const counters = [];
    for (let instanceId in component) {
      const counter = {
        instanceId,
        count: component[instanceId].count
      };
      counters.push(counter);
    }
    return counters;
  }
  getRenderCount({
    componentId
  }) {
    var _this$store$PROFILER_2, _this$store4, _this$store4$PROFILER, _this$store4$PROFILER2;
    const component = (_this$store$PROFILER_2 = (_this$store4 = this.store) === null || _this$store4 === void 0 ? void 0 : (_this$store4$PROFILER = _this$store4[PROFILER_KEY]) === null || _this$store4$PROFILER === void 0 ? void 0 : (_this$store4$PROFILER2 = _this$store4$PROFILER.components) === null || _this$store4$PROFILER2 === void 0 ? void 0 : _this$store4$PROFILER2[componentId]) !== null && _this$store$PROFILER_2 !== void 0 ? _this$store$PROFILER_2 : {};
    let total = 0;
    for (let instanceId in component) {
      total += component[instanceId].count;
    }
    return total;
  }
}