import { coord } from '../common';
export function mouseCell(grid, type) {
    return function (e, coords, td) {
        const { row, col: column } = coords;
        const payload = toMousePayload(e, type, grid, column, row);
        if (payload) {
            grid.fire({ type: 'GRID/mouse', payload });
        }
        if (payload && type === 'UP') {
            grid.fire({
                type: 'GRID/mouse',
                payload: toMousePayload(e, 'CLICK', grid, column, row),
            });
        }
    };
}
function toMousePayload(e, type, grid, column, row) {
    const cell = coord.cell.toKey(column, row);
    const cellType = coord.cell.toType({ row, column });
    if (!cellType) {
        return;
    }
    const payload = {
        cell,
        cellType,
        button: e.button === 2 ? 'RIGHT' : 'LEFT',
        grid,
        type,
        isCancelled: false,
        cancel: () => {
            e.preventDefault();
            e.stopImmediatePropagation();
            payload.isCancelled = true;
        },
    };
    return payload;
}
