/* global React */
const { useState: pUseState, useEffect: pUseEffect } = React;

// ─── Wordmark ───────────────────────────────────────────
function Wordmark({ size = 28, color }) {
  return (
    <span style={{
      fontFamily: "var(--kp-display)",
      fontSize: size, lineHeight: 1, letterSpacing: "-0.015em",
      color: color || "var(--kp-ink)", fontWeight: 400,
      display: "inline-flex", alignItems: "baseline",
    }}>
      <span>Kaleidos</span>
      <em style={{ fontStyle: "italic", margin: "0 4px", color: "var(--kp-pink)" }}>·</em>
      <span>Pay</span>
    </span>
  );
}

// ─── SectionHead ────────────────────────────────────────
function SectionHead({ num, sub, tag, children }) {
  return (
    <div style={{
      display: "grid",
      gridTemplateColumns: "auto 1fr auto",
      alignItems: "end",
      gap: "24px 36px",
      marginBottom: 56,
    }}>
      <div style={{
        fontFamily: "var(--kp-display)", fontSize: 64, lineHeight: 0.85,
        color: "var(--kp-pink)", fontStyle: "italic", fontWeight: 400,
      }}>
        {num}
        <span style={{
          display: "block", fontFamily: "var(--kp-mono)", fontSize: 9.5,
          letterSpacing: "0.2em", textTransform: "uppercase",
          color: "var(--kp-muted)", fontStyle: "normal", marginTop: 6, lineHeight: 1,
        }}>{sub}</span>
      </div>
      <h2 className="kp-display" style={{
        fontSize: "clamp(30px, 4.4vw, 54px)", lineHeight: 1.02,
        letterSpacing: "-0.02em", fontWeight: 400, maxWidth: 820, margin: 0,
      }}>{children}</h2>
      {tag && (
        <span style={{
          justifySelf: "end", alignSelf: "start", whiteSpace: "nowrap",
          fontFamily: "var(--kp-mono)", fontSize: 9, letterSpacing: "0.2em",
          textTransform: "uppercase", padding: "5px 11px",
          border: "1.5px solid var(--kp-ink)", background: "var(--kp-white)",
          boxShadow: "2px 2px 0 0 var(--kp-ink)",
        }}>{tag}</span>
      )}
    </div>
  );
}

// ─── Temperature pill ───────────────────────────────────
function TempPill({ temp }) {
  const map = {
    fervendo: { cls: "tempo-hot", label: "🔥 Fervendo" },
    quente:   { cls: "tempo-hot", label: "🔥 Quente" },
    morno:    { cls: "tempo-warm", label: "Morno" },
    frio:     { cls: "tempo-cold", label: "Frio" },
    congelado:{ cls: "tempo-frozen", label: "Sem sinal" },
  };
  const m = map[temp] || map.frio;
  return <span className={"tempo-hot " + m.cls}>{m.label}</span>;
}

// ─── Status chip ────────────────────────────────────────
function StatusChip({ status }) {
  const map = {
    aceita:    { cls: "status-chip--accept", label: "Aceita" },
    rejeitada: { cls: "status-chip--reject", label: "Recusada" },
    viewing:   { cls: "status-chip--sent", label: "Lendo agora" },
    sent:      { cls: "status-chip--pending", label: "Aguardando" },
    expirada:  { cls: "status-chip--pending", label: "Expirada" },
  };
  const m = map[status] || map.sent;
  return <span className={"status-chip " + m.cls}>{m.label}</span>;
}

// ─── Format helpers ─────────────────────────────────────
function brl(n) {
  return n.toLocaleString("pt-BR", { style: "currency", currency: "BRL", minimumFractionDigits: 2 });
}
function fmtDur(seg) {
  const m = Math.floor(seg / 60);
  const s = seg % 60;
  if (m === 0) return `${s}s`;
  return `${m}min ${String(s).padStart(2, "0")}s`;
}

// ─── Avatar ─────────────────────────────────────────────
function Avatar({ initials, size = 44, bg = "var(--kp-soft)" }) {
  return (
    <div style={{
      width: size, height: size,
      background: bg,
      border: "1.5px solid var(--kp-ink)",
      display: "inline-flex", alignItems: "center", justifyContent: "center",
      fontFamily: "var(--kp-display)", fontSize: size * 0.42, fontStyle: "italic",
      color: "var(--kp-ink)", flexShrink: 0,
    }}>{initials}</div>
  );
}

