/* @jsx React.createElement */
const { useState: useStateHdr, useEffect: useEffectHdr } = React;

const HDR_WA_URL = "https://chat.whatsapp.com/E1iQGAiwPYXBiyfRFrexgg";
const HDR_QR_URL = `https://api.qrserver.com/v1/create-qr-code/?size=160x160&margin=4&color=111111&bgcolor=FAF8F6&data=${encodeURIComponent(HDR_WA_URL)}`;

function Header({ page, go: appGo }) {
  const [open, setOpen] = useStateHdr(false);
  const [scrolled, setScrolled] = useStateHdr(false);

  useEffectHdr(() => {
    const onScroll = () => setScrolled(window.scrollY > 12);
    window.addEventListener("scroll", onScroll);
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  // Close the burger menu when the user clicks outside the header or hits Escape.
  // Also flag <body> while open so the mobile sticky CTA can hide itself
  // (otherwise it overlaps the lower menu items on small screens).
  useEffectHdr(() => {
    if (open) document.body.classList.add("os-menu-open");
    else document.body.classList.remove("os-menu-open");

    if (!open) return;
    const onDocClick = (e) => {
      const headerEl = document.querySelector("header");
      if (headerEl && !headerEl.contains(e.target)) setOpen(false);
    };
    const onKey = (e) => { if (e.key === "Escape") setOpen(false); };
    // Defer click listener so the click that opened the menu does not immediately close it
    const id = setTimeout(() => document.addEventListener("click", onDocClick), 0);
    document.addEventListener("keydown", onKey);
    return () => {
      clearTimeout(id);
      document.removeEventListener("click", onDocClick);
      document.removeEventListener("keydown", onKey);
    };
  }, [open]);

  const items = [
    { id: "home", label: "Startseite" },
    { id: "weg", label: "Mein Weg" },
    { id: "hund", label: "Meine Hunde" },
    { id: "buchung", label: "Buchungen und Angebote" },
    { id: "tierheime", label: "Tierheime" },
    { id: "anfragen", label: "Anfragen" },
    { id: "shop", label: "Shop" },
    { id: "berichte", label: "Erfahrungsberichte" },
  ];

  // Use App's go (View Transition + scroll-to-top), then close the burger menu.
  const go = (id) => { appGo(id); setOpen(false); };

  return (
    <header style={{
      position: "sticky", top: 0, zIndex: 50,
      background: scrolled ? "rgba(255,255,255,0.96)" : "var(--os-paper)",
      borderBottom: "1px solid var(--border-1)",
      transition: "background 220ms var(--ease-standard)",
    }}>
      <div style={{ maxWidth: 1240, margin: "0 auto", padding: "9px 32px", display: "flex", alignItems: "center", justifyContent: "space-between", gap: 32 }}>
        <a href="#" onClick={(e) => { e.preventDefault(); go("home"); }} style={{ display: "flex", alignItems: "center", gap: 14, textDecoration: "none" }}>
          <img src="assets/logo-mark.png" alt="Logo Orientierungsstark" className="os-header-logo" style={{ width: 86, height: "auto", display: "block" }} />
          <span className="os-header-wordmark" style={{ display: "flex", flexDirection: "column" }}>
            <span style={{ fontFamily: "var(--font-sans)", fontWeight: 300, fontSize: 22, lineHeight: 1, color: "var(--os-warmgray-7)", letterSpacing: "-0.01em" }}>
              Orientierungs<span style={{ fontWeight: 700, color: "var(--os-red-485)" }}>stark</span>
            </span>
            <span style={{ fontSize: 10, letterSpacing: "0.18em", color: "var(--fg-3)", marginTop: 6, fontWeight: 600, fontFamily: "var(--font-sans)" }}>HUNDEVERHALTEN · THURGAU</span>
          </span>
        </a>

        <nav style={{ display: "flex", gap: 16, alignItems: "center" }}>
          <a href="tel:+41798436183" className="os-header-tel" style={{
            display: "inline-flex", alignItems: "center", gap: 8,
            fontFamily: "var(--font-sans)", fontWeight: 600, fontSize: 13,
            color: "var(--os-black)", textDecoration: "none",
            padding: "10px 6px", letterSpacing: "0.01em",
          }}>
            <i data-lucide="phone" style={{ width: 14, height: 14, color: "var(--os-red-485)" }}></i>
            079 / 843 61 83
          </a>
          <a href="https://www.instagram.com/orientierungsstark" target="_blank" rel="noopener" className="os-header-ig" aria-label="Orientierungsstark auf Instagram" style={{
            display: "inline-flex", alignItems: "center", gap: 10,
            textDecoration: "none", padding: "8px 14px",
            background: "var(--os-paper)", border: "1px solid var(--border-1)",
            borderRadius: 2, height: 48,
          }}>
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="var(--os-red-485)" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true" style={{ display: "block" }}>
              <rect x="2" y="2" width="20" height="20" rx="5" ry="5" />
              <path d="M16 11.37A4 4 0 1 1 12.63 8 4 4 0 0 1 16 11.37z" />
              <line x1="17.5" y1="6.5" x2="17.51" y2="6.5" />
            </svg>
            <span style={{
              fontFamily: "var(--font-sans)", fontWeight: 600, fontSize: 11,
              letterSpacing: "0.16em", textTransform: "uppercase",
              color: "var(--os-black)",
            }}>Instagram</span>
          </a>
          <a href={HDR_WA_URL} target="_blank" rel="noopener" className="os-header-wa" aria-label="WhatsApp-Gruppe öffnen" style={{
            display: "inline-flex", alignItems: "center", gap: 12,
            textDecoration: "none", padding: "4px 14px 4px 4px",
            background: "var(--os-paper)", border: "1px solid var(--border-1)",
            borderRadius: 2,
          }}>
            <img src={HDR_QR_URL} alt="QR-Code WhatsApp-Gruppe" width="40" height="40" style={{ display: "block", width: 40, height: 40 }} />
            <span style={{
              fontFamily: "var(--font-sans)", fontWeight: 600, fontSize: 11,
              letterSpacing: "0.16em", textTransform: "uppercase",
              color: "var(--os-black)", lineHeight: 1.3,
            }}>WhatsApp<br />Gruppe</span>
          </a>
          <button onClick={() => setOpen(o => !o)} aria-label="Menü" aria-expanded={open} className="os-cta-outline" style={{
            background: "transparent", border: "2px solid var(--os-black)", padding: "8px 12px",
            cursor: "pointer", color: "var(--os-black)", borderRadius: 2,
            display: "flex", alignItems: "center", gap: 8,
            fontFamily: "var(--font-sans)", fontWeight: 600, fontSize: 12, letterSpacing: "0.18em", textTransform: "uppercase",
          }}>
            <span className={"os-icon-burger" + (open ? " is-open" : "")} aria-hidden="true">
              <span className="bar" />
              <span className="bar" />
              <span className="bar" />
            </span>
            {open ? "Schliessen" : "Menü"}
          </button>
        </nav>
      </div>

      <div className={"os-drawer" + (open ? " is-open" : "")} style={{ background: "var(--os-black)", color: "var(--os-white)", borderTop: open ? "1px solid rgba(255,255,255,0.08)" : "none" }}>
        <div style={{ maxWidth: 1240, margin: "0 auto", padding: "40px 32px 48px", display: "grid", gridTemplateColumns: "2fr 1fr", gap: 64 }}>
          <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: "0 48px" }}>
            {items.map((it, i) => (
              <a key={it.id} href="#" tabIndex={open ? 0 : -1} className="os-drawer-item" onClick={(e) => { e.preventDefault(); go(it.id); }} style={{
                color: page === it.id ? "var(--os-red-485)" : "var(--os-white)", textDecoration: "none",
                fontFamily: "var(--font-sans)", fontWeight: 300, fontSize: 24,
                padding: "14px 0", borderBottom: "1px solid rgba(255,255,255,0.08)",
                display: "flex", alignItems: "baseline", gap: 14,
              }}>
                <span style={{ fontFamily: "var(--font-sans)", fontWeight: 600, fontSize: 11, letterSpacing: "0.18em", color: "rgba(255,255,255,0.4)", minWidth: 24 }}>0{i+1}</span>
                {it.label}
              </a>
            ))}
          </div>
          <div style={{ borderLeft: "1px solid rgba(255,255,255,0.12)", paddingLeft: 32 }}>
            <div style={{ fontFamily: "var(--font-sans)", fontWeight: 600, fontSize: 11, letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--os-red-485)", marginBottom: 14 }}>Direkt zu mir</div>
            <div style={{ fontFamily: "var(--font-sans)", fontSize: 14, lineHeight: 2, color: "rgba(255,255,255,0.78)" }}>
              <div style={{ display: "flex", alignItems: "center", gap: 10 }}><i data-lucide="phone" style={{ width: 14, height: 14, color: "var(--os-red-485)" }}></i>079 / 843 61 83</div>
              <div style={{ display: "flex", alignItems: "center", gap: 10 }}><i data-lucide="mail" style={{ width: 14, height: 14, color: "var(--os-red-485)" }}></i>c.p.hundeschule@gmail.com</div>
              <div style={{ display: "flex", alignItems: "center", gap: 10 }}><i data-lucide="map-pin" style={{ width: 14, height: 14, color: "var(--os-red-485)" }}></i>Widestrasse 3, 8580 Hefenhofen</div>
              <a href="https://chat.whatsapp.com/E1iQGAiwPYXBiyfRFrexgg" target="_blank" rel="noopener" tabIndex={open ? 0 : -1} style={{ display: "flex", alignItems: "center", gap: 10, color: "rgba(255,255,255,0.78)", textDecoration: "none" }}><i data-lucide="message-circle" style={{ width: 14, height: 14, color: "var(--os-red-485)" }}></i>WhatsApp-Gruppe beitreten</a>
              <a href="https://www.instagram.com/orientierungsstark" target="_blank" rel="noopener" tabIndex={open ? 0 : -1} style={{ display: "flex", alignItems: "center", gap: 10, color: "rgba(255,255,255,0.78)", textDecoration: "none" }}>
                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="var(--os-red-485)" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
                  <rect x="2" y="2" width="20" height="20" rx="5" ry="5" />
                  <path d="M16 11.37A4 4 0 1 1 12.63 8 4 4 0 0 1 16 11.37z" />
                  <line x1="17.5" y1="6.5" x2="17.51" y2="6.5" />
                </svg>
                Instagram
              </a>
            </div>
          </div>
        </div>
      </div>
    </header>
  );
}

window.Header = Header;
