/* global React, ReactDOM, COPY, Hero, Products, Brand, ContentStrip, Logo */
const { useState, useEffect } = React;

const INSTAGRAM_URL = "https://instagram.com/drommecookies";

/* ── NAV ─────────────────────────────────────────────────────── */
/* Logo is fixed-positioned and morphs from hero centerpiece (large, centered)
   to nav corner (small, top-left) based on scroll. */
function Nav({ t }) {
  const [vw, setVw] = useState(() =>
    typeof window !== "undefined" ? window.innerWidth : 1200
  );
  const [progress, setProgress] = useState(0);

  useEffect(() => {
    let ticking = false;
    const onScroll = () => {
      if (ticking) return;
      ticking = true;
      requestAnimationFrame(() => {
        const threshold = Math.min(window.innerHeight * 0.5, 480);
        const p = Math.min(1, Math.max(0, window.scrollY / threshold));
        setProgress(p);
        ticking = false;
      });
    };
    const onResize = () => setVw(window.innerWidth);
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
    window.addEventListener("resize", onResize, { passive: true });
    return () => {
      window.removeEventListener("scroll", onScroll);
      window.removeEventListener("resize", onResize);
    };
  }, []);

  // Smooth easeInOutCubic — softer feel than linear
  const ease = (t) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2);
  const p = ease(progress);

  // Size interpolation
  const startSize = Math.min(248, Math.max(132, vw * 0.18));
  const endSize = 56;
  const logoSize = startSize + (endSize - startSize) * p;

  // Center-point interpolation — keeps the visual trajectory clean as size shrinks
  const isNarrow = vw < 768;
  const startTop = isNarrow ? 96 : 120;
  const endTop = (64 - endSize) / 2;
  const startCenterY = startTop + startSize / 2;
  const endCenterY = endTop + endSize / 2;
  const centerY = startCenterY + (endCenterY - startCenterY) * p;
  const top = centerY - logoSize / 2;

  const startCenterX = vw / 2;
  const endCenterX = (isNarrow ? 18 : 28) + endSize / 2;
  const centerX = startCenterX + (endCenterX - startCenterX) * p;
  const left = centerX - logoSize / 2;

  return (
    <>
      <header style={{
        position: "fixed", top: 0, left: 0, right: 0,
        zIndex: 50,
        height: 64,
        display: "flex", alignItems: "center", justifyContent: "flex-end",
        padding: "0 clamp(20px, 4vw, 64px)",
        background: `rgba(246,241,234,${0.92 * p})`,
        backdropFilter: p > 0.15 ? "blur(14px)" : "none",
        WebkitBackdropFilter: p > 0.15 ? "blur(14px)" : "none",
        borderBottom: p > 0.85 ? "1px solid var(--rule)" : "1px solid transparent",
        transition: "border-color 0.3s ease",
      }}>
        <div style={{
          display: "flex", alignItems: "center", gap: 24,
          opacity: progress,
          pointerEvents: progress < 0.4 ? "none" : "auto",
          transition: "opacity 0.18s ease",
        }}>
          <a
            href={INSTAGRAM_URL}
            target="_blank"
            rel="noopener noreferrer"
            style={{
              fontFamily: "var(--sans)", fontSize: 10,
              fontWeight: 600, letterSpacing: "0.12em",
              textTransform: "uppercase", textDecoration: "none",
              padding: "9px 22px",
              border: "1px solid var(--rule-strong)",
              color: "var(--chocolate)",
              transition: "background 0.3s ease, color 0.3s ease",
              background: "transparent",
            }}
            onMouseEnter={e => {
              e.currentTarget.style.background = "var(--chocolate)";
              e.currentTarget.style.color = "var(--paper)";
            }}
            onMouseLeave={e => {
              e.currentTarget.style.background = "transparent";
              e.currentTarget.style.color = "var(--chocolate)";
            }}
          >
            {t.nav.order}
          </a>
        </div>
      </header>

      {/* Morphing logo: hero centerpiece → nav corner */}
      <a
        href="/"
        aria-label="Drømme Cookies"
        style={{
          position: "fixed",
          top, left,
          width: logoSize, height: logoSize,
          zIndex: 60,
          color: "var(--chocolate)",
          lineHeight: 0,
          display: "block",
        }}
      >
        <Logo size={logoSize} uid="nav" />
      </a>
    </>
  );
}

/* ── FOOTER ──────────────────────────────────────────────────── */
function Footer({ t }) {
  const links = [
    { label: t.footer.order,     href: INSTAGRAM_URL,         external: true },
    { label: t.footer.instagram, href: INSTAGRAM_URL,         external: true },
    { label: t.footer.contact,   href: "mailto:hei@drommecookies.no", external: false },
    { label: t.footer.tiktok,    href: "#",                   external: false },
  ];

  return (
    <footer style={{
      background: "var(--espresso)",
      color: "var(--paper)",
      padding: "clamp(72px, 12vw, 160px) clamp(20px, 4vw, 64px) clamp(32px, 4.5vw, 56px)",
    }}>
      <div style={{
        display: "flex",
        justifyContent: "space-between",
        alignItems: "flex-start",
        flexWrap: "wrap",
        gap: "clamp(32px, 6vw, 64px)",
        marginBottom: "clamp(48px, 8vw, 96px)",
      }}>
        <Logo size={168} variant="light" />

        <nav style={{
          display: "grid",
          gridTemplateColumns: "1fr 1fr",
          columnGap: 40,
          rowGap: 16,
          paddingTop: 6,
        }}>
          {links.map(({ label, href, external }, i) => (
            <a
              key={i}
              href={href}
              target={external ? "_blank" : undefined}
              rel={external ? "noopener noreferrer" : undefined}
              style={{
                fontFamily: "var(--sans)", fontSize: 14,
                color: "rgba(250,248,244,0.44)",
                textDecoration: "none",
                transition: "color 0.28s ease",
              }}
              onMouseEnter={e => { e.currentTarget.style.color = "var(--paper)"; }}
              onMouseLeave={e => { e.currentTarget.style.color = "rgba(250,248,244,0.44)"; }}
            >
              {label}
            </a>
          ))}
        </nav>
      </div>

      <div style={{
        display: "flex",
        justifyContent: "space-between",
        flexWrap: "wrap",
        gap: 12,
        paddingTop: 20,
        borderTop: "1px solid rgba(250,248,244,0.08)",
        fontFamily: "var(--mono)", fontSize: 9,
        letterSpacing: "0.22em", textTransform: "uppercase",
        color: "rgba(250,248,244,0.2)",
      }}>
        <span>{t.footer.copy}</span>
        <span>{t.footer.location}</span>
      </div>
    </footer>
  );
}

/* ── APP ─────────────────────────────────────────────────────── */
function App() {
  const lang = "no";
  const t = COPY[lang];

  useEffect(() => { document.documentElement.lang = lang; }, [lang]);

  return (
    <>
      <Nav t={t} />
      <main>
        <Hero t={t} />
        <Products t={t} />
        <Brand t={t} />
        <ContentStrip t={t} />
      </main>
      <Footer t={t} />
    </>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
