"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get handleMouseEvent () {
        return handleMouseEvent;
    },
    get mouseDown () {
        return mouseDown;
    },
    get mouseOver () {
        return mouseOver;
    },
    get mouseUp () {
        return mouseUp;
    }
});
const _event = require("./../helpers/dom/event");
function mouseDown({ isShiftKey, isLeftClick, isRightClick, coords, selection, controller, cellCoordsFactory }) {
    const currentSelection = selection.isSelected() ? selection.getSelectedRange().current() : null;
    const selectedCorner = selection.isSelectedByCorner();
    const selectedRow = selection.isSelectedByRowHeader();
    selection.markSource('mouse');
    if (isShiftKey && currentSelection) {
        if (coords.row >= 0 && coords.col >= 0 && !controller.cell) {
            selection.setRangeEnd(coords);
        } else if ((selectedCorner || selectedRow) && coords.row >= 0 && coords.col >= 0 && !controller.cell) {
            selection.setRangeEnd(cellCoordsFactory(coords.row, coords.col));
        } else if (selectedCorner && coords.row < 0 && !controller.column) {
            selection.setRangeEnd(cellCoordsFactory(currentSelection.to.row, coords.col));
        } else if (selectedRow && coords.col < 0 && !controller.row) {
            selection.setRangeEnd(cellCoordsFactory(coords.row, currentSelection.to.col));
        } else if ((!selectedCorner && !selectedRow && coords.col < 0 || selectedCorner && coords.col < 0) && !controller.row) {
            selection.selectRows(Math.max(currentSelection.from.row, 0), coords.row, coords.col);
        } else if ((!selectedCorner && !selectedRow && coords.row < 0 || selectedRow && coords.row < 0) && !controller.column) {
            selection.selectColumns(Math.max(currentSelection.from.col, 0), coords.col, coords.row);
        }
    } else {
        const allowRightClickSelection = !selection.inInSelection(coords);
        const performSelection = isLeftClick || isRightClick && allowRightClickSelection;
        // clicked row header and when some column was selected
        if (coords.row < 0 && coords.col >= 0 && !controller.column) {
            if (performSelection) {
                selection.selectColumns(coords.col, coords.col, coords.row);
            }
        // clicked column header and when some row was selected
        } else if (coords.col < 0 && coords.row >= 0 && !controller.row) {
            if (performSelection) {
                selection.selectRows(coords.row, coords.row, coords.col);
            }
        } else if (coords.col >= 0 && coords.row >= 0 && !controller.cell) {
            if (performSelection) {
                selection.setRangeStart(coords);
            }
        } else if (coords.col < 0 && coords.row < 0) {
            selection.selectAll(true, true, {
                disableHeadersHighlight: true,
                focusPosition: {
                    row: 0,
                    col: 0
                }
            });
        }
    }
    selection.markEndSource();
}
function mouseOver({ isLeftClick, coords, selection, controller, cellCoordsFactory }) {
    if (!isLeftClick) {
        return;
    }
    const selectedRow = selection.isSelectedByRowHeader();
    const selectedColumn = selection.isSelectedByColumnHeader();
    const countCols = selection.tableProps.countCols();
    const countRows = selection.tableProps.countRows();
    selection.markSource('mouse');
    if (selectedColumn && !controller.column) {
        selection.setRangeEnd(cellCoordsFactory(countRows - 1, coords.col));
    } else if (selectedRow && !controller.row) {
        selection.setRangeEnd(cellCoordsFactory(coords.row, countCols - 1));
    } else if (!controller.cell) {
        selection.setRangeEnd(coords);
    }
    selection.markEndSource();
}
function mouseUp({ isLeftClick, selection, cellRangeMapper }) {
    if (!isLeftClick || selection.settings.selectionMode !== 'multiple') {
        return;
    }
    const selectionRange = selection.getSelectedRange();
    const renderableRange = selectionRange.clone().map((range)=>cellRangeMapper.toRenderable(range));
    const lastRenderableRange = renderableRange.current();
    if (renderableRange.size() > 1 && !lastRenderableRange.isHeader() && !selection.isMultiple(lastRenderableRange)) {
        const ranges = renderableRange.findAll(lastRenderableRange);
        // Mark the selection source as 'deselect' so `afterSetRangeEnd` in core.js skips the
        // viewport scroll for this dedup-driven refresh. Other side effects (`closeEditor`,
        // per-range `render` + `prepareEditor`, and the final batched render in
        // `afterSelectionFinished`) are guarded against the 'deselect' source in the same way
        // the existing 'refresh' source is handled - so a single batched render still happens
        // once the refresh completes.
        if (ranges.length === renderableRange.size()) {
            // if the last selection range is the same as the first one (case when the single cell
            // is selected twice or more) remove duplicate ranges
            selection.markSource('deselect');
            selectionRange.pop();
            selection.refresh();
            selection.markEndSource();
        } else if (ranges.length > 1) {
            selection.markSource('deselect');
            selectionRange.removeLayers(ranges.map(({ layer })=>layer));
            selection.refresh();
            selection.markEndSource();
        }
    }
}
const handlers = new Map([
    [
        'touchstart',
        mouseDown
    ],
    [
        'touchend',
        mouseUp
    ],
    [
        'mousedown',
        mouseDown
    ],
    [
        'mouseover',
        mouseOver
    ],
    [
        'mouseup',
        mouseUp
    ]
]);
function handleMouseEvent(event, options) {
    handlers.get(event.type)({
        isShiftKey: event.shiftKey,
        isLeftClick: (0, _event.isLeftClick)(event) || event.type === 'touchstart',
        isRightClick: (0, _event.isRightClick)(event),
        ...options
    });
}
