// Haptic feedback utilities for mobile devices

type HapticStyle = 'light' | 'medium' | 'heavy' | 'success' | 'warning' | 'error';

export function hapticFeedback(style: HapticStyle = 'light') {
    // Check if vibration API is available
    if (!navigator.vibrate) return;

    // Only trigger on touch devices
    if (!('ontouchstart' in window)) return;

    const patterns: Record<HapticStyle, number | number[]> = {
        light: 10,
        medium: 20,
        heavy: 40,
        success: [20, 50, 20], // Short-pause-short
        warning: [30, 30, 30], // Three quick pulses
        error: [50, 50, 50, 50, 100], // Intense pattern
    };

    try {
        navigator.vibrate(patterns[style]);
    } catch (e) {
        // Silently fail - vibration not critical
    }
}

// iOS haptic using AudioContext (for devices that support it)
export function iosHaptic() {
    if (typeof AudioContext === 'undefined') return;

    try {
        const audioCtx = new AudioContext();
        const oscillator = audioCtx.createOscillator();
        const gainNode = audioCtx.createGain();

        oscillator.connect(gainNode);
        gainNode.connect(audioCtx.destination);

        oscillator.type = 'sine';
        oscillator.frequency.value = 200;
        gainNode.gain.value = 0;

        oscillator.start();
        oscillator.stop(audioCtx.currentTime + 0.01);
    } catch (e) {
        // Silently fail
    }
}

// Hook for haptic feedback
import { useCallback } from 'react';

export function useHaptic() {
    const trigger = useCallback((style: HapticStyle = 'light') => {
        hapticFeedback(style);
    }, []);

    return { trigger };
}