// ─── Tiny SVG icons ─────────────────────────────────────
const Icon = {
  eye: (p) => (<svg width={p.s||14} height={p.s||14} viewBox="0 0 16 16" fill="none"><path d="M1 8s2.5-5 7-5 7 5 7 5-2.5 5-7 5-7-5-7-5z" stroke="currentColor" strokeWidth="1.4"/><circle cx="8" cy="8" r="2.2" stroke="currentColor" strokeWidth="1.4"/></svg>),
  clock: (p) => (<svg width={p.s||14} height={p.s||14} viewBox="0 0 16 16" fill="none"><circle cx="8" cy="8" r="6.5" stroke="currentColor" strokeWidth="1.4"/><path d="M8 4v4l2.5 2" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/></svg>),
  flame: (p) => (<svg width={p.s||14} height={p.s||14} viewBox="0 0 16 16" fill="none"><path d="M8 1s4 3 4 7a4 4 0 0 1-8 0c0-2 1.5-3 1.5-3S6 7 8 6c0-2 0-5 0-5z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round"/></svg>),
  send: (p) => (<svg width={p.s||14} height={p.s||14} viewBox="0 0 16 16" fill="none"><path d="M14.5 1.5L1.5 7 7 9l2 5.5 5.5-13z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round"/><path d="M14.5 1.5L7 9" stroke="currentColor" strokeWidth="1.4"/></svg>),
  check: (p) => (<svg width={p.s||14} height={p.s||14} viewBox="0 0 16 16" fill="none"><path d="M3 8.5l3 3 7-7" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  edit: (p) => (<svg width={p.s||14} height={p.s||14} viewBox="0 0 16 16" fill="none"><path d="M11.5 2.5l2 2-8 8H3.5v-2l8-8z" stroke="currentColor" strokeWidth="1.4" strokeLinejoin="round"/></svg>),
  arrow: (p) => (<svg width={p.s||12} height={p.s||12} viewBox="0 0 16 16" fill="none"><path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  back: (p) => (<svg width={p.s||14} height={p.s||14} viewBox="0 0 16 16" fill="none"><path d="M13 8H3M7 4L3 8l4 4" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/></svg>),
  sparkle: (p) => (<svg width={p.s||14} height={p.s||14} viewBox="0 0 16 16" fill="none"><path d="M8 1l1.5 4.5L14 7l-4.5 1.5L8 13l-1.5-4.5L2 7l4.5-1.5L8 1z" fill="currentColor"/></svg>),
  link: (p) => (<svg width={p.s||14} height={p.s||14} viewBox="0 0 16 16" fill="none"><path d="M6 10a3 3 0 0 0 4 0l2-2a3 3 0 0 0-4-4l-1 1M10 6a3 3 0 0 0-4 0l-2 2a3 3 0 0 0 4 4l1-1" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/></svg>),
  copy: (p) => (<svg width={p.s||14} height={p.s||14} viewBox="0 0 16 16" fill="none"><rect x="4" y="4" width="9" height="9" stroke="currentColor" strokeWidth="1.4"/><path d="M3 11V3h8" stroke="currentColor" strokeWidth="1.4"/></svg>),
  whatsapp: (p) => (<svg width={p.s||14} height={p.s||14} viewBox="0 0 16 16" fill="none"><path d="M8 1.5a6.5 6.5 0 0 0-5.5 9.9L1.5 14.5l3.2-1A6.5 6.5 0 1 0 8 1.5z" stroke="currentColor" strokeWidth="1.4"/><path d="M5.5 5.5c0 2 1.5 4 3.5 4.5L10 9c1 .5 1.5 0 1.5-.5" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/></svg>),
  mail: (p) => (<svg width={p.s||14} height={p.s||14} viewBox="0 0 16 16" fill="none"><rect x="1.5" y="3" width="13" height="10" stroke="currentColor" strokeWidth="1.4"/><path d="M2 4l6 4.5L14 4" stroke="currentColor" strokeWidth="1.4"/></svg>),
};

window.SEQ_UI = { Wordmark, SectionHead, TempPill, StatusChip, Avatar, Icon, brl, fmtDur };
