import { TableRowSelectionMode } from '../types';
import { DisabledSelectionManager } from './selection-managers/disabled-selection-manager';
import { MultiSelectionManager } from './selection-managers/multi-selection-manager';
import { SingleSelectionManager } from './selection-managers/single-selection-manager';
/**
 * Manages the behavior associated with interactive row selection for the table, including
 * handling when the selection mode of the table is changed.
 */
export class InteractiveSelectionManager {
    constructor(tanStackTable, selectionMode, actionMenusPreserveSelection) {
        this.tanStackTable = tanStackTable;
        this.actionMenusPreserveSelection = actionMenusPreserveSelection;
        this.selectionManager = this.createSelectionManager(selectionMode, actionMenusPreserveSelection);
    }
    handleRowSelectionToggle(rowState, isSelecting, shiftKey) {
        if (!rowState) {
            return false;
        }
        return this.selectionManager.handleRowSelectionToggle(rowState, isSelecting, shiftKey);
    }
    handleRowClick(rowState, shiftKey, ctrlKey) {
        if (!rowState) {
            return false;
        }
        return this.selectionManager.handleRowClick(rowState, shiftKey, ctrlKey);
    }
    handleActionMenuOpening(rowState) {
        if (!rowState) {
            return false;
        }
        return this.selectionManager.handleActionMenuOpening(rowState);
    }
    handleSelectionModeChanged(selectionMode) {
        this.selectionManager = this.createSelectionManager(selectionMode, this.actionMenusPreserveSelection);
    }
    handleActionMenusPreserveSelectionChanged(actionMenusPreserveSelection) {
        this.actionMenusPreserveSelection = actionMenusPreserveSelection;
        this.selectionManager.updateActionMenusPreserveSelection(actionMenusPreserveSelection);
    }
    handleSelectionReset() {
        this.selectionManager.reset();
    }
    getCurrentSelectedRecordIds() {
        const tanStackSelectionState = this.tanStackTable.options.state.rowSelection;
        if (!tanStackSelectionState) {
            return [];
        }
        const selectedRecordIds = [];
        Object.entries(tanStackSelectionState).forEach(([recordId, isSelected]) => {
            if (isSelected) {
                selectedRecordIds.push(recordId);
            }
        });
        return selectedRecordIds;
    }
    createSelectionManager(selectionMode, actionMenusPreserveSelection) {
        switch (selectionMode) {
            case TableRowSelectionMode.multiple:
                return new MultiSelectionManager(this.tanStackTable, actionMenusPreserveSelection);
            case TableRowSelectionMode.single:
                return new SingleSelectionManager(this.tanStackTable, actionMenusPreserveSelection);
            case TableRowSelectionMode.none:
                return new DisabledSelectionManager(this.tanStackTable, actionMenusPreserveSelection);
            default:
                throw new Error('unknown selection mode found');
        }
    }
}
//# sourceMappingURL=interactive-selection-manager.js.map