"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, "options", void 0);
    (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;
    this.options = params.options;
    (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)(() => {
        if (!this.options.length) return;
        this.viewType = this.options.includes(res) ? res : this.options[0];
      });
    } catch (err) {
      this.container.errorMonitor.captureException(err);
    }
  }
  changeViewType(newViewType) {
    var _this$container$inter;
    if (!this.options.length) return;
    const isLayoutToggleRedesignEnabled = (_this$container$inter = this.container.internalExperiments) == null ? void 0 : _this$container$inter.enabled('specs.cairo.layout-toggle.redesign');
    if (!isLayoutToggleRedesignEnabled) {
      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 = this.options.includes(newViewType) ? newViewType : this.options[0];
    this.dataCapsule.setItem('view-type', this.viewType);
  }
  reportLayoutSwitch() {
    var _this$collection2;
    this.container.reportBi((0, _bexCore.withoutDefaults)(_bi.cairoFiltersPanelUsed)({
      ...((_this$collection2 = this.collection) == null ? void 0 : _this$collection2._commonDynamicBiParams()),
      feature: 'Switch Layout'
    }));
  }
  reportLayoutOptionClicked(option, index) {
    var _this$collection3;
    this.container.reportBi((0, _bexCore.withoutDefaults)(_bi.cairoCtaClicked)({
      ...((_this$collection3 = this.collection) == null ? void 0 : _this$collection3._commonDynamicBiParams()),
      ctaName: option,
      location: 'switch_layout_dropdown',
      itemId: option,
      itemIndex: index
    }));
  }
}
exports.ViewTypeState = ViewTypeState;
//# sourceMappingURL=ViewTypeState.js.map