import { ToolbarCollectionState, } from './ToolbarCollectionState';
import { TableVirtualState } from './TableVirtualState';
import { LoadingRowState } from '../components/LoadingRow/LoadingRowState';
export class TableState {
    constructor(params) {
        this.tableDragAndDropState = null;
        this.retryErrorState = () => this.toolbar.retryErrorState();
        this.onAddItemActionComplete = async (items) => {
            return this.toolbar.onAddItemActionComplete(items);
        };
        this.sort = (columnId, { forceDirection } = {}) => {
            this.collection.sort({ id: columnId }, { forceDirection });
        };
        this.toolbar =
            params.toolbar ??
                new ToolbarCollectionState({
                    collection: params.collection,
                    container: params.container,
                    componentType: 'Table',
                });
        this.virtual = new TableVirtualState({
            table: this,
        });
        this.loadingRowState = new LoadingRowState();
    }
    get visibleColumns() {
        return this.toolbar.selectedOrderedColumnsOrAll;
    }
    // backwards compat
    get table() {
        return this.toolbar;
    }
    get collection() {
        return this.table.collection;
    }
    get tableState() {
        return this;
    }
    get itemsContentWidth() {
        return this.toolbar.itemsContentRect.rect.width ?? 0;
    }
    get itemsToRenderCount() {
        return this.collection.result.size;
    }
    init(params) {
        return this.table.init(params);
    }
    get keyedItems() {
        return this.collection.result.keyedItems;
    }
    getStickyColumnsCount(params) {
        const { horizontalScroll } = params;
        if (!horizontalScroll) {
            return params.stickyColumns;
        }
        const { tableDragAndDropState, toolbar } = this;
        const { customColumnsState, customColumnsProps, customColumnsStateInitialized, } = toolbar;
        const { stickySelectionColumn, showSelection } = params;
        let stickyColumns = params.stickyColumns ?? 0;
        const customColumnsIsEffective = customColumnsProps && customColumnsState && customColumnsStateInitialized;
        const reorderDisabledColumnsIsEffective = !!customColumnsState?.dragAndDrop?.orderedColumnsByReorderDisabled
            .reorderDisabled?.length;
        if (customColumnsIsEffective) {
            stickyColumns = customColumnsState.getStickyColumnsCount({
                ...params,
                stickyColumns,
            });
        }
        if (tableDragAndDropState) {
            stickyColumns = tableDragAndDropState.getStickyColumnsCount({
                ...params,
                stickyColumns,
            });
        }
        const checkboxColumnStickyCompat = reorderDisabledColumnsIsEffective ||
            stickySelectionColumn ||
            tableDragAndDropState != null;
        if (showSelection && checkboxColumnStickyCompat) {
            stickyColumns += 1;
        }
        return stickyColumns;
    }
    get showEmptyState() {
        return this.toolbar.multi.showEmptyState;
    }
    get hasAvailableItems() {
        return this.toolbar.multi.hasAvailableItems;
    }
    get resultOriginQuerySearch() {
        return this.toolbar.multi.resultOriginQuerySearch;
    }
    get hasNonPersistentActiveFilters() {
        return this.toolbar.multi.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;
    }
}
//# sourceMappingURL=TableState.js.map