import { useState, useEffect, useRef } from "react";
function formatElapsedTime(ms) {
  const seconds = Math.floor(ms / 1e3);
  const tenths = Math.floor(ms % 1e3 / 100);
  if (seconds < 60) {
    return `${seconds}.${tenths}s`;
  }
  const minutes = Math.floor(seconds / 60);
  const remainingSeconds = seconds % 60;
  if (minutes < 60) {
    return `${minutes}m ${remainingSeconds}s`;
  }
  const hours = Math.floor(minutes / 60);
  const remainingMinutes = minutes % 60;
  return `${hours}h ${remainingMinutes}m ${remainingSeconds}s`;
}
function useElapsedTime({
  isActive,
  intervalMs = 100
}) {
  const [elapsedMs, setElapsedMs] = useState(0);
  const startTimeRef = useRef(null);
  const intervalRef = useRef(null);
  useEffect(() => {
    if (isActive) {
      startTimeRef.current = Date.now();
      setElapsedMs(0);
      intervalRef.current = setInterval(() => {
        if (startTimeRef.current !== null) {
          setElapsedMs(Date.now() - startTimeRef.current);
        }
      }, intervalMs);
    } else {
      if (intervalRef.current) {
        clearInterval(intervalRef.current);
        intervalRef.current = null;
      }
      startTimeRef.current = null;
      setElapsedMs(0);
    }
    return () => {
      if (intervalRef.current) {
        clearInterval(intervalRef.current);
        intervalRef.current = null;
      }
    };
  }, [isActive, intervalMs]);
  return {
    elapsedMs,
    formatted: formatElapsedTime(elapsedMs)
  };
}
export {
  useElapsedTime
};
