import { CarouselInternalState , CarouselProps } from "../types" ; function getOriginalCounterPart ( index , { slidesToShow , currentSlide } , childrenArr ) { if ( childrenArr . length > slidesToShow * 2 ) { const originalCouterPart = index + slidesToShow * 2 ; return originalCouterPart ; } else { if ( currentSlide >= childrenArr . length ) { return childrenArr . length + index ; } else { return index ; } } } interface Table { [ key ] : number ; } function getOriginalIndexLookupTableByClones ( slidesToShow , childrenArr ) { if ( childrenArr . length > slidesToShow * 2 ) { const table = { } ; const firstBeginningOfClones = childrenArr . length - slidesToShow * 2 ; const firstEndOfClones = childrenArr . length - firstBeginningOfClones ; let firstCount = firstBeginningOfClones ; for ( let i = 0 ; i < firstEndOfClones ; i ++ ) { table [ i ] = firstCount ; firstCount ++ ; } const secondBeginningOfClones = childrenArr . length + firstEndOfClones ; const secondEndOfClones = secondBeginningOfClones + childrenArr . slice ( 0 , slidesToShow * 2 ) . length ; let secondCount = 0 ; for ( let i = secondBeginningOfClones ; i <= secondEndOfClones ; i ++ ) { table [ i ] = secondCount ; secondCount ++ ; } const originalStart = firstEndOfClones ; const originalEnd = secondBeginningOfClones ; let originalCounter = 0 ; for ( let i = originalStart ; i < originalEnd ; i ++ ) { table [ i ] = originalCounter ; originalCounter ++ ; } return table ; } else { const table = { } ; const totalSlides = childrenArr . length * 3 ; let count = 0 ; for ( let i = 0 ; i < totalSlides ; i ++ ) { table [ i ] = count ; count ++ ; if ( count === childrenArr . length ) { count = 0 ; } } return table ; } } function getClones ( slidesToShow , childrenArr ) { if ( childrenArr . length < slidesToShow ) { return childrenArr ; } if ( childrenArr . length > slidesToShow * 2 ) { return [ ... childrenArr . slice ( childrenArr . length - slidesToShow * 2 , childrenArr . length ) , ... childrenArr , ... childrenArr . slice ( 0 , slidesToShow * 2 ) ] ; } return [ ... childrenArr , ... childrenArr , ... childrenArr ] ; } function getInitialSlideInInfiniteMode ( slidesToShow , childrenArr ) { if ( childrenArr . length > slidesToShow * 2 ) { return slidesToShow * 2 ; } else { return childrenArr . length ; } } function checkClonesPosition ( { currentSlide , slidesToShow , itemWidth , totalItems } , childrenArr , props ) { let nextSlide = 0 ; let nextPosition = 0 ; let isReachingTheEnd ; let isReachingTheStart = currentSlide === 0 ; const originalFirstSlide = childrenArr . length - ( childrenArr . length - slidesToShow * 2 ) ; if ( childrenArr . length < slidesToShow ) { nextSlide = 0 ; nextPosition = 0 ; isReachingTheEnd = false ; isReachingTheStart = false ; } else if ( childrenArr . length > slidesToShow * 2 ) { isReachingTheEnd = currentSlide >= originalFirstSlide + childrenArr . length ; if ( isReachingTheEnd ) { nextSlide = currentSlide - childrenArr . length ; nextPosition = - ( itemWidth * nextSlide ) ; } if ( isReachingTheStart ) { nextSlide = originalFirstSlide + ( childrenArr . length - slidesToShow * 2 ) ; nextPosition = - ( itemWidth * nextSlide ) ; } } else { isReachingTheEnd = currentSlide >= childrenArr . length * 2 ; if ( isReachingTheEnd ) { nextSlide = currentSlide - childrenArr . length ; nextPosition = - ( itemWidth * nextSlide ) ; } if ( isReachingTheStart ) { if ( props . showDots ) { nextSlide = childrenArr . length ; nextPosition = - ( itemWidth * nextSlide ) ; } else { nextSlide = totalItems - slidesToShow * 2 ; nextPosition = - ( itemWidth * nextSlide ) ; } } } return { isReachingTheEnd , isReachingTheStart , nextSlide , nextPosition } ; } export { getOriginalCounterPart , getOriginalIndexLookupTableByClones , getClones , checkClonesPosition , getInitialSlideInInfiniteMode } ;