"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.NestedTableNestedModeState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _bexCore = require("@wix/bex-core");
var _NestedCollectionState = require("./NestedCollectionState");
var _TableVirtualState = require("../TableVirtualState");
var _NestedTableNodeState = require("./NestedTableNodeState");
var _mobx = require("mobx");
var _getNodeOrLeaf = require("./getNodeOrLeaf");
var _NestedTableNestedModeStateBIReporter = require("./NestedTableNestedModeStateBIReporter");
var _util = require("@wix/bex-core/util");
var _createNestedOptimisticActions = require("./createNestedOptimisticActions");
class NestedTableNestedModeState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "wrapper", void 0);
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "bi", void 0);
    (0, _defineProperty2.default)(this, "nestedCollection", void 0);
    (0, _defineProperty2.default)(this, "virtual", void 0);
    (0, _defineProperty2.default)(this, "initTask", new _bexCore.TaskState());
    (0, _defineProperty2.default)(this, "root", void 0);
    (0, _defineProperty2.default)(this, "nestedTableDragAndDrop", null);
    (0, _defineProperty2.default)(this, "_indentationWidth", 28);
    (0, _defineProperty2.default)(this, "_parentCellPaddingStart", 24);
    (0, _defineProperty2.default)(this, "retryErrorState", (0, _mobx.action)(() => {
      const {
        initTask
      } = this;
      if (initTask.status.isError) {
        initTask.runOnce();
      }
    }));
    (0, _defineProperty2.default)(this, "expandCollapseAll", (0, _mobx.action)(() => {
      this.bi.reportExpandCollapseAll();
      const isAllCollapsed = this.isAllCollapsed;
      const nodes = this.root.getDescendants();
      for (const node of nodes) {
        if (node.expanded !== isAllCollapsed) {
          const nodeState = node.node;
          if (!nodeState) {
            continue;
          }
          nodeState._expanded = !nodeState._expanded;
        }
      }
    }));
    const {
      wrapper
    } = params;
    this.wrapper = wrapper;
    this.container = wrapper.container;
    this.bi = new _NestedTableNestedModeStateBIReporter.NestedTableNestedModeStateBIReporter(this);
    const rootLevelDescriptor = this.wrapper.levels[0];
    if (rootLevelDescriptor == null) {
      throw new Error('NestedTableState must have at least one level');
    }
    const nestedOptimisticActionsState = {
      parentId: 'null',
      sequences: wrapper.sequences,
      query: wrapper.query,
      levelDescriptor: rootLevelDescriptor
    };
    const rootNodeCollection = rootLevelDescriptor.createCollection(nestedOptimisticActionsState);
    (0, _createNestedOptimisticActions.createNestedOptimisticActions)(this.container, rootNodeCollection.collection, {
      ...nestedOptimisticActionsState,
      parentFilter: rootNodeCollection.parentFilter
    });
    this.nestedCollection = new _NestedCollectionState.NestedCollectionState({
      container: this.container,
      levels: wrapper.levels,
      query: wrapper.query,
      nodeCollection: rootNodeCollection,
      sequences: wrapper.sequences
    });
    this.root = new _NestedTableNodeState.NestedTableNodeState({
      nestedTable: this,
      node: this.nestedCollection.root,
      levelDescriptor: rootLevelDescriptor,
      parentNode: null,
      parent: null,
      preRegister: true
    });
    this.virtual = new _TableVirtualState.TableVirtualState({
      table: this,
      getVirtualItemKey: item => {
        var _item$item$state$pare;
        return [item.id, (_item$item$state$pare = item.item.state.parent) == null ? void 0 : _item$item$state$pare.id].filter(Boolean).join('_');
      }
    });
    (0, _mobx.makeObservable)(this, {
      hasExpandableItems: _mobx.computed
    });
  }
  getCollectionSnapshot() {
    var _toolbar$viewsState, _originQuery$sort;
    const {
      toolbar
    } = this;
    const {
      collection: {
        result: {
          originQuery,
          total
        }
      }
    } = toolbar;
    return {
      viewShown: (_toolbar$viewsState = toolbar.viewsState) == null || (_toolbar$viewsState = _toolbar$viewsState.views.currentView) == null ? void 0 : _toolbar$viewsState.id,
      tabShown: toolbar.getCurrentTabName(),
      sortOrderApplied: (_originQuery$sort = originQuery.sort) != null && _originQuery$sort.length ? JSON.stringify(originQuery.sort) : undefined,
      filtersApplied: JSON.stringify(originQuery.filtersKey),
      filteredListSizeShown: total
    };
  }
  get multi() {
    return this.toolbar.multi;
  }
  get tableState() {
    return this.wrapper.tableState;
  }
  get toolbar() {
    return this.wrapper.toolbar;
  }
  get total() {
    return this.nestedCollection.total;
  }
  get originQuery() {
    return this.toolbar.collection.originQuery;
  }
  get keyedItems() {
    return this.root.keyedItems;
  }
  get _keyedItemsMap() {
    return this.root._keyedItemsMap;
  }
  getKeyedItem(key) {
    return this.root.getKeyedItem(key);
  }
  getKeyedItemByIndex(ind) {
    return this.root.getKeyedItemByIndex(ind);
  }
  getNode(key) {
    const keyedItem = this.root._allKeyedItemsMap.get(key);
    if (keyedItem == null || keyedItem.item.originalKeyedItem == null) {
      return undefined;
    }
    const {
      item
    } = keyedItem;
    return (0, _getNodeOrLeaf.getNodeOrLeaf)(item.state, item.originalKeyedItem);
  }
  getAllNodes() {
    return this.root.getDescendants();
  }
  get expandAll() {
    return this.nestedCollection.expandAll;
  }
  get isFetchAllAborted() {
    return this.nestedCollection.isFetchAllAborted;
  }
  get isAllCollapsed() {
    return this.root.getDescendants().every(node => !node.expanded);
  }
  get hasExpandableItems() {
    return this.root.hasExpandableItems;
  }
  get showEmptyState() {
    return this.root.showEmptyState;
  }
  get hasAvailableItems() {
    return this.toolbar.multi.hasAvailableItems;
  }
  get resultOriginQuerySearch() {
    return this.toolbar.multi.resultOriginQuerySearch;
  }
  get hasNonPersistentActiveFilters() {
    return this.toolbar.multi.hasNonPersistentActiveFilters;
  }
  get showErrorState() {
    const {
      initTask
    } = this;
    return initTask.status.isError;
  }
  get showLoadingState() {
    return this.initTask.status.isLoading;
  }
  get errorStatus() {
    const {
      initTask
    } = this;
    return initTask.errorStatus;
  }
  get query() {
    return this.wrapper.query;
  }
  get levels() {
    return this.wrapper.levels;
  }
  init() {
    const {
      initTask,
      wrapper,
      root,
      nestedCollection
    } = this;
    const attemptFetchAllState = wrapper._attemptFetchAllAndPopulateCache();
    const disposers = [(0, _util.addEventListener)(root.collection.hooks, 'beforeInitialFetch', () => attemptFetchAllState.initTask.status.promise), (0, _util.addEventListener)(wrapper.rootNodeCollection.collection.hooks, 'beforeInitialFetch', () => attemptFetchAllState.initTask.status.promise), attemptFetchAllState.init(), wrapper.init(), root.init(), nestedCollection.init()];
    initTask.runOnce(async () => {
      wrapper.initTask.runOnce();
      attemptFetchAllState.initTask.runOnce();
      root.initTask.runOnce();
      await attemptFetchAllState.initTask.status.promise;
      await root.initTask.status.promise;
      await wrapper.initTask.status.promise;
    });
    return () => {
      disposers.forEach(d => d());
    };
  }
}
exports.NestedTableNestedModeState = NestedTableNestedModeState;
//# sourceMappingURL=NestedTableNestedModeState.js.map