import { useRef } from 'react';

type UseCallRingtoneProps = {
  src: string;
};
const useCallRingtone = (props: UseCallRingtoneProps) => {
  const { src } = props;
  const audioRef = useRef<HTMLAudioElement | null>(null);

  const component = (
    <audio
      style={{ visibility: 'hidden' }}
      ref={audioRef}
      src={src}
      loop
    />
  );

  const playAudio = () => {
    if (audioRef?.current) {
      audioRef.current.src = src;
      audioRef.current.load();
      audioRef.current.play();
    }
  };

  const stopAudio = () => {
    if (audioRef?.current) {
      audioRef.current.pause();
      audioRef.current.currentTime = 0;
      audioRef.current.src = '';
      audioRef.current.load();
    }
  };

  return {
    component,
    playAudio,
    stopAudio,
  };
};

export default useCallRingtone;
