"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.MultiBulkActionToolbarState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
class MultiBulkActionToolbarState {
  constructor(props) {
    (0, _defineProperty2.default)(this, "_primaryActionItems", []);
    (0, _defineProperty2.default)(this, "_secondaryActionItems", []);
    (0, _defineProperty2.default)(this, "_extensionMenuItems", void 0);
    (0, _defineProperty2.default)(this, "_collection", void 0);
    const {
      collection,
      extensionMenuItems
    } = props;
    this._collection = collection;
    this._extensionMenuItems = extensionMenuItems;
    (0, _mobx.makeObservable)(this, {
      _primaryActionItems: _mobx.observable.ref,
      _secondaryActionItems: _mobx.observable.ref,
      setActions: _mobx.action,
      primaryActionItems: _mobx.computed,
      secondaryActionItems: _mobx.computed,
      secondaryActionItemsCount: _mobx.computed
    });
  }
  setActions({
    primaryActionItems = [],
    secondaryActionItems = []
  }) {
    this._primaryActionItems = primaryActionItems.map(item => {
      var _item$subItems;
      return {
        ...item,
        subItems: item.subItems ? Array.isArray((_item$subItems = item.subItems) == null ? void 0 : _item$subItems[0]) ? item.subItems : [item.subItems] : item.subItems
      };
    });
    this._secondaryActionItems = Array.isArray(secondaryActionItems == null ? void 0 : secondaryActionItems[0]) ? secondaryActionItems : [secondaryActionItems];
  }
  _moreActionItemClick(item) {
    const {
      result: {
        total
      },
      bulkSelect
    } = this._collection;
    const {
      selectedIds,
      uncheckedIds,
      allSelected
    } = bulkSelect;
    item.onClick({
      selectedIds,
      uncheckedIds,
      isSelectAll: allSelected,
      total,
      ...this._extensionMenuItems.containerProps
    });
  }
  get primaryActionItems() {
    return this._primaryActionItems;
  }
  get secondaryActionItems() {
    const moreActionItems = this._extensionMenuItems.menuItems.map(item => ({
      ...item,
      label: item.text,
      onClick: () => this._moreActionItemClick(item)
    })) || [];
    return [...this._secondaryActionItems, moreActionItems];
  }
  get secondaryActionItemsCount() {
    return this.secondaryActionItems.reduce((result, group) => result + group.length, 0);
  }
}
exports.MultiBulkActionToolbarState = MultiBulkActionToolbarState;
//# sourceMappingURL=MultiBulkActionToolbarState.js.map