"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.OptimisticPatch = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _events = require("events");
class OptimisticPatch {
  constructor(params) {
    (0, _defineProperty2.default)(this, "patch", void 0);
    (0, _defineProperty2.default)(this, "selector", void 0);
    (0, _defineProperty2.default)(this, "actionType", void 0);
    (0, _defineProperty2.default)(this, "patchedItems", void 0);
    (0, _defineProperty2.default)(this, "originalItems", void 0);
    (0, _defineProperty2.default)(this, "originalTotal", void 0);
    (0, _defineProperty2.default)(this, "keepPosition", void 0);
    (0, _defineProperty2.default)(this, "queryNames", void 0);
    (0, _defineProperty2.default)(this, "queryCache", void 0);
    (0, _defineProperty2.default)(this, "patches", void 0);
    (0, _defineProperty2.default)(this, "move", void 0);
    (0, _defineProperty2.default)(this, "createdAt", Date.now());
    (0, _defineProperty2.default)(this, "collectionSnapshot", void 0);
    (0, _defineProperty2.default)(this, "afterItemKey", void 0);
    (0, _defineProperty2.default)(this, "isStale", false);
    (0, _defineProperty2.default)(this, "events", new _events.EventEmitter());
    (0, _defineProperty2.default)(this, "onEventuallyUpdated", () => {
      const {
        events,
        queryCache,
        queryNames,
        patches
      } = this;
      this.isStale = true;
      if (!events.listeners('scheduleRemoval').length) {
        patches.remove(this);
        queryNames.forEach(queryName => {
          queryCache.findAll({
            queryKey: [queryName]
          }).forEach(q => q.invalidate());
        });
      } else {
        events.emit('scheduleRemoval', this);
      }
    });
    this.patch = item => params.patch == null ? void 0 : params.patch(item, this);
    this.selector = item => params.selector(item, this);
    this.actionType = params.actionType;
    this.keepPosition = params.keepPosition;
    this.queryNames = params.queryNames;
    this.queryCache = params.queryCache;
    this.patches = params.patches;
    this.move = params.move;
    this.collectionSnapshot = params.collectionSnapshot;
    this.afterItemKey = params.afterItemKey;
    this.patchedItems = _mobx.observable.map(params.patchedItems, {
      deep: false
    });
    this.originalItems = _mobx.observable.map(params.originalItems, {
      deep: false
    });
    this.originalTotal = params.originalTotal;
    (0, _mobx.makeObservable)(this, {
      patchedItemsEntries: _mobx.computed,
      isStale: _mobx.observable.ref,
      onEventuallyUpdated: _mobx.action
    });
  }
  get patchedItemsEntries() {
    return Array.from(this.patchedItems.entries());
  }
}
exports.OptimisticPatch = OptimisticPatch;
//# sourceMappingURL=CollectionOptimisticPatch.js.map