import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useRef, useState } from 'react';
import { RowNumberCell } from '../cells/row-number-cell.js';
import { useInternalTableState } from '../table-provider/table-provider.js';
import { TableRow } from './table-row.js';
import { RenderCell } from '../cells/render-cell/render-cell.js';
import { InformationCell } from '../cells/information-cell.js';
import { RowActions } from './row-actions.js';
const RenderRow = ({ item, rowIndex, mode = 'default' }) => {
    const { config, state: { selectedRowIndexes, selectedCellIndex }, api, } = useInternalTableState();
    const { allowRowSelection } = config;
    /**
     * Create a handler for the click event on the table numbering cell
     * This handler is used to select a row when the ctrl key is pressed
     * or select single rows when the index is clicked
     */
    const createSelectRowOnClickHandler = useCallback((index) => (event) => {
        if (!allowRowSelection)
            return;
        if (event.ctrlKey || event.metaKey) {
            event.stopPropagation();
            api.selection.toggleRow(index);
        }
        else if (event.shiftKey) {
            return; // just let the row handle it
        }
        else {
            api.selection.selectRow(index);
        }
    }, [allowRowSelection, api.selection]);
    /**
     * Create a handler for the click event on the table row
     * This handler is used to select a row when the ctrl key is pressed
     * in any other place other than the index cell or select a range of rows
     * when the shift key is pressed
     */
    const createAddSelectedRowHandler = useCallback((index) => (event) => {
        if (!allowRowSelection)
            return;
        if (event.ctrlKey || event.metaKey) {
            api.selection.toggleRow(index);
        }
        else if (event.shiftKey) {
            // Prevent text selection when shift key is pressed
            document.getSelection()?.removeAllRanges();
            api.selection.selectFromLastActiveRow(index);
        }
    }, [allowRowSelection, api.selection]);
    /**
     * Prevent text selection when shift key is pressed so that we
     * can select a range of rows while keeping a good user experience
     */
    const handleMouseDown = useCallback((event) => {
        if (event.shiftKey) {
            document.getSelection()?.removeAllRanges();
        }
    }, []);
    const rowRef = useRef(null);
    // actions state management
    const [actionsOpen, setActionsOpen] = useState(false);
    const [isSecondaryActionsOpen, setIsSecondaryActionsOpen] = useState(false);
    const handleRowMouseEnter = useCallback(() => {
        setActionsOpen(true);
    }, []);
    const handleRowMouseLeave = useCallback(() => {
        setActionsOpen(false);
    }, []);
    const handleSecondaryActionsOpen = useCallback(() => {
        setIsSecondaryActionsOpen(true);
    }, []);
    const handleSecondaryActionsClose = useCallback(() => {
        setIsSecondaryActionsOpen(false);
    }, []);
    return (_jsxs(TableRow, { ref: rowRef, index: rowIndex, onClick: createAddSelectedRowHandler(rowIndex), onMouseDown: handleMouseDown, onMouseEnter: handleRowMouseEnter, onMouseLeave: handleRowMouseLeave, children: [_jsx(RowNumberCell, { index: rowIndex + 1, handleSelectRowOnClick: createSelectRowOnClickHandler(rowIndex), selected: selectedRowIndexes.includes(rowIndex) || selectedCellIndex?.row === rowIndex }), config.columns.map((column, columnIndex) => {
                return (_jsx(RenderCell, { rowItem: item, column: column, rowIndex: rowIndex, columnIndex: columnIndex, renderEmptyCell: mode === 'empty' }, column.field));
            }), (api.isEditable() || api.canDelete()) && _jsx(InformationCell, { rowIndex: rowIndex, emptyRow: mode === 'empty' }), config.actions && mode === 'default' && (_jsx(RowActions, { row: item, open: actionsOpen, rowIndex: rowIndex, rowRef: rowRef, primaryAction: config.actions.primary, secondaryActions: config.actions.secondary, isSecondaryActionsOpen: isSecondaryActionsOpen, handleSecondaryActionsOpen: handleSecondaryActionsOpen, handleSecondaryActionsClose: handleSecondaryActionsClose }))] }));
};
export { RenderRow };
//# sourceMappingURL=render-row.js.map