import type { ItemCoords } from '../types'

export const getShiftIndex = (itemsInSlide = 0, itemsOffset = 0) => itemsInSlide + itemsOffset

export const getStartIndex = (index = 0, itemsCount = 0) => {
  if (itemsCount) {
    if (index >= itemsCount) {
      return itemsCount - 1
    }

    if (index > 0) {
      return index
    }
  }

  return 0
}

export const getActiveIndex = ({
  infinite = false,
  itemsCount = 0,
  startIndex = 0,
}) => (infinite ? startIndex : getStartIndex(startIndex, itemsCount))

export const getUpdateSlidePositionIndex = (activeIndex: number, itemsCount: number) => {
  if (activeIndex < 0) return itemsCount - 1
  if (activeIndex >= itemsCount) return 0

  return activeIndex
}

export const shouldRecalculateSlideIndex = (activeIndex: number, itemsCount: number) => (
  activeIndex < 0 || activeIndex >= itemsCount
)

export const shouldCancelSlideAnimation = (activeIndex: number, itemsCount: number) => (
  activeIndex < 0 || activeIndex >= itemsCount
)

export const getTransformationItemIndex = (
  transformationSet: Array<ItemCoords> = [],
  position = 0,
) => transformationSet.findIndex((item) => item.position >= Math.abs(position))

