"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.TableState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _ToolbarCollectionState = require("./ToolbarCollectionState");
var _TableVirtualState = require("./TableVirtualState");
var _LoadingRowState = require("../components/LoadingRow/LoadingRowState");
class TableState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    (0, _defineProperty2.default)(this, "virtual", void 0);
    (0, _defineProperty2.default)(this, "loadingRowState", void 0);
    (0, _defineProperty2.default)(this, "tableDragAndDropState", null);
    (0, _defineProperty2.default)(this, "retryErrorState", () => this.toolbar.retryErrorState());
    (0, _defineProperty2.default)(this, "onAddItemActionComplete", async items => {
      return this.toolbar.onAddItemActionComplete(items);
    });
    (0, _defineProperty2.default)(this, "sort", (columnId, {
      forceDirection
    } = {}) => {
      this.collection.sort({
        id: columnId
      }, {
        forceDirection
      });
    });
    this.toolbar = params.toolbar ?? new _ToolbarCollectionState.ToolbarCollectionState({
      collection: params.collection,
      container: params.container,
      componentType: 'Table'
    });
    this.virtual = new _TableVirtualState.TableVirtualState({
      table: this
    });
    this.loadingRowState = new _LoadingRowState.LoadingRowState();
  }
  get visibleColumns() {
    return this.toolbar.selectedOrderedColumnsOrAll;
  }

  // backwards compat
  get table() {
    return this.toolbar;
  }
  get collection() {
    return this.table.collection;
  }
  get tableState() {
    return this;
  }
  get itemsContentWidth() {
    return this.toolbar.itemsContentRect.rect.width ?? 0;
  }
  get itemsToRenderCount() {
    return this.collection.result.size;
  }
  init(params) {
    return this.table.init(params);
  }
  get keyedItems() {
    return this.collection.result.keyedItems;
  }
  getStickyColumnsCount(params) {
    var _customColumnsState$d;
    const {
      horizontalScroll
    } = params;
    if (!horizontalScroll) {
      return params.stickyColumns;
    }
    const {
      tableDragAndDropState,
      toolbar
    } = this;
    const {
      customColumnsState,
      customColumnsProps,
      customColumnsStateInitialized
    } = toolbar;
    const {
      stickySelectionColumn,
      showSelection
    } = params;
    let stickyColumns = params.stickyColumns ?? 0;
    const customColumnsIsEffective = customColumnsProps && customColumnsState && customColumnsStateInitialized;
    const reorderDisabledColumnsIsEffective = !!(customColumnsState != null && (_customColumnsState$d = customColumnsState.dragAndDrop) != null && (_customColumnsState$d = _customColumnsState$d.orderedColumnsByReorderDisabled.reorderDisabled) != null && _customColumnsState$d.length);
    if (customColumnsIsEffective) {
      stickyColumns = customColumnsState.getStickyColumnsCount({
        ...params,
        stickyColumns
      });
    }
    if (tableDragAndDropState) {
      stickyColumns = tableDragAndDropState.getStickyColumnsCount({
        ...params,
        stickyColumns
      });
    }
    const checkboxColumnStickyCompat = reorderDisabledColumnsIsEffective || stickySelectionColumn || tableDragAndDropState != null;
    if (showSelection && checkboxColumnStickyCompat) {
      stickyColumns += 1;
    }
    return stickyColumns;
  }
  get showEmptyState() {
    return this.toolbar.multi.showEmptyState;
  }
  get hasAvailableItems() {
    return this.toolbar.multi.hasAvailableItems;
  }
  get resultOriginQuerySearch() {
    return this.toolbar.multi.resultOriginQuerySearch;
  }
  get hasNonPersistentActiveFilters() {
    return this.toolbar.multi.hasNonPersistentActiveFilters;
  }
  get showErrorState() {
    return this.toolbar.multi.showErrorState;
  }
  get errorStatus() {
    return this.toolbar.multi.errorStatus;
  }
  get showLoadingState() {
    const {
      initTask,
      toolbar
    } = this;
    return initTask.status.isIdle || initTask.status.isLoading || toolbar.multi.showLoadingState;
  }
  get initTask() {
    return this.toolbar.initTask;
  }
}
exports.TableState = TableState;
//# sourceMappingURL=TableState.js.map