import { Num, Optional } from '@ephox/katamari';
const withGrid = (values, index, numCols, f) => {
    const oldRow = Math.floor(index / numCols);
    const oldColumn = index % numCols;
    return f(oldRow, oldColumn).bind((address) => {
        const newIndex = address.row * numCols + address.column;
        return newIndex >= 0 && newIndex < values.length ? Optional.some(values[newIndex]) : Optional.none();
    });
};
const cycleHorizontal = (values, index, numRows, numCols, delta) => withGrid(values, index, numCols, (oldRow, oldColumn) => {
    const onLastRow = oldRow === numRows - 1;
    const colsInRow = onLastRow ? values.length - (oldRow * numCols) : numCols;
    const newColumn = Num.cycleBy(oldColumn, delta, 0, colsInRow - 1);
    return Optional.some({
        row: oldRow,
        column: newColumn
    });
});
const cycleVertical = (values, index, numRows, numCols, delta) => withGrid(values, index, numCols, (oldRow, oldColumn) => {
    const newRow = Num.cycleBy(oldRow, delta, 0, numRows - 1);
    const onLastRow = newRow === numRows - 1;
    const colsInRow = onLastRow ? values.length - (newRow * numCols) : numCols;
    const newCol = Num.clamp(oldColumn, 0, colsInRow - 1);
    return Optional.some({
        row: newRow,
        column: newCol
    });
});
const cycleRight = (values, index, numRows, numCols) => cycleHorizontal(values, index, numRows, numCols, +1);
const cycleLeft = (values, index, numRows, numCols) => cycleHorizontal(values, index, numRows, numCols, -1);
const cycleUp = (values, index, numRows, numCols) => cycleVertical(values, index, numRows, numCols, -1);
const cycleDown = (values, index, numRows, numCols) => cycleVertical(values, index, numRows, numCols, +1);
export { cycleDown, cycleUp, cycleLeft, cycleRight };
//# sourceMappingURL=WrapArrNavigation.js.map