"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ViewTypeState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _bi = require("@wix/bex-core/bi");
var _mobx = require("mobx");
class ViewTypeState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "viewType", 'table');
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "dataCapsule", void 0);
    (0, _defineProperty2.default)(this, "componentType", void 0);
    (0, _defineProperty2.default)(this, "collection", void 0);
    (0, _defineProperty2.default)(this, "initTask", new _bexCore.TaskState());
    this.container = params.container;
    this.dataCapsule = params.dataCapsule;
    this.componentType = params.componentType;
    this.collection = params.collection;
    (0, _mobx.makeObservable)(this, {
      viewType: _mobx.observable.ref,
      changeViewType: _mobx.action.bound
    });
  }
  init() {
    this.initTask.runOnce(async () => {
      await this._getViewTypeFromStorage();
    });
  }
  async _getViewTypeFromStorage() {
    try {
      const res = await this.dataCapsule.getItem('view-type');
      (0, _mobx.runInAction)(() => {
        this.viewType = res || this.viewType;
      });
    } catch (err) {
      this.container.errorMonitor.captureException(err);
    }
  }
  changeViewType(newViewType) {
    var _this$collection;
    this.container.reportBi((0, _bi.cairoSectionCtaClicked)({
      ctaType: 'More actions',
      loaction: 'Collection Toolbar',
      location: 'Collection Toolbar',
      componentType: this.componentType,
      sectionType: this.viewType,
      ctaName: `layout switch to ${newViewType}`,
      ...((_this$collection = this.collection) == null ? void 0 : _this$collection._commonDynamicBiParams())
    }));
    this.viewType = newViewType === 'grid' ? 'grid' : 'table';
    this.dataCapsule.setItem('view-type', newViewType);
  }
}
exports.ViewTypeState = ViewTypeState;
//# sourceMappingURL=ViewTypeState.js.map