import { ReactNode } from "react";

type AnimatedWordsProps = {
  text: string;
  delayPerWord?: number;
};

const AnimatedWords = ({
  text,
  delayPerWord = 0.06,
}: AnimatedWordsProps) => {
  const words = text.trim().split(/\s+/);
  const result: ReactNode[] = [];

  words.forEach((word, i) => {
    result.push(
      <span
        key={`word-${i}`}
        className="overflow-hidden inline-block align-top"
      >
        <span
          className="word"
          style={{ animationDelay: `${i * delayPerWord}s` }}
        >
          {word}
        </span>
      </span>
    );
    if (i < words.length - 1) result.push(" ");
  });

  return <>{result}</>;
};

export default AnimatedWords;
