/**
 * Calculate velocity from position change
 *
 * @param current - Current position
 * @param previous - Previous position
 * @param deltaTime - Time elapsed in milliseconds
 * @returns Velocity in pixels per frame
 */
function calculateVelocity(current, previous, deltaTime) {
    if (deltaTime === 0)
        return 0;
    // Convert to pixels per frame (assuming 60fps = 16.67ms per frame)
    const frameTime = 16.67;
    return ((current - previous) / deltaTime) * frameTime;
}
/**
 * Clamp scroll position to valid bounds
 *
 * @param position - Position to clamp
 * @param min - Minimum value
 * @param max - Maximum value
 * @returns Clamped position
 */
function clampScroll(position, min, max) {
    return Math.max(min, Math.min(max, position));
}
/**
 * Detect scroll direction from velocity
 *
 * @param velocity - Current velocity
 * @param threshold - Minimum velocity to detect direction (default: 0.1)
 * @returns 1 for down/right, -1 for up/left, 0 for stationary
 */
function detectScrollDirection(velocity, threshold = 0.1) {
    if (Math.abs(velocity) < threshold)
        return 0;
    return velocity > 0 ? 1 : -1;
}
/**
 * Check if user prefers reduced motion
 *
 * @returns True if prefers-reduced-motion is set
 */
function isReducedMotion() {
    if (typeof window === 'undefined')
        return false;
    return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
/**
 * Linear interpolation between two values
 *
 * @param start - Start value
 * @param end - End value
 * @param factor - Interpolation factor (0-1)
 * @returns Interpolated value
 */
function lerp(start, end, factor) {
    return start + (end - start) * factor;
}
/**
 * Calculate scroll progress as percentage
 *
 * @param current - Current scroll position
 * @param max - Maximum scroll position
 * @returns Progress from 0 to 1
 */
function calculateScrollProgress(current, max) {
    if (max === 0)
        return 0;
    return Math.max(0, Math.min(1, current / max));
}
/**
 * Normalize wheel delta across different browsers and input devices
 *
 * @param event - WheelEvent
 * @param orientation - Scroll orientation ('vertical' uses deltaY, 'horizontal' uses deltaX)
 * @returns Normalized delta and device detection
 */
function normalizeWheelDelta(event, orientation = 'vertical') {
    // Pick the axis that matches orientation.
    // For horizontal, prefer deltaX but fall back to deltaY (shift+wheel on
    // some browsers sends deltaY even for horizontal scrolling).
    const rawDelta = orientation === 'horizontal'
        ? (event.deltaX !== 0 ? event.deltaX : event.deltaY)
        : event.deltaY;
    let delta = 0;
    // Determine delta based on deltaMode
    switch (event.deltaMode) {
        case 0: // Pixels (most common)
            delta = rawDelta;
            break;
        case 1: // Lines
            delta = rawDelta * 16; // Approximate line height
            break;
        case 2: // Pages
            delta =
                rawDelta *
                    (orientation === 'horizontal'
                        ? window.innerWidth
                        : window.innerHeight);
            break;
    }
    // Detect trackpad vs mouse wheel
    // Trackpad typically has smaller, more frequent deltas
    const isTouchpad = Math.abs(delta) < 50;
    return { delta, isTouchpad };
}
/**
 * Get maximum scroll position for element
 *
 * @param element - Scrollable element
 * @param orientation - Scroll orientation
 * @returns Maximum scroll position
 */
function getMaxScroll(element, orientation = 'vertical') {
    if (orientation === 'horizontal') {
        return element.scrollWidth - element.clientWidth;
    }
    return element.scrollHeight - element.clientHeight;
}

export { calculateScrollProgress, calculateVelocity, clampScroll, detectScrollDirection, getMaxScroll, isReducedMotion, lerp, normalizeWheelDelta };
