"use client";

import { useEffect, useState } from "react";

/**
 * The masthead's "Today" line as a vintage news display: amber dot-matrix
 * lettering scrolling across a dark, framed panel (like an old newsroom
 * "zipper" sign). A pause button stops it (WCAG 2.2.2), and it holds still
 * for visitors who ask for reduced motion. Screen readers get the text once.
 */
export default function VintageTicker({ label, text }: { label: string; text: string }) {
  const [paused, setPaused] = useState(false);
  const [still, setStill] = useState(false);

  useEffect(() => {
    const mq = window.matchMedia("(prefers-reduced-motion: reduce)");
    const on = () => setStill(mq.matches);
    on();
    mq.addEventListener("change", on);
    return () => mq.removeEventListener("change", on);
  }, []);

  const moving = !still && !paused;
  return (
    <div className="breaking-banner vintage-display" role="region" aria-label={label || "Today"}>
      {label && <span className="breaking-label">{label}</span>}
      <div className={`vd-window${moving ? " is-moving" : ""}`}>
        <span className="sr-only">{text}</span>
        <div className="vd-track" aria-hidden="true">
          <span className="vd-text">{text}</span>
          {moving && <span className="vd-text">{text}</span>}
        </div>
      </div>
      {!still && (
        <button
          type="button"
          className="vd-pause"
          aria-pressed={paused}
          aria-label={paused ? "Play the scrolling headline" : "Pause the scrolling headline"}
          onClick={() => setPaused((p) => !p)}
        >
          {paused ? (
            <svg width="14" height="14" viewBox="0 0 24 24" aria-hidden="true">
              <polygon points="6 4 20 12 6 20 6 4" fill="currentColor" />
            </svg>
          ) : (
            <svg width="14" height="14" viewBox="0 0 24 24" aria-hidden="true">
              <rect x="5" y="4" width="5" height="16" fill="currentColor" />
              <rect x="14" y="4" width="5" height="16" fill="currentColor" />
            </svg>
          )}
        </button>
      )}
    </div>
  );
}
