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 { useMemo } from 'react';
import { getMatrixDimensions } from './util';
const ColumnIndicator = ({ column, label, selected, onSelect, setContextMenu, deleteColumn, addColumnLeft, addColumnRight, columnContextEnable, cell, selectedColumn, minimumColumnWidth, maxColLimit, disableDeleteOption, contextMenu, onAddColumn, onDeleteColumn, setVisibleRange, scrollToColumn, }) => {
    const dispatch = useDispatch();
    const excelData = useSelector((state) => state.model.data);
    const columnWidth = useSelector((state) => state.columnDimensions[column]?.width ?? minimumColumnWidth);
    const matrixData = useSelector((state) => state.model.data);
    const { columnCount } = getMatrixDimensions(matrixData);
    const updateVisibleRangeEnd = (action) => {
        setVisibleRange((prev) => {
            const value = {
                ...prev,
                end: action === 'increment' ? prev.end + 1 : prev.end - 1,
            };
            if (selectedColumn) {
                scrollToColumn(selectedColumn);
            }
            return value;
        });
    };
    const options = useMemo(() => {
        return [
            {
                label: 'Add Column Left',
                value: 'Add Column Left',
                iconName: 'plus_icon',
                action: () => {
                    updateVisibleRangeEnd('increment');
                    addColumnLeft(minimumColumnWidth);
                    onAddColumn?.(selectedColumn ?? column, true);
                },
                disableTooltip: 'Column limit reached',
                visible: cell?.contextDisable?.['Add Column Left'] ?? false,
                disable: columnCount >= maxColLimit,
            },
            {
                label: 'Add Column Right',
                value: 'Add Column Right',
                iconName: 'plus_icon',
                action: () => {
                    updateVisibleRangeEnd('increment');
                    addColumnRight(minimumColumnWidth);
                    onAddColumn?.(selectedColumn ?? column, false);
                },
                disableTooltip: 'Column limit reached',
                visible: cell?.contextDisable?.['Add Column Right'] ?? false,
                disable: columnCount >= maxColLimit,
            },
            ...(!disableDeleteOption
                ? [
                    {
                        label: 'Delete Column',
                        value: 'Delete Column',
                        iconName: 'delete',
                        action: () => {
                            updateVisibleRangeEnd('decrement');
                            deleteColumn();
                            onDeleteColumn?.(selectedColumn ?? column);
                        },
                        disableTooltip: '',
                        visible: cell?.contextDisable?.['Delete Column'] ?? false,
                        disable: false,
                    },
                ]
                : []),
        ];
    }, [selectedColumn, column, excelData]);
    const onMouseDrag = React.useCallback((event, isRight) => {
        const getTargetColumn = isRight ? column - 1 : column;
        const targetColumn = Math.max(getTargetColumn, 0);
        onSelect(targetColumn, event.shiftKey);
        const startX = event.clientX;
        const initialWidth = Number(columnWidth);
        const onMouseMove = (moveEvent) => {
            const delta = moveEvent.clientX - startX;
            const newWidth = Math.max(minimumColumnWidth, initialWidth + delta);
            dispatch(Actions.setColumnWidth(targetColumn, newWidth));
        };
        const onMouseUp = () => {
            document.removeEventListener('mousemove', onMouseMove);
            document.removeEventListener('mouseup', onMouseUp);
            dispatch(Actions.dragEnd());
        };
        document.addEventListener('mousemove', onMouseMove);
        document.addEventListener('mouseup', onMouseUp);
        dispatch(Actions.dragStart());
    }, [column, columnWidth]);
    const activate = React.useCallback((point) => dispatch(Actions.activate(point)), [dispatch]);
    const handleClick = React.useCallback((event) => {
        onSelect(column, event.shiftKey);
    }, [onSelect, column]);
    const contextClick = React.useCallback((event) => {
        event.preventDefault();
        onSelect(column, event.shiftKey);
        setContextMenu({
            open: columnContextEnable,
            contextType: 'column',
            options,
        });
    }, [column, selectedColumn, columnContextEnable, options]);
    React.useEffect(() => {
        if (selectedColumn !== undefined && contextMenu?.contextType === 'column') {
            setContextMenu((prev) => ({
                open: prev.open,
                contextType: 'column',
                options,
            }));
        }
    }, [
        column,
        selectedColumn,
        columnContextEnable,
        options,
        contextMenu?.contextType === 'column',
    ]);
    return (_jsxs("th", { className: classNames('ff-spreadsheet-header', {
            'ff-spreadsheet-header--selected': selected,
        }), style: { width: `${columnWidth}px`, minWidth: '100px' }, onClick: handleClick, onContextMenu: contextClick, tabIndex: 0, children: [_jsx(Typography, { fontWeight: "medium", children: label ?? columnIndexToLabel(column) }), _jsx("div", { className: "drag-column-selector drag-column-left", onMouseDown: (e) => onMouseDrag(e, false), onDoubleClick: () => dispatch(Actions.setColumnPosition(column, columnWidth)), onClick: () => activate({ row: -1, column }) }), _jsx("div", { className: "drag-column-selector drag-column-right", onMouseDown: (e) => onMouseDrag(e, true), onDoubleClick: () => dispatch(Actions.setColumnPosition(column, columnWidth)), onClick: () => activate({ row: -1, column }) })] }, column));
};
export const enhance = (ColumnIndicatorComponent) => {
    return function ColumnIndicatorWrapper(props) {
        const dispatch = useDispatch();
        const selectEntireColumn = React.useCallback((column, extend) => dispatch(Actions.selectEntireColumn(column, extend)), [dispatch]);
        const selected = useSelector((state) => state.selected.hasEntireColumn(props.column));
        const selectedColumn = useSelector((state) => state.selectedColumn ?? undefined);
        const cell = useSelector((state) => state.active ? Matrix.get(state.active, state.model.data) : undefined);
        return (_jsx(ColumnIndicatorComponent, { ...props, cell: cell, selected: selected, selectedColumn: selectedColumn, onSelect: selectEntireColumn, scrollToColumn: props.scrollToColumn }));
    };
};
const EnhancedColumnIndicator = enhance(ColumnIndicator);
export default EnhancedColumnIndicator;
function columnIndexToLabel(column) {
    let label = '';
    let index = column;
    while (index >= 0) {
        label = String.fromCharCode(65 + (index % 26)) + label;
        index = Math.floor(index / 26) - 1;
    }
    return label;
}
//# sourceMappingURL=ColumnIndicator.js.map