// Low-level primitives shared across views.

const { useState, useEffect, useRef, useMemo, useCallback } = React;

// Alt / Option (⌥) glyph — flat, minimal stroke drawing.
function AltGlyph({ size = 40, color = "currentColor", withWordmark = false }) {
  const s = size;
  return (
    <svg width={s} height={s} viewBox="0 0 40 40" fill="none" style={{ display: "block" }}>
      {/* ⌥ symbol — top-right bracket segment + bottom-left rising stroke */}
      <path d="M8 14 H 16.5 L 26 28 H 33" stroke={color} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/>
      <path d="M24 14 H 33" stroke={color} strokeWidth="1.6" strokeLinecap="round"/>
      {withWordmark && (
        <text x="36.5" y="37" fontSize="6.5" fill={color} textAnchor="end" fontFamily="inherit" fontWeight="500" opacity="0.82">alt</text>
      )}
    </svg>
  );
}

// Small inline Alt glyph for use in body copy
function AltGlyphInline({ size = 14, color = "currentColor" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 40 40" fill="none" style={{ display: "inline-block", verticalAlign: "-2px" }}>
      <path d="M8 14 H 16.5 L 26 28 H 33" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
      <path d="M24 14 H 33" stroke={color} strokeWidth="2" strokeLinecap="round"/>
    </svg>
  );
}

// Keycap — single key rendered as a flat, minimal keycap. Label can be
// a string or a ReactNode (use <AltGlyph/> for the Alt key).
function Keycap({ label, width = 52, height = 52, pressed = false, soft = false, style = {} }) {
  const isGlyph = typeof label !== "string";
  return (
    <div
      style={{
        width,
        height,
        borderRadius: Math.round(width * 0.18),
        position: "relative",
        display: "inline-flex",
        alignItems: "center",
        justifyContent: "center",
        transition: "background 120ms ease, border-color 120ms ease, transform 120ms ease, box-shadow 120ms ease",
        transform: pressed ? "translateY(1px)" : "translateY(0)",
        background: pressed
          ? "rgba(0,0,0,0.05)"
          : soft
          ? "#ffffff"
          : "#ffffff",
        border: `1px solid ${pressed ? "rgba(0,0,0,0.18)" : "rgba(0,0,0,0.12)"}`,
        boxShadow: pressed
          ? "0 1px 0 rgba(0,0,0,0.04)"
          : "0 2px 0 rgba(0,0,0,0.08)",
        color: "var(--ink)",
        ...style,
      }}
    >
      {isGlyph ? (
        <span style={{ opacity: pressed ? 0.7 : 1, display: "inline-flex" }}>{label}</span>
      ) : (
        <span
          style={{
            fontSize: Math.round(height * 0.26),
            fontWeight: 500,
            letterSpacing: "0.02em",
            lineHeight: 1,
            color: "var(--ink)",
            opacity: pressed ? 0.7 : 0.9,
          }}
        >
          {label}
        </span>
      )}
    </div>
  );
}

// Pill — reusable button
function Pill({ children, onClick, active, icon, compact, title }) {
  return (
    <button
      title={title}
      onClick={onClick}
      style={{
        height: compact ? 26 : 30,
        padding: compact ? "0 10px" : "0 12px",
        borderRadius: 999,
        border: "1px solid var(--line)",
        background: active ? "var(--ink)" : "#fff",
        color: active ? "#fff" : "var(--ink)",
        display: "inline-flex",
        alignItems: "center",
        gap: 6,
        fontSize: 12.5,
        fontWeight: 500,
        cursor: "pointer",
        transition: "all 120ms ease",
        boxShadow: active ? "none" : "0 1px 0 rgba(2,2,2,.02)",
      }}
      onMouseEnter={(e) => {
        if (!active) e.currentTarget.style.background = "#fafafa";
      }}
      onMouseLeave={(e) => {
        if (!active) e.currentTarget.style.background = "#fff";
      }}
    >
      {icon}
      {children}
    </button>
  );
}

// IconButton — square-ish ghost button
function IconButton({ icon, onClick, title, size = 28, active }) {
  const [hover, setHover] = useState(false);
  return (
    <button
      onClick={onClick}
      title={title}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      style={{
        width: size,
        height: size,
        borderRadius: 7,
        border: "none",
        background: active ? "rgba(2,2,2,.06)" : hover ? "rgba(2,2,2,.04)" : "transparent",
        color: active ? "var(--ink)" : "var(--ink-soft)",
        display: "inline-flex",
        alignItems: "center",
        justifyContent: "center",
        cursor: "pointer",
        transition: "background 120ms ease, color 120ms ease",
      }}
    >
      {icon}
    </button>
  );
}

