"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.BulkActionToolbarState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _bexCore = require("@wix/bex-core");
var _react = require("react");
class BulkActionToolbarState {
  constructor(_params) {
    (0, _defineProperty2.default)(this, "toolbarBIReporter", void 0);
    (0, _defineProperty2.default)(this, "panels", void 0);
    (0, _defineProperty2.default)(this, "multi", void 0);
    (0, _defineProperty2.default)(this, "modals", void 0);
    (0, _defineProperty2.default)(this, "bulkSelectionCheckboxRef", /*#__PURE__*/(0, _react.createRef)());
    (0, _defineProperty2.default)(this, "selectionDisabledProp", () => false);
    (0, _defineProperty2.default)(this, "checkboxTooltipContentProp", void 0);
    (0, _defineProperty2.default)(this, "shouldIncludeCollection", void 0);
    (0, _defineProperty2.default)(this, "selectionDisabledFn", ({
      key
    }) => {
      const {
        selectionDisabledProp
      } = this;
      if (typeof selectionDisabledProp === 'function') {
        var _this$_getCollectionO;
        const keyedItem = (_this$_getCollectionO = this._getCollectionOf(key)) == null ? void 0 : _this$_getCollectionO.result.get(key);
        if (keyedItem == null) {
          return false;
        }
        return Boolean(selectionDisabledProp(keyedItem));
      }
      return () => !!selectionDisabledProp;
    });
    (0, _defineProperty2.default)(this, "getCheckboxTooltipContent", ({
      key
    }) => {
      const {
        checkboxTooltipContentProp
      } = this;
      if (typeof checkboxTooltipContentProp === 'function') {
        var _this$_getCollectionO2;
        const keyedItem = (_this$_getCollectionO2 = this._getCollectionOf(key)) == null ? void 0 : _this$_getCollectionO2.result.get(key);
        if (keyedItem != null) {
          return checkboxTooltipContentProp(keyedItem);
        }
      }
      return undefined;
    });
    (0, _defineProperty2.default)(this, "maxSelection", void 0);
    (0, _defineProperty2.default)(this, "_selectionStartTime", 0);
    (0, _defineProperty2.default)(this, "clearSelection", () => {
      this.multi.collections.forEach(({
        bulkSelect
      }) => {
        bulkSelect.resetToDefaults();
      });
    });
    (0, _defineProperty2.default)(this, "isRowCheckboxDisabled", (0, _mobx.action)(row => {
      return this.reachedMaxSelection && !this.isChecked(row.key) || this.selectionDisabledFn(row);
    }));
    (0, _defineProperty2.default)(this, "toggleSelectAll", (0, _mobx.action)(params => {
      if (!this.hasClickableRows) {
        this.deselectAll(params);
      } else if (this.maxSelection != null) {
        this.selectAllUpToMaxSelection();
      } else {
        this.selectAll(params);
      }
    }));
    this.toolbarBIReporter = _params.toolbarBIReporter;
    this.panels = _params.panels;
    this.multi = _params.multi;
    this.modals = new _bexCore.BulkActionModalState({
      translate: _params.translate,
      scrollToTop: _params.scrollToTop
    });
    (0, _mobx.makeObservable)(this, {
      hasClickableRows: _mobx.computed,
      reachedMaxSelection: _mobx.computed,
      total: _mobx.computed,
      selectedCountOrTotal: _mobx.computed,
      selectedIds: _mobx.computed
    });
  }
  get filteredCollections() {
    return this.shouldIncludeCollection ? this.multi.collections.filter(this.shouldIncludeCollection) : this.multi.collections;
  }
  get selectedIds() {
    return this.multi.collections.reduce((a, {
      bulkSelect
    }) => [...a, ...bulkSelect.selectedIds], []);
  }
  get selectedValues() {
    return this.multi.collections.reduce((a, {
      bulkSelect
    }) => [...a, ...bulkSelect.selectedValues], []);
  }
  get uncheckedValues() {
    return this.multi.collections.reduce((a, {
      bulkSelect
    }) => [...a, ...bulkSelect.uncheckedValues], []);
  }
  get selectedCount() {
    return this.filteredCollections.reduce((s, {
      bulkSelect
    }) => s + bulkSelect.selectedCount, 0);
  }
  get selectedCountOrTotal() {
    if (this.allSelected) {
      return this.filteredCollections.reduce((s, {
        bulkSelect
      }) => s + bulkSelect.selectedCountOrTotal, 0);
    }
    return this.selectedCount;
  }
  get hasClickableRows() {
    return this.filteredCollections.some(({
      bulkSelect,
      ...collection
    }) => collection.result.keyedItems.some(row => !bulkSelect.select.isChecked(row.key) && !this.isRowCheckboxDisabled(row)));
  }
  get allSelected() {
    return this.filteredCollections.every(({
      bulkSelect
    }) => bulkSelect.allSelected);
  }
  get reachedMaxSelection() {
    const {
      maxSelection,
      selectedCount
    } = this;
    if (maxSelection != null) {
      return selectedCount >= maxSelection;
    }
    return false;
  }
  get total() {
    return this.filteredCollections.reduce((s, {
      result: {
        total
      }
    }) => s + total, 0);
  }
  isChecked(key) {
    return this.multi.collections.some(({
      bulkSelect
    }) => bulkSelect.isChecked(key));
  }
  selectAllUpToMaxSelection() {
    for (const collection of this.filteredCollections) {
      if (this.reachedMaxSelection) {
        break;
      }
      for (const item of collection.result.keyedItems) {
        if (this.reachedMaxSelection) {
          break;
        }
        if (!this.isRowCheckboxDisabled(item)) {
          collection.bulkSelect.select.set(item);
        }
      }
    }
  }
  selectAll({
    location
  }) {
    const onSelectAllToggledEnd = this.toolbarBIReporter.selectAllToggledStart({
      location
    });
    this.filteredCollections.forEach(collection => collection.bulkSelect.selectAll());
    onSelectAllToggledEnd();
  }
  deselectAll({
    location
  }) {
    var _this$bulkSelectionCh;
    const onSelectAllToggledEnd = this.toolbarBIReporter.selectAllToggledStart({
      location
    });
    this.filteredCollections.forEach(collection => collection.bulkSelect.deselectAll());
    onSelectAllToggledEnd();
    (_this$bulkSelectionCh = this.bulkSelectionCheckboxRef.current) == null || _this$bulkSelectionCh.focus();
  }
  _getCollectionOf(id) {
    return this.multi.collections.find(collection => collection.result.has(id));
  }
  selectOne(id) {
    var _this$_getCollectionO3;
    const onSelectionToggledEnd = this.toolbarBIReporter.itemSelectionToggledStarted({
      itemId: id,
      clickType: 'Selection'
    });
    (_this$_getCollectionO3 = this._getCollectionOf(id)) == null || _this$_getCollectionO3.bulkSelect.selectOne(id);
    onSelectionToggledEnd();
  }
  deselectOne(id) {
    var _this$_getCollectionO4;
    const onSelectionToggledEnd = this.toolbarBIReporter.itemSelectionToggledStarted({
      itemId: id,
      clickType: 'Deselection'
    });
    (_this$_getCollectionO4 = this._getCollectionOf(id)) == null || _this$_getCollectionO4.bulkSelect.deselectOne(id);
    onSelectionToggledEnd();
  }
  init() {
    const disposers = [...this.multi.collections.map(({
      bulkSelect
    }) => (0, _mobx.reaction)(() => ({
      selectedValues: bulkSelect.select.selectedValues
    }), ({
      selectedValues
    }) => {
      if (this.selectionDisabledFn == null) {
        return;
      }
      for (const keyedItem of selectedValues) {
        const collectionItem = bulkSelect.getCollectionItem(keyedItem.key);
        if (collectionItem && this.selectionDisabledFn(collectionItem)) {
          bulkSelect.deselectOneItem(keyedItem);
        }
      }
    }))];
    return () => {
      disposers.forEach(d => d());
    };
  }
}
exports.BulkActionToolbarState = BulkActionToolbarState;
//# sourceMappingURL=BulkActionToolbarState.js.map