import { computed, makeObservable, observable } from 'mobx';
export class CollectionOptimisticActionsState {
    constructor(params) {
        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 = observable.array([], {
                deep: false,
            });
            patchesCache.set(cacheKey, patches);
        }
        this.patches = patches;
        makeObservable(this, {
            latestMap: 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());
    }
}
//# sourceMappingURL=CollectionOptimisticActionsState.js.map