"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.GridFoldersState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _GridBaseState = require("./GridBaseState");
var _mobx = require("mobx");
var _ToolbarCollectionState = require("./ToolbarCollectionState");
var _FoldersAndItemsCollectionsState = require("./FoldersAndItemsCollectionsState");
var _gridSizePresets = require("./gridSizePresets");
var _GridFoldersBIReporter = require("./GridFoldersBIReporter");
class GridFoldersState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "container", void 0);
    (0, _defineProperty2.default)(this, "toolbar", void 0);
    (0, _defineProperty2.default)(this, "items", void 0);
    (0, _defineProperty2.default)(this, "folders", void 0);
    (0, _defineProperty2.default)(this, "collections", void 0);
    (0, _defineProperty2.default)(this, "bi", void 0);
    (0, _defineProperty2.default)(this, "_subGrids", []);
    (0, _defineProperty2.default)(this, "_sizePreset", undefined);
    (0, _defineProperty2.default)(this, "showAllFolders", false);
    (0, _defineProperty2.default)(this, "toggleShowAllFolders", (0, _mobx.action)(() => {
      this.showAllFolders = !this.showAllFolders;
      this.bi.onToggleShowAllButtonClick();
    }));
    (0, _defineProperty2.default)(this, "retryErrorState", (0, _mobx.action)(() => this.toolbar.retryErrorState()));
    this.container = params.container;
    this.toolbar = params.toolbar ?? new _ToolbarCollectionState.ToolbarCollectionState({
      collection: params.items,
      container: params.container,
      componentType: 'GridFolders'
    });
    this.collections = params.collections ?? new _FoldersAndItemsCollectionsState.FoldersAndItemsCollectionsState({
      ...params,
      toolbar: this.toolbar
    });
    this.items = new _GridBaseState.GridBaseState({
      collection: params.items,
      container: params.container,
      toolbar: this.toolbar,
      sizePreset: _gridSizePresets.topGridSizePreset,
      parentState: this,
      layoutType: 'grid'
    });
    this.folders = new _GridBaseState.GridBaseState({
      collection: params.folders,
      container: params.container,
      toolbar: this.toolbar,
      sizePreset: _gridSizePresets.sideGridSizePreset,
      parentState: this,
      layoutType: 'grid'
    });
    this.bi = new _GridFoldersBIReporter.GridFoldersBIReporter({
      state: this
    });
    (0, _mobx.makeObservable)(this, {
      showAllFolders: _mobx.observable.ref,
      foldersLocalLimit: _mobx.computed,
      effectiveFoldersLocalLimit: _mobx.computed,
      showToggleShowAllLessButton: _mobx.computed,
      showItemsSection: _mobx.computed,
      showItemsSectionHeader: _mobx.computed,
      showFoldersSection: _mobx.computed,
      showFoldersSectionHeader: _mobx.computed,
      showLoadingSection: _mobx.computed
    });
  }
  get foldersLocalLimit() {
    return this.folders.columnsCount * 2;
  }
  get effectiveFoldersLocalLimit() {
    return this.showAllFolders ? null : this.folders.columnsCount * 2;
  }
  get showToggleShowAllLessButton() {
    return this.collections.folders.result.size > this.foldersLocalLimit;
  }
  get showFoldersSectionHeader() {
    return this.showToggleShowAllLessButton || this.showItemsSection;
  }
  get showItemsSection() {
    return !this.collections.items.result.isEmptyAndNotFetching && !this.collections.hasFoldersOnlyActiveFilters && (!this.showAllFolders || this.folders.collection.fetchedLastPage);
  }
  get initTask() {
    return this.toolbar.initTask;
  }
  get showErrorState() {
    return this.toolbar.multi.showErrorState;
  }
  get errorStatus() {
    return this.toolbar.multi.errorStatus;
  }
  get showLoadingState() {
    return this.toolbar.multi.showLoadingState;
  }
  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 showItemsSectionHeader() {
    return this.showFoldersSection;
  }
  get showFoldersSection() {
    return !this.collections.folders.result.isEmptyAndNotFetching && !this.collections.hasItemsOnlyActiveFilters;
  }
  get showLoadingSection() {
    const {
      collections: {
        hasFoldersOnlyActiveFilters,
        hasItemsOnlyActiveFilters
      },
      toolbar: {
        initTask,
        multi
      }
    } = this;
    const isLoadingToolbarAndCollection = () => multi.showInitialLoader && initTask.status.isLoading || initTask.status.isIdle;
    return !hasFoldersOnlyActiveFilters && !hasItemsOnlyActiveFilters && (multi.showRefreshLoader || isLoadingToolbarAndCollection());
  }
  init(params) {
    const disposers = [this.collections.init(params), (0, _mobx.reaction)(() => this.effectiveFoldersLocalLimit, effectiveFoldersLocalLimit => {
      this.folders._localLimit = effectiveFoldersLocalLimit;
    }, {
      fireImmediately: true
    })];
    return () => {
      disposers.forEach(d => d());
    };
  }
}
exports.GridFoldersState = GridFoldersState;
//# sourceMappingURL=GridFoldersState.js.map