"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ColumnSizesState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _services = require("../services");
var _mobx = require("mobx");
class ColumnSizesState {
  constructor(params) {
    var _this$container$inter;
    (0, _defineProperty2.default)(this, "columnSizeService", void 0);
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "isResizeExperimentEnabled", void 0);
    (0, _defineProperty2.default)(this, "initTask", new _bexCore.TaskState());
    (0, _defineProperty2.default)(this, "columnWidths", undefined);
    this.container = params.container;
    this.isResizeExperimentEnabled = ((_this$container$inter = this.container.internalExperiments) == null ? void 0 : _this$container$inter.enabled('specs.cairo.ResizeColumns')) ?? false;
    this.columnSizeService = new _services.ColumnSizesService({
      fqdn: params.fqdn,
      storage: params.dataCapsule,
      errorHandler: params.container.errorHandler
    });
    (0, _mobx.makeObservable)(this, {
      columnWidths: _mobx.observable.ref,
      isResizeExperimentEnabled: _mobx.observable
    });
  }
  init() {
    if (!this.isResizeExperimentEnabled) {
      return;
    }
    this.initTask.runOnce(async () => {
      const data = await this.columnSizeService.fetchColumnSizes();
      (0, _mobx.runInAction)(() => {
        this.columnWidths = data == null ? void 0 : data.columnWidths;
      });
    });
  }
  mergeSizes(baseColumns) {
    if (!this.isResizeExperimentEnabled) {
      return baseColumns;
    }
    const merged = baseColumns.map(column => {
      var _this$columnWidths;
      const persistedWidth = column.id ? (_this$columnWidths = this.columnWidths) == null ? void 0 : _this$columnWidths[column.id] : undefined;
      return persistedWidth ? {
        ...column,
        width: persistedWidth
      } : column;
    });
    return merged;
  }
  async updateColumnWidth(columnId, width) {
    const oldColumnWidths = {
      ...this.columnWidths
    };
    const newColumnWidths = {
      ...oldColumnWidths,
      [columnId]: width
    };
    (0, _mobx.runInAction)(() => {
      this.columnWidths = newColumnWidths;
    });
    try {
      await this.columnSizeService.updateColumnWidths(newColumnWidths);
    } catch (error) {
      var _this$container$error, _this$container;
      const resolvedError = (_this$container$error = this.container.errorHandler) == null || _this$container$error.getResolvedError == null ? void 0 : _this$container$error.getResolvedError(error);
      (_this$container = this.container) == null || _this$container.showToast == null || _this$container.showToast({
        biName: 'cairo-column-sizes-update-error',
        message: resolvedError == null ? void 0 : resolvedError.message,
        type: 'ERROR'
      });
    }
  }
}
exports.ColumnSizesState = ColumnSizesState;
//# sourceMappingURL=ColumnSizesState.js.map