/**
 * Apply elastic edge effect at scroll boundaries
 * Creates a spring-back effect when scrolling past limits
 *
 * @param position - Current scroll position
 * @param min - Minimum scroll boundary (usually 0)
 * @param max - Maximum scroll boundary
 * @param strength - Effect strength (0-1, higher = more resistance)
 * @returns Modified position with elastic effect applied
 */
function applyElasticEdgeEffect(position, min, max, strength = 0.3) {
    // Clamp strength between 0 and 1
    const clampedStrength = Math.max(0, Math.min(1, strength));
    if (position < min) {
        // Below minimum - apply exponential damping
        const distance = min - position;
        const damped = distance * Math.exp(-distance * clampedStrength * 0.1);
        return min - damped;
    }
    if (position > max) {
        // Above maximum - apply exponential damping
        const distance = position - max;
        const damped = distance * Math.exp(-distance * clampedStrength * 0.1);
        return max + damped;
    }
    return position;
}
/**
 * Apply rubber-band overscroll effect
 * iOS-style resistance when scrolling past boundaries
 *
 * @param delta - Scroll delta to apply
 * @param position - Current scroll position
 * @param min - Minimum scroll boundary
 * @param max - Maximum scroll boundary
 * @param strength - Resistance strength (0-1, higher = more resistance)
 * @returns Modified delta with rubber-band effect
 */
function applyRubberBandEffect(delta, position, min, max, strength = 0.5) {
    // Clamp strength between 0 and 1
    const clampedStrength = Math.max(0, Math.min(1, strength));
    // Calculate how far we are from boundaries
    let distanceFromEdge = 0;
    if (position < min && delta < 0) {
        // Scrolling down past top
        distanceFromEdge = min - position;
    }
    else if (position > max && delta > 0) {
        // Scrolling up past bottom
        distanceFromEdge = position - max;
    }
    else {
        // Within bounds - no resistance
        return delta;
    }
    // Apply resistance based on distance from edge
    // Further from edge = more resistance
    const resistance = 1 - Math.min(1, distanceFromEdge / 100) * clampedStrength;
    return delta * Math.max(0.1, resistance);
}
/**
 * Calculate spring-back force for elastic edges
 * Returns velocity adjustment to spring back to bounds
 *
 * @param position - Current position
 * @param min - Minimum boundary
 * @param max - Maximum boundary
 * @param stiffness - Spring stiffness (higher = faster spring-back)
 * @returns Spring force to apply
 */
function calculateSpringForce(position, min, max, stiffness = 0.15) {
    if (position < min) {
        // Spring back up
        return (min - position) * stiffness;
    }
    if (position > max) {
        // Spring back down
        return (max - position) * stiffness;
    }
    return 0;
}

export { applyElasticEdgeEffect, applyRubberBandEffect, calculateSpringForce };
