/**
 * Snap the given value to the closest point, taking into account the velocity.
 */
export function snapPoint(
    value: number,
    velocity: number,
    points: number[],
    projectionScale = 0.2
) {
    "worklet"

    if (points.length === 0) {
        throw new Error("Must provide at least one snap-point.")
    }

    const projectedPoint = value + velocity * projectionScale
    const deltas = points.map((p) => Math.abs(projectedPoint - p))
    const minDelta = Math.min(...deltas)

    return points[deltas.indexOf(minDelta)] ?? null
}
