import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { forwardRef, useEffect, useImperativeHandle, useRef, useState, } from "react";
import { useIntl } from "react-intl";
function _InstantCounter({ from = 0, to, framesPerSecond = 30, durationInSeconds, formatOptions = {}, autoStart = true, }, ref) {
    const [started, setStarted] = useState(autoStart);
    const initialValue = useRef(from);
    const spanRef = useRef(null);
    const { formatNumber } = useIntl();
    useImperativeHandle(ref, () => ({
        start: () => setStarted(true),
    }), []);
    useEffect(() => {
        if (started) {
            const incrementBy = (to - initialValue.current) / (framesPerSecond * durationInSeconds);
            spanRef.current.innerHTML = formatNumber(initialValue.current, formatOptions);
            const intervalId = setInterval(() => {
                initialValue.current += incrementBy;
                spanRef.current.innerHTML = formatNumber(initialValue.current, formatOptions);
                if (Math.round(initialValue.current) === Math.round(to)) {
                    clearInterval(intervalId);
                }
            }, 1000 / framesPerSecond);
            return () => {
                clearInterval(intervalId);
            };
        }
    }, [started, to]);
    return (_jsx(_Fragment, { children: _jsx("span", { ref: spanRef }) }));
}
/**
 * Increments the numbers from 0 to the provided value
 **/
const InstantCounter = forwardRef(_InstantCounter);
export default InstantCounter;
//# sourceMappingURL=InstantCounter.js.map