/** * Returns the new index in the list, in a circular way. If next value is out of bonds from the total, * it will wrap to either 0 or itemCount - 1. * * @param {number} moveAmount Number of positions to move. Negative to move backwards, positive forwards. * @param {number} initialIndex The initial position to move from. * @param {number} itemCount The total number of items. * @returns {number} The new index after the move. */ // eslint-disable-next-line complexity const getNextWrappingIndex = ( moveAmount: number, initialIndex: number | null, itemCount: number ) => { const itemsLastIndex = itemCount - 1 if ( typeof initialIndex !== 'number' || initialIndex < 0 || initialIndex >= itemCount ) { initialIndex = moveAmount > 0 ? -1 : itemsLastIndex + 1 } const newIndex = initialIndex + moveAmount if (newIndex < 0) { return itemsLastIndex } if (newIndex > itemsLastIndex) { return 0 } return newIndex } export default getNextWrappingIndex