import { Num, Optional } from '@ephox/katamari';
const toCell = (matrix, rowIndex, columnIndex) => Optional.from(matrix[rowIndex]).bind((row) => Optional.from(row[columnIndex]).map((cell) => ({
    rowIndex,
    columnIndex,
    cell
})));
const cycleHorizontal = (matrix, rowIndex, startCol, deltaCol) => {
    const row = matrix[rowIndex];
    const colsInRow = row.length;
    const newColIndex = Num.cycleBy(startCol, deltaCol, 0, colsInRow - 1);
    return toCell(matrix, rowIndex, newColIndex);
};
const cycleVertical = (matrix, colIndex, startRow, deltaRow) => {
    const nextRowIndex = Num.cycleBy(startRow, deltaRow, 0, matrix.length - 1);
    const colsInNextRow = matrix[nextRowIndex].length;
    const nextColIndex = Num.clamp(colIndex, 0, colsInNextRow - 1);
    return toCell(matrix, nextRowIndex, nextColIndex);
};
const moveHorizontal = (matrix, rowIndex, startCol, deltaCol) => {
    const row = matrix[rowIndex];
    const colsInRow = row.length;
    const newColIndex = Num.clamp(startCol + deltaCol, 0, colsInRow - 1);
    return toCell(matrix, rowIndex, newColIndex);
};
const moveVertical = (matrix, colIndex, startRow, deltaRow) => {
    const nextRowIndex = Num.clamp(startRow + deltaRow, 0, matrix.length - 1);
    const colsInNextRow = matrix[nextRowIndex].length;
    const nextColIndex = Num.clamp(colIndex, 0, colsInNextRow - 1);
    return toCell(matrix, nextRowIndex, nextColIndex);
};
// return address(Math.floor(index / columns), index % columns);
const cycleRight = (matrix, startRow, startCol) => cycleHorizontal(matrix, startRow, startCol, +1);
const cycleLeft = (matrix, startRow, startCol) => cycleHorizontal(matrix, startRow, startCol, -1);
const cycleUp = (matrix, startRow, startCol) => cycleVertical(matrix, startCol, startRow, -1);
const cycleDown = (matrix, startRow, startCol) => cycleVertical(matrix, startCol, startRow, +1);
const moveLeft = (matrix, startRow, startCol) => moveHorizontal(matrix, startRow, startCol, -1);
const moveRight = (matrix, startRow, startCol) => moveHorizontal(matrix, startRow, startCol, +1);
const moveUp = (matrix, startRow, startCol) => moveVertical(matrix, startCol, startRow, -1);
const moveDown = (matrix, startRow, startCol) => moveVertical(matrix, startCol, startRow, +1);
export { cycleRight, cycleLeft, cycleUp, cycleDown, moveLeft, moveRight, moveUp, moveDown };
//# sourceMappingURL=MatrixNavigation.js.map