import { computed, makeObservable } from 'mobx';
import { ToolbarCollectionState } from './ToolbarCollectionState';
import { FoldersAndItemsCollectionsState, } from './FoldersAndItemsCollectionsState';
import { TableState } from './TableState';
import { TableVirtualState } from './TableVirtualState';
export function isItemKeyedItem(keyedItem) {
    return keyedItem.item._tag === 'item';
}
export function isFolderKeyedItem(keyedItem) {
    return keyedItem.item._tag === 'folder';
}
export class TableFoldersState {
    constructor(params) {
        this.retryErrorState = () => this.toolbar.retryErrorState();
        this.container = params.container;
        this.virtual = new TableVirtualState({
            table: this,
        });
        this.toolbar =
            params.toolbar ??
                new ToolbarCollectionState({
                    collection: params.items,
                    container: params.container,
                    componentType: 'TableFolders',
                });
        this.collections = new FoldersAndItemsCollectionsState({
            ...params,
            toolbar: this.toolbar,
        });
        this.table = new TableState({
            container: this.container,
            collection: params.items,
            toolbar: this.toolbar,
        });
        makeObservable(this, {
            showItemsSection: computed,
            showFoldersSection: computed,
            keyedItems: 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());
        };
    }
}
//# sourceMappingURL=TableFoldersState.js.map