import { useEffect } from "react";
import type { LegacySessionAutoplayProps } from "./legacySessionActionTypes";

export function useLegacySessionAutoplay({
  autoPlaySpeed,
  setAutoPlaySpeed,
  stage,
  stepLife,
  trajectoryIsEnd,
}: LegacySessionAutoplayProps) {
  useEffect(() => {
    if (!autoPlaySpeed || stage !== "trajectory") {
      return;
    }

    const intervalId = window.setInterval(() => {
      if (trajectoryIsEnd) {
        setAutoPlaySpeed(null);
        return;
      }

      stepLife();
    }, autoPlaySpeed);

    return () => {
      window.clearInterval(intervalId);
    };
  }, [autoPlaySpeed, setAutoPlaySpeed, stage, stepLife, trajectoryIsEnd]);
}
