/* ============================================================
   NELSON — Collage Hero  (4 equal vertical strips)
   ============================================================
   Four full-height columns of real product photography, each
   layered under an autoplay video loop where available.
   Text overlay cycles through editorial headline sets every 6 s.
   To swap imagery: update POSTERS / VIDEOS arrays below.
   ============================================================ */

/* No real product media yet — placeholders, category-accurate labels */
const POSTERS = [
(window.RB_IMGS && window.RB_IMGS["FORMAL · OXFORD"]) || "",
(window.RB_IMGS && window.RB_IMGS["CRAFT · DETAIL"]) || "",
(window.RB_IMGS && window.RB_IMGS["BOOTS · CHELSEA"]) || "",
(window.RB_IMGS && window.RB_IMGS["MAKER · PORTRAIT"]) || ""];

const VIDS = window.RB_VIDEOS || [];
const VIDEOS = [VIDS[0] || "", VIDS[1] || "", VIDS[2] || "", VIDS[3] || ""];


const SLIDES = [
{
  eyebrow: "Nelson",
  h: ["Shoes,", "Done Right."],
  cta: "Shop Now",
  ctaFn: (nav) => nav("shop", {})
},
{
  eyebrow: "The Formal Edit",
  h: ["Sharp,", "By Design."],
  cta: "Shop Formal",
  ctaFn: (nav) => nav("shop", { cat: "Formal" })
},
{
  eyebrow: "The Everyday Edit",
  h: ["Built for", "Every Day."],
  cta: "Shop Now",
  ctaFn: (nav) => nav("shop", {})
}];


/* ── Single video strip (poster still behind an autoplay video, or a Ph-style placeholder when no media yet) ── */
function ImageStrip({ src, videoSrc, delay, label }) {
  const videoRef = useRef(null);
  const [videoFailed, setVideoFailed] = useState(false);
  useEffect(() => {
    const el = videoRef.current;
    if (el) el.muted = true; // React's `muted` attr can lose the DOM property on some browsers — force it
  }, [videoSrc]);
  return (
    <div className="hcol-strip">
      {src ? (
        <img
          className="hcol-media hcol-poster"
          src={src}
          alt=""
          aria-hidden="true"
          style={{ animationDelay: delay + "ms", opacity: 1 }} />
      ) : (
        <div className="hcol-media hcol-poster ph portrait" style={{ position: "absolute", inset: 0, animationDelay: delay + "ms", display: "flex", alignItems: "flex-start", justifyContent: "center", paddingTop: "180px" }}>
          <span className="ph-label">{label}</span>
        </div>
      )}
      {videoSrc && !videoFailed ? (
        <video
          ref={videoRef}
          className="hcol-media"
          src={videoSrc}
          poster={src}
          autoPlay
          muted
          loop
          playsInline
          aria-hidden="true"
          onError={() => setVideoFailed(true)}
          style={{ animationDelay: delay + "ms" }} />
      ) : null}
    </div>);

}

/* ══════════════════════════════════════════════════════════
   MAIN COMPONENT
   ══════════════════════════════════════════════════════════ */
function HeroCollage({ onNav }) {
  const INTERVAL = 6000;

  const [idx, setIdx] = useState(0);
  const [textIn, setTextIn] = useState(true);

  useEffect(() => {
    const t = setInterval(() => {
      setTextIn(false);
      setTimeout(() => {
        setIdx((i) => (i + 1) % SLIDES.length);
        setTextIn(true);
      }, 450);
    }, INTERVAL);
    return () => clearInterval(t);
  }, []);

  const slide = SLIDES[idx];

  return (
    <section className="hero hcol-root" aria-label="Hero banner">

      {/* ── 4 equal vertical stock-image strips ── */}
      <div className="hcol-strips" aria-hidden="true">
        <ImageStrip src={POSTERS[0]} videoSrc={VIDEOS[0]} delay={0} label="FORMAL · OXFORD" />
        <ImageStrip src={POSTERS[1]} videoSrc={VIDEOS[1]} delay={200} label="CRAFT · DETAIL" />
        <ImageStrip src={POSTERS[2]} videoSrc={VIDEOS[2]} delay={400} label="BOOTS · CHELSEA" />
        <ImageStrip src={POSTERS[3]} videoSrc={VIDEOS[3]} delay={600} label="MAKER · PORTRAIT" />
      </div>

      {/* Cinematic gradient overlay */}
      <div className="hcol-overlay" aria-hidden="true" />

      {/* Gold top rule */}
      <div className="hcol-top-rule" aria-hidden="true" />

      {/* Vertical gap lines between strips */}
      <div className="hcol-dividers" aria-hidden="true">
        <span /><span /><span />
      </div>

      {/* Editorial text */}
      <div
        className={"hcol-content" + (textIn ? " hcol-content-in" : " hcol-content-out")}
        key={idx}>
        
        <p className="hcol-eyebrow mono">{slide.eyebrow}</p>
        <h1 className="display hcol-h">
          <span className="hcol-hline">{slide.h[0]}</span>
          <em className="hcol-hline hcol-hem">{slide.h[1]}</em>
        </h1>
        <div className="hcol-cta-row">
          <Btn onClick={() => slide.ctaFn(onNav)}>{slide.cta}</Btn>
        </div>
      </div>



      {/* Bottom corner label */}
      <div className="hcol-corner mono" aria-hidden="true">
        {(window.BRAND && window.BRAND.name) || "Nelson"}
      </div>

      {/* Scroll pulse line */}
      <div className="hcol-scroll" aria-hidden="true">
        <span className="hcol-scroll-line" />
      </div>

    </section>);

}

Object.assign(window, { HeroCollage });