'use client'
import { useEffect, useState } from 'react'

export default function Counter({ end, duration = 2 }) {
    const [count, setCount] = useState(0)

    useEffect(() => {
        let start = 0
        const totalFrames = duration * 60 // 60 FPS
        const increment = end / totalFrames

        const counter = setInterval(() => {
            start += increment
            if (start >= end) {
                start = end
                clearInterval(counter)
            }
            setCount(Math.floor(start))
        }, 1000 / 60)

        return () => clearInterval(counter)
    }, [end, duration])

    return <>{count}</>
}
