import { action, computed, makeObservable, reaction } from 'mobx';
import { BulkActionModalState, } from '@wix/bex-core';
import { createRef } from 'react';
export class BulkActionToolbarState {
    constructor(params) {
        this.bulkSelectionCheckboxRef = createRef();
        this.selectionDisabledProp = () => false;
        this.selectionDisabledFn = ({ key }) => {
            const { selectionDisabledProp } = this;
            if (typeof selectionDisabledProp === 'function') {
                const keyedItem = this._getCollectionOf(key)?.result.get(key);
                if (keyedItem == null) {
                    return false;
                }
                return Boolean(selectionDisabledProp(keyedItem));
            }
            return () => !!selectionDisabledProp;
        };
        this.getCheckboxTooltipContent = ({ key, }) => {
            const { checkboxTooltipContentProp } = this;
            if (typeof checkboxTooltipContentProp === 'function') {
                const keyedItem = this._getCollectionOf(key)?.result.get(key);
                if (keyedItem != null) {
                    return checkboxTooltipContentProp(keyedItem);
                }
            }
            return undefined;
        };
        this._selectionStartTime = 0;
        this.clearSelection = () => {
            this.multi.collections.forEach(({ bulkSelect }) => {
                bulkSelect.resetToDefaults();
            });
        };
        this.isRowCheckboxDisabled = action((row) => {
            return ((this.reachedMaxSelection && !this.isChecked(row.key)) ||
                this.selectionDisabledFn(row));
        });
        this.toggleSelectAll = action((params) => {
            if (!this.hasClickableRows) {
                this.deselectAll(params);
            }
            else if (this.maxSelection != null) {
                this.selectAllUpToMaxSelection();
            }
            else {
                this.selectAll(params);
            }
        });
        this.toolbarBIReporter = params.toolbarBIReporter;
        this.panels = params.panels;
        this.multi = params.multi;
        this.modals = new BulkActionModalState({
            translate: params.translate,
            scrollToTop: params.scrollToTop,
        });
        makeObservable(this, {
            hasClickableRows: computed,
            reachedMaxSelection: computed,
            total: computed,
            selectedCountOrTotal: computed,
            selectedIds: computed,
        });
    }
    get filteredCollections() {
        return this.shouldIncludeCollection
            ? this.multi.collections.filter(this.shouldIncludeCollection)
            : this.multi.collections;
    }
    get selectedIds() {
        return this.multi.collections.reduce((a, { bulkSelect }) => [...a, ...bulkSelect.selectedIds], []);
    }
    get selectedValues() {
        return this.multi.collections.reduce((a, { bulkSelect }) => [...a, ...bulkSelect.selectedValues], []);
    }
    get uncheckedValues() {
        return this.multi.collections.reduce((a, { bulkSelect }) => [...a, ...bulkSelect.uncheckedValues], []);
    }
    get selectedCount() {
        return this.filteredCollections.reduce((s, { bulkSelect }) => s + bulkSelect.selectedCount, 0);
    }
    get selectedCountOrTotal() {
        if (this.allSelected) {
            return this.filteredCollections.reduce((s, { bulkSelect }) => s + bulkSelect.selectedCountOrTotal, 0);
        }
        return this.selectedCount;
    }
    get hasClickableRows() {
        return this.filteredCollections.some(({ bulkSelect, ...collection }) => collection.result.keyedItems.some((row) => !bulkSelect.select.isChecked(row.key) &&
            !this.isRowCheckboxDisabled(row)));
    }
    get allSelected() {
        return this.filteredCollections.every(({ bulkSelect }) => bulkSelect.allSelected);
    }
    get reachedMaxSelection() {
        const { maxSelection, selectedCount } = this;
        if (maxSelection != null) {
            return selectedCount >= maxSelection;
        }
        return false;
    }
    get total() {
        return this.filteredCollections.reduce((s, { result: { total } }) => s + total, 0);
    }
    isChecked(key) {
        return this.multi.collections.some(({ bulkSelect }) => bulkSelect.isChecked(key));
    }
    selectAllUpToMaxSelection() {
        for (const collection of this.filteredCollections) {
            if (this.reachedMaxSelection) {
                break;
            }
            for (const item of collection.result.keyedItems) {
                if (this.reachedMaxSelection) {
                    break;
                }
                if (!this.isRowCheckboxDisabled(item)) {
                    collection.bulkSelect.select.set(item);
                }
            }
        }
    }
    selectAll({ location }) {
        const onSelectAllToggledEnd = this.toolbarBIReporter.selectAllToggledStart({
            location,
        });
        this.filteredCollections.forEach((collection) => collection.bulkSelect.selectAll());
        onSelectAllToggledEnd();
    }
    deselectAll({ location }) {
        const onSelectAllToggledEnd = this.toolbarBIReporter.selectAllToggledStart({
            location,
        });
        this.filteredCollections.forEach((collection) => collection.bulkSelect.deselectAll());
        onSelectAllToggledEnd();
        this.bulkSelectionCheckboxRef.current?.focus();
    }
    _getCollectionOf(id) {
        return this.multi.collections.find((collection) => collection.result.has(id));
    }
    selectOne(id) {
        const onSelectionToggledEnd = this.toolbarBIReporter.itemSelectionToggledStarted({
            itemId: id,
            clickType: 'Selection',
        });
        this._getCollectionOf(id)?.bulkSelect.selectOne(id);
        onSelectionToggledEnd();
    }
    deselectOne(id) {
        const onSelectionToggledEnd = this.toolbarBIReporter.itemSelectionToggledStarted({
            itemId: id,
            clickType: 'Deselection',
        });
        this._getCollectionOf(id)?.bulkSelect.deselectOne(id);
        onSelectionToggledEnd();
    }
    init() {
        const disposers = [
            ...this.multi.collections.map(({ bulkSelect }) => reaction(() => ({
                selectedValues: bulkSelect.select.selectedValues,
            }), ({ selectedValues }) => {
                if (this.selectionDisabledFn == null) {
                    return;
                }
                for (const keyedItem of selectedValues) {
                    const collectionItem = bulkSelect.getCollectionItem(keyedItem.key);
                    if (collectionItem && this.selectionDisabledFn(collectionItem)) {
                        bulkSelect.deselectOneItem(keyedItem);
                    }
                }
            })),
        ];
        return () => {
            disposers.forEach((d) => d());
        };
    }
}
//# sourceMappingURL=BulkActionToolbarState.js.map