import { CarouselConfig } from '../types' ; param fn param delay export function debounce ( fn , delay ) { let timerId ; return function ( ... args ) { if ( timerId ) { clearTimeout ( timerId ) ; } timerId = setTimeout ( ( ) => { fn ( ... args ) ; timerId = null ; } , delay ) ; } ; } /**
 * return a throttle version of the function
 * Throttling
 *
 */ export function throttle ( fn , limit ) { let inThrottle ; return function ( ... args ) { const self = this ; if ( ! inThrottle ) { fn . apply ( self , args ) ; inThrottle = true ; setTimeout ( ( ) => ( inThrottle = false ) , limit ) ; } } ; } export function getSlidesVNodes ( vNode ) { if ( ! vNode ) return [ ] ; if ( vNode [ 0 ] ?. type ?. name === 'CarouselSlide' ) return vNode ; return vNode [ 0 ] ?. children || [ ] ; } export function getMaxSlideIndex ( config , slidesCount ) { if ( config . wrapAround ) { return slidesCount - 1 ; } switch ( config . snapAlign ) { case 'start' : return slidesCount - config . itemsToShow ; case 'end' : return slidesCount - 1 ; case 'center' : case 'center-odd' : return slidesCount - Math . ceil ( config . itemsToShow / 2 ) ; case 'center-even' : return slidesCount - Math . ceil ( config . itemsToShow / 2 ) ; default : return 0 ; } } export function getMinSlideIndex ( config ) { if ( config . wrapAround ) { return 0 ; } switch ( config . snapAlign ) { case 'start' : return 0 ; case 'end' : return config . itemsToShow - 1 ; case 'center' : case 'center-odd' : return Math . floor ( ( config . itemsToShow - 1 ) / 2 ) ; case 'center-even' : return Math . floor ( ( config . itemsToShow - 2 ) / 2 ) ; default : return 0 ; } } export function getCurrentSlideIndex ( config , val , max , min ) { if ( config . wrapAround ) { return val ; } return Math . min ( Math . max ( val , min ) , max ) ; } export function getSlidesToScroll ( { slidesBuffer , currentSlide , snapAlign , itemsToShow , wrapAround , slidesCount , } ) { let output = slidesBuffer . indexOf ( currentSlide ) ; if ( snapAlign === 'center' || snapAlign === 'center-odd' ) { output -= ( itemsToShow - 1 ) / 2 ; } else if ( snapAlign === 'center-even' ) { output -= ( itemsToShow - 2 ) / 2 ; } else if ( snapAlign === 'end' ) { output -= itemsToShow - 1 ; } if ( ! wrapAround ) { const max = slidesCount - itemsToShow ; const min = 0 ; output = Math . max ( Math . min ( output , max ) , min ) ; } return output ; }