"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.NestedTableNodeStateOptimisticActions = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _once = _interopRequireDefault(require("lodash/once"));
var _getNodeOrLeaf = require("./getNodeOrLeaf");
var _mobx = require("mobx");
class NestedTableNodeStateOptimisticActions {
  constructor(node) {
    (0, _defineProperty2.default)(this, "node", void 0);
    this.node = node;
    (0, _mobx.makeObservable)(this, {
      _propagate: _mobx.action,
      _runAction: _mobx.action,
      deleteMany: _mobx.action,
      updateMany: _mobx.action
    });
  }
  _getActionsParams(params) {
    const primaryActionParams = {
      ...params,
      submit: (0, _once.default)(params.submit)
    };
    const secondaryActionParams = {
      ...primaryActionParams,
      successToast: undefined,
      errorToast: undefined
    };
    return {
      primaryActionParams,
      secondaryActionParams
    };
  }
  _runAction(action, params) {
    const {
      node: {
        flatModeCollection,
        children,
        nestedTable
      }
    } = this;
    const {
      primaryActionParams,
      secondaryActionParams
    } = this._getActionsParams(params);
    if (children != null && children._optimisticActions) {
      action(children._optimisticActions, primaryActionParams);
    }
    if (flatModeCollection != null && flatModeCollection._optimisticActions) {
      action(flatModeCollection._optimisticActions, secondaryActionParams);
    }
    if (nestedTable.toolbar.collection !== children && nestedTable.toolbar.collection._optimisticActions) {
      action(nestedTable.toolbar.collection._optimisticActions, secondaryActionParams);
    }
  }
  _propagate(items, action) {
    const {
      node
    } = this;
    if (!node.children) {
      return;
    }
    const {
      itemKey,
      result: {
        _keyedItemsMap
      }
    } = node.collection;
    const directItems = items.map(item => _keyedItemsMap.get(itemKey(item)));
    const directNodes = directItems.map(keyedItem => (0, _getNodeOrLeaf.getNodeOrLeaf)(node, keyedItem));
    for (const subNode of directNodes) {
      var _subNode$children;
      const subItems = (_subNode$children = subNode.children) == null ? void 0 : _subNode$children.result.items;
      if (!(subItems != null && subItems.length)) {
        continue;
      }
      action(subNode, subItems);
    }
  }
  createOne(item, params) {
    this.createMany([item], params);
  }
  createMany(items, params) {
    this._runAction((a, p) => a.createMany(items, p), params);
  }
  updateAll(patchOrFn, params) {
    this._runAction((a, p) => a.updateAll(patchOrFn, p), params);
  }
  deleteMany(items, params) {
    const {
      primaryActionParams,
      secondaryActionParams
    } = this._getActionsParams(params);
    this._propagate(items, (subNode, subItems) => {
      var _subNode$optimisticAc;
      (_subNode$optimisticAc = subNode.optimisticActions) == null || _subNode$optimisticAc.deleteMany(subItems, secondaryActionParams);
    });
    this._runAction((a, p) => a.deleteMany(items, p), primaryActionParams);
  }
  deleteOne(item, params) {
    this.deleteMany([item], params);
  }
  deleteAll(params) {
    this._runAction((a, p) => a.deleteAll(p), params);
  }
  updateMany(items, params) {
    const {
      primaryActionParams,
      secondaryActionParams
    } = this._getActionsParams(params);
    if (params != null && params.propagate) {
      this._propagate(items, (subNode, subItems) => {
        var _subNode$optimisticAc2;
        (_subNode$optimisticAc2 = subNode.optimisticActions) == null || _subNode$optimisticAc2.updateMany(subItems, secondaryActionParams);
      });
    }
    this._runAction((a, p) => a.updateMany(items, p), primaryActionParams);
  }
  updateOne(item, params) {
    this.updateMany([item], params);
  }
}
exports.NestedTableNodeStateOptimisticActions = NestedTableNodeStateOptimisticActions;
//# sourceMappingURL=NestedTableNodeStateOptimisticActions.js.map