import { GridBaseState } from './GridBaseState';
import { action, computed, makeObservable, observable, reaction } from 'mobx';
import { ToolbarCollectionState } from './ToolbarCollectionState';
import { FoldersAndItemsCollectionsState, } from './FoldersAndItemsCollectionsState';
import { sideGridSizePreset, topGridSizePreset, } from './gridSizePresets';
import { GridFoldersBIReporter } from './GridFoldersBIReporter';
export class GridFoldersState {
    constructor(params) {
        this._subGrids = [];
        this._sizePreset = undefined;
        this.showAllFolders = false;
        this.toggleShowAllFolders = action(() => {
            this.showAllFolders = !this.showAllFolders;
            this.bi.onToggleShowAllButtonClick();
        });
        this.retryErrorState = action(() => this.toolbar.retryErrorState());
        this.container = params.container;
        this.toolbar =
            params.toolbar ??
                new ToolbarCollectionState({
                    collection: params.items,
                    container: params.container,
                    componentType: 'GridFolders',
                });
        this.collections =
            params.collections ??
                new FoldersAndItemsCollectionsState({
                    ...params,
                    toolbar: this.toolbar,
                });
        this.items = new GridBaseState({
            collection: params.items,
            container: params.container,
            toolbar: this.toolbar,
            sizePreset: topGridSizePreset,
            parentState: this,
            layoutType: 'grid',
        });
        this.folders = new GridBaseState({
            collection: params.folders,
            container: params.container,
            toolbar: this.toolbar,
            sizePreset: sideGridSizePreset,
            parentState: this,
            layoutType: 'grid',
        });
        this.bi = new GridFoldersBIReporter({
            state: this,
        });
        makeObservable(this, {
            showAllFolders: observable.ref,
            foldersLocalLimit: computed,
            effectiveFoldersLocalLimit: computed,
            showToggleShowAllLessButton: computed,
            showItemsSection: computed,
            showItemsSectionHeader: computed,
            showFoldersSection: computed,
            showFoldersSectionHeader: computed,
            showLoadingSection: 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),
            reaction(() => this.effectiveFoldersLocalLimit, (effectiveFoldersLocalLimit) => {
                this.folders._localLimit = effectiveFoldersLocalLimit;
            }, {
                fireImmediately: true,
            }),
        ];
        return () => {
            disposers.forEach((d) => d());
        };
    }
}
//# sourceMappingURL=GridFoldersState.js.map