const { useState: uS, useEffect: uE, useRef: uR } = window.React;

/* Three hero variations */

/* ─── Hero A — Manifesto split (title left, single mac window right) ─── */
const HeroA = ({ lang }) => {
  const sectionRef = uR(null);
  const dragRef = uR({ active: false, startX: 0, startY: 0, origX: 0, origY: 0 });
  const [pos, setPos] = uS({ x: 0, y: 0 });
  const [dragging, setDragging] = uS(false);

  const clamp = (x, y) => {
    const sec = sectionRef.current;
    if (!sec) return { x, y };
    const sw = sec.offsetWidth;
    const sh = sec.offsetHeight;
    // Left boundary: 42% of section (don't overlap title area)
    const minX = -(sw * 0.12);
    const maxX = sw * 0.22;
    const minY = -(sh * 0.28);
    const maxY = sh * 0.28;
    return { x: Math.max(minX, Math.min(maxX, x)), y: Math.max(minY, Math.min(maxY, y)) };
  };

  const onDown = (e) => {
    if (e.button !== 0) return;
    e.preventDefault();
    dragRef.current = { active: true, startX: e.clientX, startY: e.clientY, origX: pos.x, origY: pos.y };
    setDragging(true);
  };

  uE(() => {
    const onMove = (e) => {
      const d = dragRef.current;
      if (!d.active) return;
      const dx = e.clientX - d.startX;
      const dy = e.clientY - d.startY;
      setPos(clamp(d.origX + dx, d.origY + dy));
    };
    const onUp = () => {
      dragRef.current.active = false;
      setDragging(false);
    };
    window.addEventListener("mousemove", onMove);
    window.addEventListener("mouseup", onUp);
    return () => { window.removeEventListener("mousemove", onMove); window.removeEventListener("mouseup", onUp); };
  }, []);

  return (
    <section ref={sectionRef} style={{
      minHeight: "88vh", display: "grid", gridTemplateColumns: "1.1fr 1.3fr",
      borderBottom: "1px solid var(--rule)", position: "relative", overflow: "hidden",
    }}>
      <div style={{ padding: "80px 64px", display: "flex", flexDirection: "column", justifyContent: "center", position: "relative", zIndex: 2 }}>
        <div className="mono-hi" style={{ color: "var(--accent)", marginBottom: 28 }}>
          LLMINDMAP · v2.4 · {lang === "ja" ? "2026年春" : "SPRING 2026"}
        </div>
        <h1 className="display">
          {lang === "ja" ? (
            <><span>そのチャット、</span><em style={{ fontStyle: "italic" }}>構造</em><span>見えてる？</span></>
          ) : (
            <><span>Chats, </span><em style={{ fontStyle: "italic" }}>visualized</em>.</>
          )}
        </h1>
        <p className="lede" style={{ marginTop: 32, maxWidth: 440 }}>
          {lang === "ja"
            ? "ChatGPT、Claude、Geminiの会話を、編集可能なマインドマップへ。時間を切り離し、構造で考える。"
            : "LLMindMap turns ChatGPT, Claude, and Gemini conversations into editable mindmaps. Cut loose from time. Think in structure."}
        </p>
        <div style={{ display: "flex", gap: 12, marginTop: 40, flexWrap: "wrap" }}>
          <a className="btn btn-accent" href="https://chromewebstore.google.com/detail/llmindmap/jlaffgbaoefnmgfkagopepnljgpcfepl" target="_blank" rel="noopener">{lang === "ja" ? "思考を、地図にする。" : "Map your thinking"}</a>
          <a className="btn btn-ghost" href="#how">{lang === "ja" ? "しくみを見る" : "See how it works"}</a>
        </div>
        <div style={{ marginTop: 48, display: "flex", gap: 24 }}>
          {["ChatGPT", "Claude", "Gemini"].map(p => (
            <div key={p} className="mono-hi">{p} ✓</div>
          ))}
        </div>
      </div>
      <div style={{
        display: "flex", alignItems: "center", justifyContent: "center",
        padding: "48px 0", overflow: "visible", position: "relative",
      }}>
        <div
          onMouseDown={onDown}
          style={{
            perspective: 1200,
            transform: `translateX(${-100 + pos.x}px) translateY(${pos.y}px)`,
            cursor: dragging ? "grabbing" : "grab",
            userSelect: "none",
            transition: dragging ? "none" : "transform 0.4s cubic-bezier(0.2, 0.8, 0.2, 1)",
          }}>
          <div className="hero-mock" style={{
            animation: dragging ? "none" : "heroFloat 6s ease-in-out infinite",
            transform: dragging ? "rotateY(-8deg) rotateX(3deg)" : "none",
            transformStyle: "preserve-3d",
          }}>
            <ChatPlusExtensionWindow lang={lang} interactive={true} autoPlay={true} fast={true}/>
          </div>
        </div>
      </div>
    </section>
  );
};

