"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.MutationState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
class MutationState {
  constructor(parent, handlers, createOptimisticActions) {
    (0, _defineProperty2.default)(this, "_handlers", void 0);
    (0, _defineProperty2.default)(this, "_optimisticActions", void 0);
    (0, _defineProperty2.default)(this, "_parent", void 0);
    this._parent = parent;
    this._handlers = handlers;
    this._optimisticActions = createOptimisticActions(parent.collection);
  }
  init() {
    return this._optimisticActions.init();
  }
  addItem(afterItemKey) {
    const newItem = this._handlers.createItem();
    this._optimisticActions.createOne(newItem, {
      submit: this._handlers.submitCreate,
      afterItemKey,
      successToast: 'Item created',
      errorToast: (_err, {
        retry
      }) => ({
        message: 'Failed to create item',
        action: {
          onClick: retry,
          text: 'Retry'
        }
      })
    });
    return this._parent.collection.itemKey(newItem);
  }
  duplicateItem(rowKey) {
    const keyedItem = this._parent.keyedItems.find(ki => ki.key === rowKey);
    if (!keyedItem) {
      return;
    }
    const sourceItem = keyedItem.item;
    const newItem = this._handlers.createItem();
    const {
      itemKey
    } = this._parent.collection;
    const newId = itemKey(newItem);
    const sourceId = itemKey(sourceItem);
    const duplicate = {
      ...sourceItem
    };
    for (const k of Object.keys(duplicate)) {
      if (String(duplicate[k]) === sourceId) {
        duplicate[k] = newId;
        break;
      }
    }
    this._optimisticActions.createOne(duplicate, {
      submit: this._handlers.submitCreate,
      afterItemKey: keyedItem.key,
      successToast: 'Item duplicated',
      errorToast: (_err, {
        retry
      }) => ({
        message: 'Failed to duplicate item',
        action: {
          onClick: retry,
          text: 'Retry'
        }
      })
    });
  }
  deleteItems(rowKeys) {
    const items = rowKeys.map(key => {
      var _this$_parent$keyedIt;
      return (_this$_parent$keyedIt = this._parent.keyedItems.find(ki => ki.key === key)) == null ? void 0 : _this$_parent$keyedIt.item;
    }).filter(item => item != null);
    if (items.length === 0) {
      return;
    }
    this._optimisticActions.deleteMany(items, {
      submit: this._handlers.submitDelete,
      successToast: items.length === 1 ? 'Item deleted' : `${items.length} items deleted`,
      errorToast: (_err, {
        retry
      }) => ({
        message: 'Failed to delete items',
        action: {
          onClick: retry,
          text: 'Retry'
        }
      })
    });
  }
  updateItems(updatedItems) {
    if (updatedItems.length === 0) {
      return;
    }
    this._optimisticActions.updateMany(updatedItems, {
      keepPosition: true,
      submit: this._handlers.submitUpdate,
      successToast: updatedItems.length === 1 ? 'Item updated' : `${updatedItems.length} items updated`,
      errorToast: (_err, {
        retry
      }) => ({
        message: 'Failed to update items',
        action: {
          onClick: retry,
          text: 'Retry'
        }
      })
    });
  }
}
exports.MutationState = MutationState;
//# sourceMappingURL=MutationState.js.map