"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.TableFoldersState = void 0;
exports.isFolderKeyedItem = isFolderKeyedItem;
exports.isItemKeyedItem = isItemKeyedItem;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _ToolbarCollectionState = require("./ToolbarCollectionState");
var _FoldersAndItemsCollectionsState = require("./FoldersAndItemsCollectionsState");
var _TableState = require("./TableState");
var _TableVirtualState = require("./TableVirtualState");
function isItemKeyedItem(keyedItem) {
  return keyedItem.item._tag === 'item';
}
function isFolderKeyedItem(keyedItem) {
  return keyedItem.item._tag === 'folder';
}
class TableFoldersState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "table", void 0);
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    (0, _defineProperty2.default)(this, "virtual", void 0);
    (0, _defineProperty2.default)(this, "collections", void 0);
    (0, _defineProperty2.default)(this, "retryErrorState", () => this.toolbar.retryErrorState());
    this.container = params.container;
    this.virtual = new _TableVirtualState.TableVirtualState({
      table: this
    });
    this.toolbar = params.toolbar ?? new _ToolbarCollectionState.ToolbarCollectionState({
      collection: params.items,
      container: params.container,
      componentType: 'TableFolders'
    });
    this.collections = new _FoldersAndItemsCollectionsState.FoldersAndItemsCollectionsState({
      ...params,
      toolbar: this.toolbar
    });
    this.table = new _TableState.TableState({
      container: this.container,
      collection: params.items,
      toolbar: this.toolbar
    });
    (0, _mobx.makeObservable)(this, {
      showItemsSection: _mobx.computed,
      showFoldersSection: _mobx.computed,
      keyedItems: _mobx.computed
    });
    this.toolbar.bulk.shouldIncludeCollection = collection => {
      if (collection === this.collections.folders) {
        // Only exclude folders if they're explicitly hidden due to filters
        return !this.collections.hasItemsOnlyActiveFilters;
      }
      if (collection === this.collections.items) {
        // Only exclude items if they're explicitly hidden due to filters
        return !this.collections.hasFoldersOnlyActiveFilters;
      }
      return true;
    };
  }
  get showItemsSection() {
    return !this.collections.items.result.isEmptyAndNotFetching && !this.collections.hasFoldersOnlyActiveFilters && (!this.showFoldersSection || this.collections.folders.fetchedLastPage);
  }
  get showFoldersSection() {
    return !this.collections.folders.result.isEmptyAndNotFetching && !this.collections.hasItemsOnlyActiveFilters;
  }
  get tableState() {
    return this.table;
  }
  get showEmptyState() {
    return this.collections.showEmptyState;
  }
  get hasAvailableItems() {
    return this.collections.hasAvailableItems;
  }
  get resultOriginQuerySearch() {
    return this.collections.resultOriginQuerySearch;
  }
  get hasNonPersistentActiveFilters() {
    return this.collections.hasNonPersistentActiveFilters;
  }
  get showErrorState() {
    return this.toolbar.multi.showErrorState;
  }
  get errorStatus() {
    return this.toolbar.multi.errorStatus;
  }
  get showLoadingState() {
    const {
      initTask,
      toolbar
    } = this;
    return initTask.status.isIdle || initTask.status.isLoading || toolbar.multi.showLoadingState;
  }
  get initTask() {
    return this.toolbar.initTask;
  }
  get keyedItems() {
    const {
      showFoldersSection,
      showItemsSection,
      collections: {
        folders,
        items
      }
    } = this;
    return [...(showFoldersSection ? folders.result.keyedItems.map(keyedItem => ({
      ...keyedItem,
      item: {
        _tag: 'folder',
        originalKeyedItem: keyedItem
      }
    })) : []), ...(showItemsSection ? items.result.keyedItems.map(keyedItem => ({
      ...keyedItem,
      item: {
        _tag: 'item',
        originalKeyedItem: keyedItem
      },
      originalKeyedItem: keyedItem
    })) : [])];
  }
  init(params) {
    const disposers = [this.collections.init(params)];
    return () => {
      disposers.forEach(d => d());
    };
  }
}
exports.TableFoldersState = TableFoldersState;
//# sourceMappingURL=TableFoldersState.js.map