"use client";

import { motion } from "framer-motion";

const EASE = [0.22, 1, 0.36, 1] as const;

export default function SectionHeading({
  eyebrow,
  title,
  intro,
  light = false,
}: {
  eyebrow: string;
  title: string;
  intro?: string;
  light?: boolean;
}) {
  const words = title.split(" ");
  return (
    <motion.div
      className="max-w-3xl"
      initial="hidden"
      whileInView="show"
      viewport={{ once: true, margin: "-80px" }}
    >
      <p className={`eyebrow flex items-center gap-4 ${light ? "text-gold-light" : "text-gold-ink"}`}>
        <motion.span
          className="block h-px bg-current"
          variants={{ hidden: { width: 0 }, show: { width: 40 } }}
          transition={{ duration: 1, ease: EASE }}
        />
        <motion.span
          variants={{ hidden: { opacity: 0, x: -10 }, show: { opacity: 1, x: 0 } }}
          transition={{ duration: 0.8, delay: 0.2, ease: EASE }}
        >
          {eyebrow}
        </motion.span>
      </p>
      <h2 className="mt-5 font-serif text-4xl leading-[1.12] md:text-[3.4rem]">
        {words.map((w, i) => (
          <span key={i} className="inline-block overflow-hidden pb-[0.1em] align-bottom">
            <motion.span
              className="inline-block"
              variants={{ hidden: { y: "110%" }, show: { y: "0%" } }}
              transition={{ duration: 1, delay: 0.1 + i * 0.05, ease: EASE }}
            >
              {w}
              {i < words.length - 1 && " "}
            </motion.span>
          </span>
        ))}
      </h2>
      {intro && (
        <motion.p
          className={`mt-6 text-base leading-relaxed md:text-lg ${light ? "text-cream/70" : "text-stone"}`}
          variants={{ hidden: { opacity: 0, y: 16 }, show: { opacity: 1, y: 0 } }}
          transition={{ duration: 1, delay: 0.3 + words.length * 0.05, ease: EASE }}
        >
          {intro}
        </motion.p>
      )}
    </motion.div>
  );
}
