import { Region , RegionCardinality , Regions } from "../../regions" ; import { FocusedCellCoordinates } from "../cell" ; import { Direction } from "../direction" ; import * as DirectionUtils from "./directionUtils" ; import * as FocusedCellUtils from "./focusedCellUtils" ; /**
 * Resizes the provided region by 1 row/column in the specified direction,
 * returning a new region instance. The region may either expand *or* contract
 * depending on the presence and location of the focused cell.
 *
 * If no focused cell is provided, the region will always be *expanded* in the
 * specified direction.
 *
 * If a focused cell *is* provided, the behavior will change depending on where
 * the focused cell is within the region:
 *
 *   1. If along a top/bottom boundary while resizing UP/DOWN, the resize will
 *      expand from or shrink to the focused cell (same if along a left/right
 *      boundary while moving LEFT/RIGHT).
 *   2. If *not* along a top/bottom boundary while resizing UP/DOWN (or if *not*
 *      along a left/right boundary while moving LEFT/RIGHT), the region will
 *      simply expand in the specified direction.
 *
 * Other notes:
 * - A CELLS region can be resized vertically or horizontally.
 * - A FULL_ROWS region can be resized only vertically.
 * - A FULL_COLUMNS region can be resized only horizontally.
 * - A FULL_TABLE region cannot be resized.
 *
 * This function does not clamp the indices of the returned region; that is the
 * responsibility of the caller.
 */ export function resizeRegion ( region , direction , focusedCell ? ) { if ( Regions . getRegionCardinality ( region ) === RegionCardinality . FULL_TABLE ) { return region ; } const nextRegion = Regions . copy ( region ) ; let affectedRowIndex = 0 ; let affectedColumnIndex = 0 ; if ( focusedCell != null ) { const isAtTop = FocusedCellUtils . isFocusedCellAtRegionTop ( nextRegion , focusedCell ) ; const isAtBottom = FocusedCellUtils . isFocusedCellAtRegionBottom ( nextRegion , focusedCell ) ; const isAtLeft = FocusedCellUtils . isFocusedCellAtRegionLeft ( nextRegion , focusedCell ) ; const isAtRight = FocusedCellUtils . isFocusedCellAtRegionRight ( nextRegion , focusedCell ) ; if ( direction === Direction . UP ) { affectedRowIndex = isAtTop && ! isAtBottom ? 1 : 0 ; } else if ( direction === Direction . DOWN ) { affectedRowIndex = isAtBottom && ! isAtTop ? 0 : 1 ; } else if ( direction === Direction . LEFT ) { affectedColumnIndex = isAtLeft && ! isAtRight ? 1 : 0 ; } else { affectedColumnIndex = isAtRight && ! isAtLeft ? 0 : 1 ; } } else { affectedRowIndex = direction === Direction . DOWN ? 1 : 0 ; affectedColumnIndex = direction === Direction . RIGHT ? 1 : 0 ; } const delta = DirectionUtils . directionToDelta ( direction ) ; if ( nextRegion . rows != null ) { nextRegion . rows [ affectedRowIndex ] += delta . rows ; } if ( nextRegion . cols != null ) { nextRegion . cols [ affectedColumnIndex ] += delta . cols ; } return nextRegion ; }