"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.NestedCollectionFetchAllState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _NestedCollectionFetchAllCollectionState = require("./NestedCollectionFetchAllCollectionState");
var _bexCore = require("@wix/bex-core");
var _events = require("events");
var _mobx = require("mobx");
class NestedCollectionFetchAllState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "nestedCollectionState", void 0);
    (0, _defineProperty2.default)(this, "events", new _events.EventEmitter());
    (0, _defineProperty2.default)(this, "fetchedAllPromise", void 0);
    (0, _defineProperty2.default)(this, "options", void 0);
    (0, _defineProperty2.default)(this, "query", void 0);
    (0, _defineProperty2.default)(this, "initTask", new _bexCore.TaskState());
    (0, _defineProperty2.default)(this, "collections", void 0);
    (0, _defineProperty2.default)(this, "isFetchAllAborted", false);
    const {
      nestedCollectionState
    } = params;
    this.options = params.options;
    this.query = params.query;
    const {
      levels
    } = nestedCollectionState;
    this.nestedCollectionState = nestedCollectionState;
    this.fetchedAllPromise = new Promise(resolve => {
      this.events.once('fetchAllAbortSignal', resolve);
    });
    this.collections = levels.map(levelDescriptor => {
      return new _NestedCollectionFetchAllCollectionState.NestedCollectionFetchAllCollectionState({
        levelDescriptor,
        fetchAllState: this
      });
    });
    (0, _mobx.makeObservable)(this, {
      total: _mobx.computed,
      _setInitialExpandAllByThreshold: _mobx.action
    });
  }
  init({
    invalidate
  } = {
    invalidate: false
  }) {
    const {
      initTask,
      fetchedAllPromise,
      nestedCollectionState,
      collections
    } = this;
    if (invalidate) {
      collections.forEach(c => {
        c.collection.invalidate();
      });
    }
    const disposers = [...collections.map(c => c.init())];
    initTask.runOnce(async () => {
      collections.forEach(({
        initTask
      }) => {
        initTask.runOnce();
      });
      const collectionsFetchAllPromise = Promise.all(collections.map(c => c.initTask.status.promise));
      await Promise.race([collectionsFetchAllPromise, fetchedAllPromise]);
      nestedCollectionState.isFetchAllAborted = this.isFetchAllAborted;
      nestedCollectionState._fetchAllTotal = this.total;
      if (this.isFetchAllAborted) {
        nestedCollectionState.expandAll = false; // override expandAll to false if fetchAll was aborted
      } else {
        this._setInitialExpandAllByThreshold();
        this._populateParentIdRequestsCache();
      }
    });
    return () => {
      disposers.forEach(d => d());
    };
  }
  _setInitialExpandAllByThreshold() {
    const {
      nestedCollectionState,
      total,
      options: {
        expandAllThreshold
      }
    } = this;
    nestedCollectionState.total = total;
    nestedCollectionState.expandAll = total <= expandAllThreshold;
  }
  _populateParentIdRequestsCache() {
    const {
      uniqueCollections
    } = this;
    uniqueCollections.forEach((c, index, arr) => {
      c._populateCache(arr[index + 1]);
    });
  }
  get uniqueCollections() {
    const {
      collections
    } = this;
    return Array.from(collections.reduce((map, c) => {
      map.set(c.collection.getCurrentQuery(), c);
      return map;
    }, new Map()).values());
  }
  get total() {
    const {
      uniqueCollections
    } = this;
    return uniqueCollections.reduce((acc, c) => {
      return acc + c.collection.result.total;
    }, 0);
  }
  _otherLevelSameCollectionExists(collection) {
    const {
      collections
    } = this;
    return collections.some(other => {
      return other !== collection && other.collection.getCurrentQuery() === collection.collection.getCurrentQuery();
    });
  }
}
exports.NestedCollectionFetchAllState = NestedCollectionFetchAllState;
//# sourceMappingURL=NestedCollectionFetchAllState.js.map