import { Region , RegionCardinality , Regions } from "../../regions" ; import { CellCoordinates , FocusedCellCoordinates } from "../cell" ; import * as Errors from "../errors" ; /**
 * Returns the `focusedSelectionIndex` if both the focused cell and that
 * property are defined, or the last index of `selectedRegions` otherwise. If
 * `selectedRegions` is empty, the function always returns `undefined`.
 */ export function getFocusedOrLastSelectedIndex ( selectedRegions , focusedCell ? ) { if ( selectedRegions . length === 0 ) { return undefined ; } else if ( focusedCell != null ) { return focusedCell . focusSelectionIndex ; } else { return selectedRegions . length - 1 ; } } /**
 * Returns the proper focused cell for the given set of initial conditions.
 */ export function getInitialFocusedCell ( enableFocusedCell , focusedCellFromProps , focusedCellFromState , selectedRegions ) { if ( ! enableFocusedCell ) { return undefined ; } else if ( focusedCellFromProps != null ) { return focusedCellFromProps ; } else if ( focusedCellFromState != null ) { return focusedCellFromState ; } else if ( selectedRegions . length > 0 ) { const lastIndex = selectedRegions . length - 1 ; return { ... Regions . getFocusCellCoordinatesFromRegion ( selectedRegions [ lastIndex ] ) , focusSelectionIndex : lastIndex , } ; } else { return { col : 0 , row : 0 , focusSelectionIndex : 0 } ; } } /**
 * Returns `true` if the focused cell is located along the top boundary of the
 * provided region, or `false` otherwise.
 */ export function isFocusedCellAtRegionTop ( region , focusedCell ) { return region . rows != null && focusedCell . row === region . rows [ 0 ] ; } /**
 * Returns `true` if the focused cell is located along the bottom boundary of
 * the provided region, or `false` otherwise.
 */ export function isFocusedCellAtRegionBottom ( region , focusedCell ) { return region . rows != null && focusedCell . row === region . rows [ 1 ] ; } /**
 * Returns `true` if the focused cell is located along the left boundary of the
 * provided region, or `false` otherwise.
 */ export function isFocusedCellAtRegionLeft ( region , focusedCell ) { return region . cols != null && focusedCell . col === region . cols [ 0 ] ; } /**
 * Returns `true` if the focused cell is located along the right boundary of the
 * provided region, or `false` otherwise.
 */ export function isFocusedCellAtRegionRight ( region , focusedCell ) { return region . cols != null && focusedCell . col === region . cols [ 1 ] ; } /**
 * Returns a new cell-coordinates object that includes a focusSelectionIndex property.
 * The returned object will have the proper FocusedCellCoordinates type.
 */ export function toFullCoordinates ( cellCoords , focusSelectionIndex = 0 ) { return { ... cellCoords , focusSelectionIndex } ; } /**
 * Expands an existing region to new region based on the current focused cell.
 * The focused cell is an invariant and should not move as a result of this
 * operation. This function is used, for instance, to expand a selected region
 * on shift+click.
 */ export function expandFocusedRegion ( focusedCell , newRegion ) { switch ( Regions . getRegionCardinality ( newRegion ) ) { case RegionCardinality . FULL_COLUMNS : { const [ indexStart , indexEnd ] = getExpandedRegionIndices ( focusedCell , newRegion , "col" , "cols" ) ; return Regions . column ( indexStart , indexEnd ) ; } case RegionCardinality . FULL_ROWS : { const [ indexStart , indexEnd ] = getExpandedRegionIndices ( focusedCell , newRegion , "row" , "rows" ) ; return Regions . row ( indexStart , indexEnd ) ; } case RegionCardinality . CELLS : const [ rowIndexStart , rowIndexEnd ] = getExpandedRegionIndices ( focusedCell , newRegion , "row" , "rows" ) ; const [ colIndexStart , colIndexEnd ] = getExpandedRegionIndices ( focusedCell , newRegion , "col" , "cols" ) ; return Regions . cell ( rowIndexStart , colIndexStart , rowIndexEnd , colIndexEnd ) ; default : return Regions . table ( ) ; } } function getExpandedRegionIndices ( focusedCell , newRegion , focusedCellDimension , regionDimension ) { const sourceIndex = focusedCell [ focusedCellDimension ] ; const [ destinationIndex , destinationIndexEnd ] = newRegion [ regionDimension ] ; if ( destinationIndex !== destinationIndexEnd ) { if ( regionDimension === "rows" ) { throw new Error ( Errors . TABLE_EXPAND_FOCUSED_REGION_MULTI_ROW_REGION ) ; } else if ( regionDimension === "cols" ) { throw new Error ( Errors . TABLE_EXPAND_FOCUSED_REGION_MULTI_COLUMN_REGION ) ; } } return sourceIndex <= destinationIndex ? [ sourceIndex , destinationIndex ] : [ destinationIndex , sourceIndex ] ; }