"use client";
import { useState, useEffect } from "react";
const useSmoothProgress = (options) => {
  const start = options.start ?? 0;
  const end = options.end ?? 1;
  const updateIntervalMs = options.updateIntervalMs;
  const totalSeconds = options.totalSeconds;
  const totalNumberOfIntervals = totalSeconds * 1e3 / updateIntervalMs;
  const intervalIncreaseAmount = (end - start) / totalNumberOfIntervals;
  const [progress, setProgress] = useState(void 0);
  const [isRunning, setIsRunning] = useState(false);
  const startProgress = () => {
    setIsRunning(true);
  };
  useEffect(() => {
    let interval;
    if (isRunning) {
      interval = setInterval(() => {
        setProgress((prev) => {
          if ((prev ?? start) >= end) {
            clearInterval(interval);
            setIsRunning(false);
            return end;
          } else {
            return (prev || start) + intervalIncreaseAmount;
          }
        });
      }, updateIntervalMs);
    }
    return () => {
      clearInterval(interval);
    };
  }, [isRunning]);
  return { progress, startProgress };
};
export {
  useSmoothProgress
};
