"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CollectionOptimisticActionsState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
class CollectionOptimisticActionsState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "patches", void 0);
    (0, _defineProperty2.default)(this, "_patchesBeforeUnmount", null);
    const {
      source: {
        queryName
      },
      patchesCache,
      cacheNamespace
    } = params;
    const cacheKey = [cacheNamespace, queryName].filter(e => e != null).join(':');
    let patches = patchesCache.get(cacheKey);
    if (patches == null) {
      patches = _mobx.observable.array([], {
        deep: false
      });
      patchesCache.set(cacheKey, patches);
    }
    this.patches = patches;
    (0, _mobx.makeObservable)(this, {
      latestMap: _mobx.computed
    });
  }
  get latestMap() {
    return this.patches.reduce((map, optimisticPatch) => {
      const {
        patchedItemsEntries,
        originalItems
      } = optimisticPatch;
      for (const [key, item] of patchedItemsEntries) {
        const originalItem = originalItems.get(key);
        if (originalItem) {
          map.set(key, {
            optimisticPatch,
            item,
            originalItem
          });
        }
      }
      return map;
    }, new Map());
  }
}
exports.CollectionOptimisticActionsState = CollectionOptimisticActionsState;
//# sourceMappingURL=CollectionOptimisticActionsState.js.map