import { useState, useEffect, useCallback, useRef } from "react";
import { getRandomPhrase } from "../constants/processingPhrases.js";
import { getRandomTip } from "../constants/tips.js";
function getRandomPhraseOrTip(disableTips = false) {
  if (disableTips) {
    return getRandomPhrase();
  }
  const showTip = Math.random() < 1 / 3;
  return showTip ? getRandomTip() : getRandomPhrase();
}
function usePhraseCycler({
  isActive,
  intervalMs = 8e3,
  disableTips = false
}) {
  const [phrase, setPhrase] = useState(() => getRandomPhraseOrTip(disableTips));
  const intervalRef = useRef(null);
  const nextPhrase = useCallback(() => {
    let newPhrase = getRandomPhraseOrTip(disableTips);
    let attempts = 0;
    while (newPhrase === phrase && attempts < 3) {
      newPhrase = getRandomPhraseOrTip(disableTips);
      attempts++;
    }
    setPhrase(newPhrase);
  }, [phrase, disableTips]);
  useEffect(() => {
    if (isActive) {
      setPhrase(getRandomPhraseOrTip(disableTips));
      intervalRef.current = setInterval(() => {
        setPhrase(getRandomPhraseOrTip(disableTips));
      }, intervalMs);
    } else {
      if (intervalRef.current) {
        clearInterval(intervalRef.current);
        intervalRef.current = null;
      }
    }
    return () => {
      if (intervalRef.current) {
        clearInterval(intervalRef.current);
        intervalRef.current = null;
      }
    };
  }, [isActive, intervalMs, disableTips]);
  return { phrase, nextPhrase };
}
export {
  usePhraseCycler
};
