/* ============ News — media coverage / press list ============ */

function formatNewsDate(d) {
  if (!d) return "";
  const m = /^(\d{4})-(\d{2})-(\d{2})/.exec(d);
  return m ? `${m[1]}.${m[2]}.${m[3]}` : d;
}

function NewsCard({ n, idx }) {
  const [ref, seen] = useInView();
  const hasLink = !!n.url;
  const Tag = hasLink ? "a" : "div";
  const linkProps = hasLink ? { href: n.url, target: "_blank", rel: "noopener noreferrer" } : {};
  return (
    <Tag ref={ref} className={`news-card reveal ${seen ? "in" : ""} ${n.featured ? "feat" : ""}`}
      style={{ transitionDelay: (idx % 3) * 0.07 + "s" }} {...linkProps}>
      <div className="news-thumb">
        {n.image
          ? <img src={n.image} alt={n.title || "News"} />
          : <Placeholder label={String(n.outlet || n.type || "NEWS").toUpperCase()} k={idx} />}
        {n.type && <span className="news-type mono">{n.type}</span>}
      </div>
      <div className="news-meta mono">
        <span>{formatNewsDate(n.date)}</span>
        {n.outlet && <span className="news-outlet">{n.outlet}</span>}
      </div>
      <h3 className="news-title">{n.title}{hasLink && <span className="news-open mono">READ →</span>}</h3>
      {n.excerpt && <p className="news-excerpt">{n.excerpt}</p>}
    </Tag>
  );
}

function NewsSection() {
  const data = ((window.HASHBIT_DATA && window.HASHBIT_DATA.news) || [])
    .filter(n => n.published !== false)
    .slice()
    .sort((a, b) => (b.date || "").localeCompare(a.date || ""));
  if (!data.length) return null;
  return (
    <section className="news section wrap" id="news">
      <div className="sec-head">
        <div className="eyebrow"><span className="idx">04</span> News</div>
        <div className="mono sec-co">{String(data.length).padStart(2, "0")} ARTICLES</div>
      </div>
      <Lines className="news-h" items={["メディアに、取り上げていただきました", "News"]} />
      <div className="news-grid">
        {data.map((n, i) => <NewsCard key={n.id || i} n={n} idx={i} />)}
      </div>
    </section>
  );
}

Object.assign(window, { NewsSection });