/* ─── Hero B — Centered display over live map ─────────────────────────── */
const HeroB = ({ lang }) => (
  <section style={{ position: "relative", minHeight: "92vh", overflow: "hidden" }}>
    <div style={{ position: "absolute", inset: 0, opacity: 0.45 }}>
      <HeroMindmap width={1400} height={900}/>
    </div>
    <div style={{
      position: "absolute", inset: 0,
      background: "radial-gradient(ellipse at center, transparent 0%, var(--bg) 75%)",
    }}/>
    <div className="container" style={{
      position: "relative", minHeight: "92vh", display: "flex", flexDirection: "column",
      justifyContent: "center", alignItems: "center", textAlign: "center", padding: "80px 32px",
    }}>
      <div className="mono-hi" style={{ color: "var(--accent)", marginBottom: 24 }}>
        {lang === "ja" ? "思考のブラウザ拡張" : "A BROWSER EXTENSION FOR THINKERS"}
      </div>
      <h1 className="display" style={{ maxWidth: 980 }}>
        {lang === "ja"
          ? <><em style={{ fontStyle: "italic" }}>思考</em>は、<br/>線で<em style={{ fontStyle: "italic" }}>流れない</em>。</>
          : <>Thought<br/>doesn't move in <em style={{ fontStyle: "italic" }}>lines</em>.</>}
      </h1>
      <p className="lede" style={{ marginTop: 32, maxWidth: 640 }}>
        {lang === "ja"
          ? "LLMとの会話を、編集可能なマインドマップへ。分岐して、統合して、戻って、超える。"
          : "Turn any LLM conversation into an editable map. Branch. Merge. Return. Surpass."}
      </p>
      <div style={{ display: "flex", gap: 12, marginTop: 40 }}>
        <a className="btn btn-accent" href="https://chromewebstore.google.com/detail/llmindmap/jlaffgbaoefnmgfkagopepnljgpcfepl" target="_blank" rel="noopener">{lang === "ja" ? "思考を、地図にする。" : "Map your thinking"}</a>
        <a className="btn btn-ghost" href="#demo">{lang === "ja" ? "実演を見る" : "Watch the demo"}</a>
      </div>
    </div>
  </section>
);

/* ─── Hero C — Editorial, all type ─────────────────────────────────────── */
const HeroC = ({ lang }) => (
  <section style={{ padding: "120px 0 80px" }}>
    <div className="container">
      <div className="mono-hi" style={{ marginBottom: 64, color: "var(--ink-40)" }}>
        N° 01 — {lang === "ja" ? "序章" : "MANIFESTO"}
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "3fr 1fr", gap: 48, alignItems: "end" }}>
        <h1 className="display" style={{ fontSize: "clamp(64px, 10vw, 148px)" }}>
          {lang === "ja" ? (
            <><em style={{ fontStyle: "italic" }}>追跡せよ</em>。<br/>編集せよ。<br/>そして、<br/><em style={{ fontStyle: "italic" }}>超えろ</em>。</>
          ) : (
            <>Trace.<br/>Edit.<br/><em style={{ fontStyle: "italic" }}>Surpass</em>.</>
          )}
        </h1>
        <div>
          <div className="mono-hi" style={{ color: "var(--accent)", marginBottom: 18 }}>N° 02</div>
          <p className="body" style={{ color: "var(--ink)" }}>
            {lang === "ja"
              ? "あなたのLLM会話を、マインドマップへ。LLMindMapは、時系列の鎖を断ち、思考を構造として扱うためのブラウザ拡張です。"
              : "LLMindMap is a browser extension that turns your LLM chats into editable mindmaps — so you can think in structure, not just in sequence."}
          </p>
          <div style={{ display: "flex", gap: 10, marginTop: 28, flexWrap: "wrap" }}>
            <a className="btn btn-accent btn-sm" href="https://chromewebstore.google.com/detail/llmindmap/jlaffgbaoefnmgfkagopepnljgpcfepl" target="_blank" rel="noopener">{lang === "ja" ? "地図にする" : "Start mapping"}</a>
            <a className="btn btn-ghost btn-sm" href="#how">{lang === "ja" ? "読む" : "Read more"}</a>
          </div>
        </div>
      </div>
    </div>
    <div style={{
      marginTop: 96, padding: "48px 0", borderTop: "1px solid var(--rule)", borderBottom: "1px solid var(--rule)",
    }}>
      <div className="container" style={{ display: "flex", alignItems: "center", justifyContent: "space-between", gap: 48 }}>
        <span className="mono-hi">ChatGPT</span>
        <span className="mono-hi">·</span>
        <span className="mono-hi">Claude</span>
        <span className="mono-hi">·</span>
        <span className="mono-hi">Gemini</span>
        <span className="mono-hi">·</span>
        <span className="mono-hi">{lang === "ja" ? "日英対応" : "JA · EN"}</span>
        <span className="mono-hi">·</span>
        <span className="mono-hi">{lang === "ja" ? "ローカル保存" : "LOCAL-FIRST"}</span>
      </div>
    </div>
  </section>
);

Object.assign(window, { HeroA, HeroB, HeroC });
