import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import * as Actions from './actions';
import * as Matrix from './matrix';
import useDispatch from './use-dispatch';
import useSelector from './use-selector';
import Typography from '../../../Typography';
import { getMatrixDimensions } from './util';
const RowIndicator = ({ row, label, selected, onSelect, setContextMenu, addRowTop, addRowBottom, deleteRow, rowContextEnable, selectedRow, cell, maxRowLimit, disableDeleteOption, contextMenu, setVisibleRange, scrollToRow, }) => {
    const dispatch = useDispatch();
    const rowHeight = useSelector((state) => state.rowDimensions[row]?.height || 32);
    const matrixData = useSelector((state) => state.model.data);
    const { rowCount } = getMatrixDimensions(matrixData);
    const updateVisibleRangeEnd = (action) => {
        setVisibleRange((prev) => {
            const value = {
                ...prev,
                end: action === 'increment' ? prev.end + 1 : prev.end - 1,
            };
            if (selectedRow) {
                scrollToRow(selectedRow);
            }
            return value;
        });
    };
    const options = React.useMemo(() => {
        return [
            {
                label: 'Add Row Top',
                value: 'Add Row Top',
                iconName: 'plus_icon',
                action: () => {
                    updateVisibleRangeEnd('increment');
                    addRowTop();
                },
                disableTooltip: 'Row limit reached',
                visible: cell?.contextDisable?.['Add Row Top'] ?? false,
                disable: rowCount >= maxRowLimit,
            },
            {
                label: 'Add Row Bottom',
                value: 'Add Row Bottom',
                iconName: 'plus_icon',
                action: () => {
                    updateVisibleRangeEnd('increment');
                    addRowBottom();
                },
                disableTooltip: 'Row limit reached',
                visible: cell?.contextDisable?.['Add Row Bottom'] ?? false,
                disable: rowCount >= maxRowLimit,
            },
            ...(!disableDeleteOption
                ? [
                    {
                        label: 'Delete Row',
                        value: 'Delete Row',
                        iconName: 'delete',
                        action: () => {
                            updateVisibleRangeEnd('decrement');
                            deleteRow();
                        },
                        disableTooltip: '',
                        visible: cell?.contextDisable?.['Delete Row'] ?? false,
                        disable: false,
                    },
                ]
                : []),
        ];
    }, [selectedRow, row]);
    const MIN_ROW_HEIGHT = 32;
    const handleMouseDrag = React.useCallback((event, isUp) => {
        const getTargetRow = isUp ? row : row - 1;
        const targetRow = Math.max(getTargetRow, 0);
        onSelect(targetRow, event.shiftKey);
        const startY = event.clientY;
        const initialHeight = rowHeight;
        const onMouseMove = (moveEvent) => {
            const delta = moveEvent.clientY - startY;
            const newHeight = Math.max(MIN_ROW_HEIGHT, initialHeight + delta);
            dispatch(Actions.setRowHeight(targetRow, newHeight));
        };
        const onMouseUp = () => {
            document.removeEventListener('mousemove', onMouseMove);
            document.removeEventListener('mouseup', onMouseUp);
            dispatch(Actions.dragEnd()); // only at end
        };
        document.addEventListener('mousemove', onMouseMove);
        document.addEventListener('mouseup', onMouseUp);
    }, [rowHeight, row]);
    const activate = React.useCallback((point) => dispatch(Actions.activate(point)), [dispatch]);
    const handleClick = React.useCallback((event) => {
        onSelect(row, event.shiftKey);
    }, [row]);
    const contextClick = React.useCallback((event) => {
        event.preventDefault();
        onSelect(row, event.shiftKey);
        setContextMenu({
            open: rowContextEnable,
            contextType: 'row',
            options,
        });
    }, [row, rowContextEnable, options, selectedRow]);
    React.useEffect(() => {
        if (selectedRow !== undefined && contextMenu?.contextType === 'row') {
            setContextMenu((prev) => ({
                open: prev.open,
                contextType: 'row',
                options,
            }));
        }
    }, [
        row,
        rowContextEnable,
        options,
        selectedRow,
        contextMenu?.contextType === 'row',
    ]);
    return (_jsxs("th", { className: classNames('ff-spreadsheet-header', {
            'ff-spreadsheet-header--selected': selected,
        }), style: { height: `${rowHeight}px`, minHeight: '32px' }, onClick: handleClick, onContextMenu: contextClick, tabIndex: 0, children: [_jsx(Typography, { fontWeight: "medium", children: label !== undefined ? label : row + 1 }), _jsx("div", { className: "drag-row-selector drag-row-down", onMouseDown: (e) => handleMouseDrag(e, true), onDoubleClick: () => dispatch(Actions.setRowHeight(row, rowHeight)), onClick: () => activate({ row, column: -1 }) }), _jsx("div", { className: "drag-row-selector drag-row-up", onMouseDown: (e) => handleMouseDrag(e, false), onDoubleClick: () => dispatch(Actions.setRowHeight(row, rowHeight)), onClick: () => activate({ row, column: -1 }) })] }));
};
export default RowIndicator;
export const enhance = (RowIndicatorComponent) => {
    return function RowIndicatorWrapper(props) {
        const dispatch = useDispatch();
        const selected = useSelector((state) => state.selected.hasEntireRow(props.row));
        const cell = useSelector((state) => state.active ? Matrix.get(state.active, state.model.data) : undefined);
        const selectedRow = useSelector((state) => state.selectedRow ?? undefined);
        const selectEntireRow = React.useCallback((row, extend) => dispatch(Actions.selectEntireRow(row, extend)), [dispatch]);
        return (_jsx(RowIndicatorComponent, { ...props, selected: selected, cell: cell, selectedRow: selectedRow, onSelect: selectEntireRow, scrollToRow: props.scrollToRow }));
    };
};
//# sourceMappingURL=RowIndicator.js.map