import {ref} from 'vue';
import {useHapticFeedback} from './useHapticFeedback.js';

/**
 * Composable для Haptic Feedback по свайпу
 * @param {number} threshold - минимальное смещение для триггера
 * @param type impactOccurred или selectionChanged
 * @param feedback только для impactOccurred: 'light' | 'medium' | 'heavy' | 'soft' | 'rigid'
 * @returns {(swiper: Swiper) => void}
 */
export function useHapticScroll(
    threshold = 20,
    type = 'selectionChanged',
    feedback = 'soft'
) {
    const lastTranslate = ref(0);
    const haptic = useHapticFeedback();

    return function (
        swiper
    ) {
        const current = Math.abs(swiper.translate);
        const delta = Math.abs(current - lastTranslate.value);

        if (delta > threshold && haptic) {
            if (type === 'impactOccurred' && haptic.impactOccurred) {
                haptic.impactOccurred(feedback);
            } else if (type === 'selectionChanged' && haptic.selectionChanged) {
                haptic.selectionChanged();
            }
            lastTranslate.value = current;
        }
    };
}
