"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.NestedCollectionNodeState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _mobx = require("mobx");
var _createNestedOptimisticActions = require("./createNestedOptimisticActions");
class NestedCollectionNodeState {
  constructor(params) {
    var _this$parent;
    (0, _defineProperty2.default)(this, "nestedCollection", void 0);
    (0, _defineProperty2.default)(this, "collection", void 0);
    (0, _defineProperty2.default)(this, "levelDescriptor", void 0);
    (0, _defineProperty2.default)(this, "parent", void 0);
    (0, _defineProperty2.default)(this, "parentNode", void 0);
    (0, _defineProperty2.default)(this, "parentFilter", void 0);
    (0, _defineProperty2.default)(this, "parentKeyToChildrenMap", _mobx.observable.map([], {
      deep: false
    }));
    (0, _defineProperty2.default)(this, "_expanded", false);
    (0, _defineProperty2.default)(this, "_hovered", false);
    (0, _defineProperty2.default)(this, "_notifyAll", void 0);
    (0, _defineProperty2.default)(this, "initTask", new _bexCore.TaskState());
    const {
      nestedCollection
    } = params;
    this.nestedCollection = nestedCollection;
    this.levelDescriptor = params.levelDescriptor;
    this.parent = params.parent;
    this.parentNode = params.parentNode;
    this._notifyAll = params.notifyAll;
    (0, _mobx.runInAction)(() => {
      this._expanded = !nestedCollection.isFetchAllAborted && nestedCollection.expandAll;
    });
    const nestedOptimisticActionsState = {
      parentId: ((_this$parent = this.parent) == null ? void 0 : _this$parent.id) ?? 'null',
      sequences: nestedCollection.sequences,
      query: nestedCollection.query,
      levelDescriptor: this.levelDescriptor
    };
    const {
      collection,
      parentFilter
    } = params.nodeCollection ?? this.levelDescriptor.createCollection(nestedOptimisticActionsState);
    (0, _createNestedOptimisticActions.createNestedOptimisticActions)(this.nestedCollection.container, collection, {
      ...nestedOptimisticActionsState,
      parentFilter
    });
    if (!nestedCollection.isFetchAllAborted) {
      collection.queryObserver.setOptions({
        ...collection.queryObserver.options,
        staleTime: Infinity
      });
    }
    this.collection = collection;
    this.parentFilter = parentFilter;
    const {
      parent,
      nestedCollection: {
        query: sharedQuery
      }
    } = this;
    const {
      query
    } = collection;
    (0, _bexCore.syncWithSharedQuery)(query, sharedQuery);
    if (parent != null && parentFilter != null) {
      parentFilter.persistent = true;
      parentFilter.setValue([parent.key]);
    }
    (0, _mobx.makeObservable)(this, {
      init: _mobx.action,
      _toggleExpand: _mobx.action,
      _changeHover: _mobx.action,
      _expanded: _mobx.observable.ref,
      _hovered: _mobx.observable.ref,
      hoveredOrParentHovered: _mobx.computed
    });
  }
  async _toggleExpand() {
    this._expanded = !this._expanded;
    if (this._expanded) {
      try {
        await this.collection.initialFetch();
      } catch (e) {
        console.error(e);
      }
    }
  }
  get hoveredOrParentHovered() {
    var _this$parentNode;
    return this._hovered || !!((_this$parentNode = this.parentNode) != null && _this$parentNode.hoveredOrParentHovered);
  }
  _changeHover(hovered) {
    this._hovered = hovered;
  }
  init({
    skipCollectionInit
  } = {}) {
    const {
      initTask,
      collection,
      nestedCollection
    } = this;
    if (collection._optimisticActions) {
      nestedCollection.events.on('dispose', collection._optimisticActions.init());
    }
    const disposers = [!skipCollectionInit ? collection.init({
      skipInitialFetch: nestedCollection.isFetchAllAborted === true && !this._expanded,
      skipFilterListeners: true
    }) : undefined];
    initTask.runOnce(async () => {
      collection.initTask.runOnce();
      await collection.initTask.status.promise;
    });
    return () => {
      disposers.forEach(d => d == null ? void 0 : d());
    };
  }
  _createNode(parent, params) {
    const {
      nestedCollection
    } = this;
    return new NestedCollectionNodeState({
      parent,
      parentNode: this,
      nestedCollection,
      levelDescriptor: params.levelDescriptor,
      notifyAll: params.notifyAll
    });
  }
}
exports.NestedCollectionNodeState = NestedCollectionNodeState;
//# sourceMappingURL=NestedCollectionNodeState.js.map