// Field — labeled input row (right panel style)
function Field({ label, children, hint }) {
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 4 }}>
      {label && (
        <div
          style={{
            fontSize: 11.5,
            color: "var(--ink-mute)",
            fontWeight: 500,
            letterSpacing: "0.005em",
          }}
        >
          {label}
        </div>
      )}
      {children}
      {hint && (
        <div style={{ fontSize: 11, color: "var(--ink-faint)", marginTop: 2 }}>{hint}</div>
      )}
    </div>
  );
}

// Input shell — matches the screenshot's rounded rectangles
function InputShell({ children, style = {}, onClick }) {
  return (
    <div
      onClick={onClick}
      style={{
        height: 32,
        borderRadius: 8,
        border: "1px solid var(--line)",
        background: "#fff",
        padding: "0 10px",
        display: "flex",
        alignItems: "center",
        gap: 8,
        fontSize: 12.5,
        color: "var(--ink)",
        cursor: onClick ? "pointer" : "text",
        transition: "border 120ms ease, background 120ms ease",
        ...style,
      }}
    >
      {children}
    </div>
  );
}

// Switch
function Toggle({ on, onChange, size = "md" }) {
  const w = size === "sm" ? 30 : 36;
  const h = size === "sm" ? 18 : 20;
  const knob = h - 4;
  return (
    <button
      onClick={() => onChange(!on)}
      style={{
        width: w,
        height: h,
        borderRadius: 999,
        border: "none",
        padding: 0,
        background: on ? "var(--ink)" : "#e3e3e3",
        position: "relative",
        cursor: "pointer",
        transition: "background 160ms ease",
      }}
    >
      <span
        style={{
          position: "absolute",
          top: 2,
          left: on ? w - knob - 2 : 2,
          width: knob,
          height: knob,
          borderRadius: 999,
          background: "#fff",
          transition: "left 180ms cubic-bezier(.2,.8,.3,1.2)",
          boxShadow: "0 1px 2px rgba(2,2,2,.2)",
        }}
      />
    </button>
  );
}

// Segmented — pill-inside-shell, two options
function Segmented({ options, value, onChange }) {
  return (
    <div
      style={{
        display: "flex",
        background: "#efefef",
        borderRadius: 8,
        padding: 2,
        gap: 2,
      }}
    >
      {options.map((o) => {
        const active = o.value === value;
        return (
          <button
            key={o.value}
            onClick={() => onChange(o.value)}
            style={{
              flex: 1,
              height: 26,
              padding: "0 10px",
              borderRadius: 6,
              border: "none",
              background: active ? "#fff" : "transparent",
              color: "var(--ink)",
              fontSize: 12,
              fontWeight: active ? 500 : 400,
              cursor: "pointer",
              display: "inline-flex",
              alignItems: "center",
              justifyContent: "center",
              gap: 6,
              boxShadow: active ? "0 1px 2px rgba(2,2,2,.06), 0 0 0 1px rgba(2,2,2,.04)" : "none",
              transition: "all 140ms ease",
            }}
          >
            {o.icon}
            {o.label}
          </button>
        );
      })}
    </div>
  );
}

// Tabs — underline
function Tabs({ tabs, value, onChange }) {
  return (
    <div
      style={{
        display: "flex",
        gap: 16,
        borderBottom: "1px solid var(--line)",
        padding: "0 0",
      }}
    >
      {tabs.map((t) => {
        const active = t.value === value;
        return (
          <button
            key={t.value}
            onClick={() => onChange(t.value)}
            style={{
              border: "none",
              background: "transparent",
              padding: "10px 0",
              fontSize: 13,
              fontWeight: active ? 500 : 400,
              color: active ? "var(--ink)" : "var(--ink-mute)",
              cursor: "pointer",
              position: "relative",
            }}
          >
            {t.label}
            <span
              style={{
                position: "absolute",
                left: 0,
                right: 0,
                bottom: -1,
                height: 1.5,
                background: active ? "var(--ink)" : "transparent",
                transition: "background 140ms ease",
              }}
            />
          </button>
        );
      })}
    </div>
  );
}

Object.assign(window, { Keycap, AltGlyph, AltGlyphInline, Pill, IconButton, Field, InputShell, Toggle, Segmented, Tabs });
