// Pure CSS entrance animations — driven by CSS keyframes (compositor),
// so reveals are reliable regardless of JS scheduler / IntersectionObserver.
const { useRef, useEffect } = React;

// --- WordsPullUp (non-clipping char reveal — glyphs are never cropped) ---
window.WordsPullUp = function WordsPullUp({ text, className, style, showAsterisk = false, delay = 0 }) {
  const words = text.split(" ");
  return (
    <div className={className} style={style}>
      {words.map((word, wi) => {
        const isLast = wi === words.length - 1;
        const chars = [...word];
        return (
          <span key={`w${wi}`} className="inline-block whitespace-nowrap" style={{ marginRight: wi < words.length - 1 ? "0.18em" : 0 }}>
            {chars.map((ch, ci) => {
              const isFinalChar = isLast && ci === chars.length - 1 && showAsterisk;
              const d = delay + (wi * 0.08) + (ci * 0.06);
              return (
                <span
                  key={`w${wi}c${ci}`}
                  className="inline-block relative pullup-char anim-charrise"
                  style={{ animationDelay: `${d}s` }}
                >
                  {ch}
                  {isFinalChar && (
                    <span className="absolute" style={{ top: "0.05em", right: "-0.35em", fontSize: "0.31em", lineHeight: 1 }}>*</span>
                  )}
                </span>
              );
            })}
          </span>
        );
      })}
    </div>
  );
};

// --- WordsPullUpMultiStyle ---
window.WordsPullUpMultiStyle = function WordsPullUpMultiStyle({ segments, className }) {
  const allWords = [];
  segments.forEach((seg) => {
    seg.text.split(" ").filter(Boolean).forEach(w => allWords.push({ word: w, className: seg.className, segStyle: seg.style }));
  });
  return (
    <div className={`inline-flex flex-wrap justify-center ${className || ""}`}>
      {allWords.map((item, i) => (
        <span key={i} className="inline-block overflow-hidden align-top" style={{ marginRight: "0.22em" }}>
          <span
            className={`inline-block anim-pullup ${item.className}`}
            style={{ ...(item.segStyle || {}), animationDuration: "850ms", animationDelay: `${i * 0.07}s` }}
          >
            {item.word}
          </span>
        </span>
      ))}
    </div>
  );
};

// --- FadeUp (single element fade + slide up) ---
window.FadeUp = function FadeUp({ children, delay = 0, className, as: Tag = "div", style, ...rest }) {
  return (
    <Tag
      className={`anim-fadeup ${className || ""}`}
      style={{ ...(style || {}), animationDelay: `${delay}s` }}
      {...rest}
    >
      {children}
    </Tag>
  );
};

// --- AnimatedParagraph — staggered word fade-in ---
window.AnimatedParagraph = function AnimatedParagraph({ text, className }) {
  const words = text.split(" ");
  return (
    <p className={className}>
      {words.map((w, i) => (
        <React.Fragment key={i}>
          <span className="anim-charfade inline-block" style={{ animationDelay: `${0.1 + i * 0.05}s` }}>{w}</span>
          {i < words.length - 1 ? " " : ""}
        </React.Fragment>
      ))}
    </p>
  );
};

// --- FeatureCard with entrance animation ---
window.FeatureCard = function FeatureCard({ children, index, className = "", style }) {
  return (
    <div
      className={`relative overflow-hidden anim-scalein ${className}`}
      style={{ ...style, animationDelay: `${0.1 + index * 0.12}s` }}
    >
      {children}
    </div>
  );
};
