import { useEffect, useState } from 'react';

const useTimerAudio = (isRecording: boolean) => {
  const [elapsedSeconds, setElapsedSeconds] = useState(0);

  useEffect(() => {
    let recordingInterval: ReturnType<typeof setInterval> | undefined;

    if (isRecording) {
      recordingInterval = setInterval(() => {
        setElapsedSeconds(prevSeconds => prevSeconds + 1);
      }, 1000);
    } else clearInterval(recordingInterval);

    return () => clearInterval(recordingInterval);
  }, [isRecording]);

  const recordingHours = Math.floor(elapsedSeconds / 3600);
  const recordingMinutes = Math.floor((elapsedSeconds % 3600) / 60);
  const recordingSeconds = elapsedSeconds % 60;
  const elapsedMilliseconds = elapsedSeconds * 1000;

  return {
    recordingHours,
    recordingMinutes,
    recordingSeconds,
    elapsedMilliseconds,
  };
};

export default useTimerAudio;
