"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.EditableTableState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _bexCore = require("@wix/bex-core");
var _ToolbarCollectionState = require("../ToolbarCollectionState");
var _TableState = require("../TableState");
var _CellInteractionState = require("./CellInteractionState");
var _CellValueState = require("./CellValueState");
var _ClipboardState = require("./ClipboardState");
var _MutationState = require("./MutationState");
var _FieldActionsState = require("../FieldActionsState");
class EditableTableState {
  constructor(params) {
    var _params$extraCellType, _params$permissions;
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    (0, _defineProperty2.default)(this, "tableState", void 0);
    (0, _defineProperty2.default)(this, "cellInteraction", void 0);
    (0, _defineProperty2.default)(this, "cellValue", void 0);
    (0, _defineProperty2.default)(this, "clipboard", void 0);
    (0, _defineProperty2.default)(this, "mutation", void 0);
    (0, _defineProperty2.default)(this, "deleteConfirmModal", new _bexCore.ConditionalModalState());
    (0, _defineProperty2.default)(this, "fieldActions", void 0);
    (0, _defineProperty2.default)(this, "addItemDisabled", false);
    (0, _defineProperty2.default)(this, "updateItemDisabled", false);
    (0, _defineProperty2.default)(this, "removeItemDisabled", false);
    (0, _defineProperty2.default)(this, "_allColumns", []);
    (0, _defineProperty2.default)(this, "onAddItemActionComplete", items => {
      return this.tableState.onAddItemActionComplete(items);
    });
    (0, _defineProperty2.default)(this, "onAddItemClick", origin => {
      this.toolbar.toolbarBIReporter.onAddItemClick(origin);
    });
    (0, _defineProperty2.default)(this, "sort", (columnId, {
      forceDirection,
      multiple
    } = {}) => {
      this.collection.sort({
        id: columnId
      }, {
        forceDirection,
        clearResult: true,
        multiple
      });
    });
    this.toolbar = params.toolbar ?? new _ToolbarCollectionState.ToolbarCollectionState({
      collection: params.collection,
      container: params.container,
      componentType: 'EditableTable'
    });
    this.tableState = new _TableState.TableState({
      collection: params.collection,
      container: params.container,
      toolbar: this.toolbar
    });
    this.cellValue = new _CellValueState.CellValueState(this, new Map((_params$extraCellType = params.extraCellTypes) == null ? void 0 : _params$extraCellType.map(ct => [ct.type, ct])));
    this.cellInteraction = new _CellInteractionState.CellInteractionState((editingValue, cell) => {
      this.cellValue.commitHandler(editingValue, cell);
    }, this);
    this.clipboard = new _ClipboardState.ClipboardState(this);
    this.mutation = new _MutationState.MutationState(this, params.mutations, params.createOptimisticActions);
    this.fieldActions = new _FieldActionsState.FieldActionsState(params.collection.translate, params.fieldActions, (_params$permissions = params.permissions) == null ? void 0 : _params$permissions.fieldActions, params.collection.showToast, params.refresh);
    (0, _mobx.makeObservable)(this, {
      addItemDisabled: _mobx.observable,
      updateItemDisabled: _mobx.observable,
      removeItemDisabled: _mobx.observable,
      _allColumns: _mobx.observable.ref,
      editableColumns: _mobx.computed,
      _columnMap: _mobx.computed,
      init: _mobx.action,
      confirmDeleteItems: _mobx.action
    });
    if (params.permissions) {
      this.addItemDisabled = params.permissions.addItemDisabled ?? false;
      this.updateItemDisabled = params.permissions.updateItemDisabled ?? false;
      this.removeItemDisabled = params.permissions.removeItemDisabled ?? false;
    }
  }
  get collection() {
    return this.tableState.collection;
  }
  get keyedItems() {
    return this.tableState.keyedItems;
  }
  get visibleColumns() {
    return this.tableState.visibleColumns;
  }
  get itemsToRenderCount() {
    return this.tableState.itemsToRenderCount;
  }
  get _columnMap() {
    return new Map(this._allColumns.map(c => [c.id, c]));
  }
  _getKeyedItem(key) {
    return this.collection.getKeyedItem(key);
  }
  _toIndices(pos) {
    const row = this.keyedItems.findIndex(ki => ki.key === pos.rowKey);
    const col = this.editableColumns.findIndex(c => c.id === pos.columnId);
    return row === -1 || col === -1 ? null : {
      row,
      col
    };
  }
  _toPosition(row, col) {
    const ki = this.keyedItems[row];
    const c = this.editableColumns[col];
    return ki && c ? {
      rowKey: ki.key,
      columnId: c.id
    } : null;
  }
  get editableColumns() {
    const visibleIds = this.visibleColumns.map(c => c.id);
    if (!visibleIds.length) {
      return this._allColumns;
    }
    return visibleIds.map(id => this._allColumns.find(c => c.id === id)).filter(c => c !== undefined);
  }
  init(columns) {
    this._allColumns = columns;
    this.cellValue._buildCellTypeMap();
    return this.mutation.init();
  }
  addItem(afterItemKey) {
    return this.mutation.addItem(afterItemKey);
  }
  duplicateItem(rowKey) {
    this.mutation.duplicateItem(rowKey);
  }
  deleteItems(rowKeys) {
    this.mutation.deleteItems(rowKeys);
  }
  confirmDeleteItems(rowKeys) {
    const {
      translate: t
    } = this.collection;
    this.deleteConfirmModal.open({
      theme: 'destructive',
      title: t('cairo.deleteModal.title'),
      content: t('cairo.deleteModal.body'),
      primaryButtonText: t('cairo.delete.button'),
      secondaryButtonText: t('cairo.cancel.button'),
      primaryButtonOnClick: () => {
        this.deleteItems(rowKeys);
      }
    });
  }
  resolveCellTypeByColumnId(columnId) {
    return this.cellValue.resolveCellTypeByColumnId(columnId);
  }
  getCellDisplayValue(rowKey, columnId) {
    return this.cellValue.getCellDisplayValue(rowKey, columnId);
  }
  getCellValidation(rowKey, columnId) {
    return this.cellValue.getCellValidation(rowKey, columnId);
  }
  isCellEditable(rowKey, columnId) {
    return this.cellValue.isCellEditable(rowKey, columnId);
  }
  clearCells(cells) {
    this.cellValue.clearCells(cells);
  }
  handleCellClick(rowKey, columnId, shiftKey) {
    if (this.cellInteraction.isEditing) {
      if (this.cellInteraction.isCellEditing(rowKey, columnId)) {
        // Click landed inside the active editor — let the editor handle it.
        return;
      }
      // Shift-click while editing: ignore — extending a range selection during
      // an active edit is not a meaningful action.
      if (shiftKey) {
        return;
      }
      // Plain click on a different cell: commit the edit and move focus.
      this.cellInteraction.commitEdit();

      // If the target cell wants edit-on-click, enter edit directly.
      const col = this._columnMap.get(columnId);
      const res = col ? this.resolveCellTypeByColumnId(col.id) : null;
      if (res != null && res.editOnClick) {
        const val = this.getCellDisplayValue(rowKey, columnId);
        if (res.editOnClick(val) && this.isCellEditable(rowKey, columnId)) {
          this.cellInteraction.focusCell(rowKey, columnId);
          this.cellInteraction.startEdit(rowKey, columnId, 'click');
          return;
        }
      }
      this.cellInteraction.focusCell(rowKey, columnId);
      return;
    }
    const column = this._columnMap.get(columnId);
    const resolved = column ? this.resolveCellTypeByColumnId(column.id) : null;
    if (resolved != null && resolved.toggleValue || resolved != null && resolved.showEditWhenFocused) {
      // Toggle/showEditWhenFocused cells: click focuses the cell and shows the
      // EditComponent affordance. Keyboard navigation remains active.
      this.cellInteraction.focusCell(rowKey, columnId);
      return;
    }
    if (resolved != null && resolved.editOnClick) {
      const value = this.getCellDisplayValue(rowKey, columnId);
      if (resolved.editOnClick(value) && this.isCellEditable(rowKey, columnId)) {
        this.cellInteraction.focusCell(rowKey, columnId);
        this.cellInteraction.startEdit(rowKey, columnId, 'click');
        return;
      }
    }
    if (shiftKey && this.cellInteraction.selectionAnchor) {
      this.cellInteraction.selectRange(this.cellInteraction.selectionAnchor, {
        rowKey,
        columnId
      });
    } else {
      this.cellInteraction.focusCell(rowKey, columnId);
    }
  }
  handleCellDoubleClick(rowKey, columnId) {
    if (this.updateItemDisabled) {
      return;
    }
    const column = this._columnMap.get(columnId);
    const resolved = column ? this.resolveCellTypeByColumnId(column.id) : null;
    // Toggle-value cells (e.g. boolean) activate on single click, not double-click.
    // Entering edit mode on double-click would apply the editing box-shadow
    // without any EditComponent being shown, causing a visual border glitch.
    if (resolved != null && resolved.toggleValue || resolved != null && resolved.showEditWhenFocused) {
      return;
    }
    if (this.isCellEditable(rowKey, columnId)) {
      this.cellInteraction.startEdit(rowKey, columnId, 'doubleClick');
    }
  }
}
exports.EditableTableState = EditableTableState;
//# sourceMappingURL=EditableTableState.js.map