// ===== Teuanjai Radio — shared UI primitives =====
const { useState, useEffect, useRef, useMemo } = React;

// Simple line icons (stroke). Only geometric primitives — safe to hand-draw.
function Icon({ name, size = 22, stroke = 2 }) {
  const common = {
    width: size, height: size, viewBox: "0 0 24 24", fill: "none",
    stroke: "currentColor", strokeWidth: stroke, strokeLinecap: "round", strokeLinejoin: "round",
  };
  switch (name) {
    case "play":   return <svg {...common} fill="currentColor" stroke="none"><path d="M7 5.5v13a1 1 0 0 0 1.5.87l11-6.5a1 1 0 0 0 0-1.74l-11-6.5A1 1 0 0 0 7 5.5Z"/></svg>;
    case "pause":  return <svg {...common} fill="currentColor" stroke="none"><rect x="6.5" y="5" width="3.6" height="14" rx="1.4"/><rect x="13.9" y="5" width="3.6" height="14" rx="1.4"/></svg>;
    case "prev":   return <svg {...common} fill="currentColor" stroke="none"><rect x="5" y="5" width="2.6" height="14" rx="1.2"/><path d="M19 5.6v12.8a.9.9 0 0 1-1.36.78l-9-6.4a.9.9 0 0 1 0-1.56l9-6.4A.9.9 0 0 1 19 5.6Z"/></svg>;
    case "next":   return <svg {...common} fill="currentColor" stroke="none"><rect x="16.4" y="5" width="2.6" height="14" rx="1.2"/><path d="M5 5.6v12.8a.9.9 0 0 0 1.36.78l9-6.4a.9.9 0 0 0 0-1.56l-9-6.4A.9.9 0 0 0 5 5.6Z"/></svg>;
    case "library":return <svg {...common}><path d="M4 7h10M4 12h10M4 17h7"/><circle cx="18.5" cy="16" r="2.5"/><path d="M21 16V8.5l-2 .6"/></svg>;
    case "clock":  return <svg {...common}><circle cx="12" cy="12" r="8.2"/><path d="M12 7.8V12l2.8 1.8"/></svg>;
    case "logout": return <svg {...common}><path d="M14 7V5.5A1.5 1.5 0 0 0 12.5 4h-6A1.5 1.5 0 0 0 5 5.5v13A1.5 1.5 0 0 0 6.5 20h6a1.5 1.5 0 0 0 1.5-1.5V17"/><path d="M10 12h10m0 0-3-3m3 3-3 3"/></svg>;
    case "chevron-down": return <svg {...common}><path d="m6 9 6 6 6-6"/></svg>;
    case "check":  return <svg {...common}><path d="M5 12.5 10 17l9-10"/></svg>;
    case "user":   return <svg {...common}><circle cx="12" cy="8.5" r="3.5"/><path d="M5.5 19a6.5 6.5 0 0 1 13 0"/></svg>;
    case "wave":   return <svg {...common}><path d="M3 12h2M7 8v8M11 5v14M15 8v8M19 11v2M21 12h.01"/></svg>;
    case "globe":  return <svg {...common}><circle cx="12" cy="12" r="8.5"/><path d="M3.5 12h17M12 3.5c2.4 2.3 2.4 14.7 0 17M12 3.5c-2.4 2.3-2.4 14.7 0 17"/></svg>;
    case "spark":  return <svg {...common} fill="currentColor" stroke="none"><path d="M12 3l1.6 5.1L19 9.7l-4.4 3 .9 5.6L12 15.7 7.5 18.3l.9-5.6-4.4-3 5.4-.6L12 3Z"/></svg>;
    case "dot":    return <svg {...common} fill="currentColor" stroke="none"><circle cx="12" cy="12" r="4"/></svg>;
    case "volume":  return <svg {...common}><path d="M4 9.5v5h3.5L12 18.5V5.5L7.5 9.5H4Z"/><path d="M15.5 9a4 4 0 0 1 0 6"/><path d="M18 6.5a7.5 7.5 0 0 1 0 11"/></svg>;
    case "mute":    return <svg {...common}><path d="M4 9.5v5h3.5L12 18.5V5.5L7.5 9.5H4Z"/><path d="m16 9.5 5 5m0-5-5 5"/></svg>;
    case "sun":     return <svg {...common}><circle cx="12" cy="12" r="4.2" fill="currentColor" stroke="none"/><g strokeWidth={2.1}><path d="M12 2.6v2.4m0 14v2.4M2.6 12H5m14 0h2.4M5.1 5.1l1.7 1.7m10.4 10.4 1.7 1.7M18.9 5.1l-1.7 1.7M6.8 17.2l-1.7 1.7"/></g></svg>;
    case "sunset":  return <svg {...common}><path d="M12 3.2v5.6m0-5.6L9.4 5.8M12 3.2l2.6 2.6" strokeWidth={2.1}/><path d="M5 14a7 7 0 0 1 14 0Z" fill="currentColor" stroke="none"/><path d="M2.8 17.8h18.4M6 21h12" strokeWidth={2.1}/></svg>;
    case "moon":    return <svg {...common}><path d="M20.5 14.2A8.2 8.2 0 0 1 9.6 3.4a6.8 6.8 0 1 0 10.9 10.8Z" fill="currentColor" stroke="none"/><circle cx="17" cy="6.5" r="0.9" fill="rgba(255,255,255,.85)" stroke="none"/><circle cx="19.3" cy="9.6" r="0.6" fill="rgba(255,255,255,.7)" stroke="none"/></svg>;
    default:       return null;
  }
}

// Cover art — pastel gradient w/ soft sheen + tiny EQ glyph
function Cover({ track, size = 56, radius = 16, playing = false }) {
  const g = track ? track.g : ["#9CC2F2", "#A9E6DA"];
  return (
    <div style={{
      width: size, height: size, borderRadius: radius, flex: "0 0 auto",
      background: `linear-gradient(140deg, ${g[0]}, ${g[1]})`,
      boxShadow: "inset 0 1px 2px rgba(255,255,255,.6), 0 6px 16px rgba(80,120,170,.22)",
      position: "relative", overflow: "hidden", display: "grid", placeItems: "center",
    }}>
      <div style={{ position: "absolute", inset: 0, background: "radial-gradient(120% 80% at 30% 0%, rgba(255,255,255,.45), transparent 60%)" }} />
      <EQ playing={playing} bars={4} h={Math.round(size * 0.34)} color="rgba(255,255,255,.92)" gap={Math.max(2, size*0.04)} w={Math.max(2, size*0.05)} />
    </div>
  );
}

// Animated equalizer bars
function EQ({ playing = true, bars = 5, h = 22, w = 3, gap = 3, color = "var(--accent)" }) {
  const delays = [0, 0.25, 0.5, 0.15, 0.4, 0.32, 0.1];
  return (
    <div style={{ display: "flex", alignItems: "flex-end", gap, height: h }}>
      {Array.from({ length: bars }).map((_, i) => (
        <span key={i} style={{
          width: w, height: "100%", borderRadius: w, background: color,
          transformOrigin: "bottom",
          animation: playing ? `eq 0.9s ease-in-out ${delays[i % delays.length]}s infinite` : "none",
          transform: playing ? "none" : "scaleY(0.35)", opacity: playing ? 1 : 0.5,
        }} />
      ))}
    </div>
  );
}

// Big circular progress ring used in Now Playing
function ProgressRing({ progress = 0, size = 250, stroke = 6, children }) {
  const r = (size - stroke) / 2;
  const c = 2 * Math.PI * r;
  const off = c * (1 - Math.min(1, Math.max(0, progress)));
  return (
    <div style={{ position: "relative", width: size, height: size }}>
      <svg width={size} height={size} style={{ transform: "rotate(-90deg)", display: "block" }}>
        <defs>
          <linearGradient id="ringGrad" x1="0" y1="0" x2="1" y2="1">
            <stop offset="0%" stopColor="oklch(0.70 0.13 230)" />
            <stop offset="55%" stopColor="oklch(0.74 0.12 195)" />
            <stop offset="100%" stopColor="oklch(0.76 0.11 165)" />
          </linearGradient>
        </defs>
        <circle cx={size/2} cy={size/2} r={r} stroke="oklch(0.6 0.02 250 / 0.14)" strokeWidth={stroke} fill="none" />
        <circle cx={size/2} cy={size/2} r={r} stroke="url(#ringGrad)" strokeWidth={stroke} fill="none"
          strokeLinecap="round" strokeDasharray={c} strokeDashoffset={off}
          style={{ transition: "stroke-dashoffset 0.3s linear" }} />
      </svg>
      <div style={{ position: "absolute", inset: 0, display: "grid", placeItems: "center" }}>{children}</div>
    </div>
  );
}

// thin progress bar
function Bar({ progress = 0, h = 5 }) {
  return (
    <div style={{ height: h, borderRadius: h, background: "oklch(0.6 0.02 250 / 0.16)", overflow: "hidden" }}>
      <div style={{ height: "100%", borderRadius: h, width: (Math.min(1, progress) * 100) + "%",
        background: "linear-gradient(90deg, var(--accent), var(--accent-2) 60%, var(--accent-3))",
        transition: "width 0.3s linear" }} />
    </div>
  );
}

// pill chip
function Chip({ children, tone = "soft" }) {
  const styles = tone === "solid"
    ? { background: "linear-gradient(120deg, var(--accent), var(--accent-2))", color: "#fff" }
    : { background: "oklch(1 0 0 / 0.6)", color: "var(--ink-soft)", border: "1px solid var(--glass-line)" };
  return <span style={{ ...styles, fontSize: 11.5, fontWeight: 600, padding: "3px 10px", borderRadius: 999, lineHeight: 1.4, whiteSpace: "nowrap" }}>{children}</span>;
}

// keyframes injected once
(function injectKeyframes() {
  if (document.getElementById("tj-kf")) return;
  const s = document.createElement("style");
  s.id = "tj-kf";
  s.textContent = `
    @keyframes eq { 0%,100%{transform:scaleY(0.3)} 50%{transform:scaleY(1)} }
    @keyframes pop { 0%{transform:scale(0.6);opacity:0} 60%{transform:scale(1.08)} 100%{transform:scale(1);opacity:1} }
    @keyframes pulse { 0%,100%{box-shadow:0 0 0 0 oklch(0.7 0.12 200 / 0.35)} 50%{box-shadow:0 0 0 10px oklch(0.7 0.12 200 / 0)} }
    @keyframes slideUp { from{transform:translateY(100%)} to{transform:translateY(0)} }
    @keyframes livedot { 0%,100%{opacity:1;transform:scale(1)} 50%{opacity:0.4;transform:scale(0.7)} }
    @keyframes flow { from{background-position:0 0} to{background-position:40px 0} }
    @media (prefers-reduced-motion: reduce){ @keyframes eq {0%,100%{transform:scaleY(0.6)}} }
  `;
  document.head.appendChild(s);
})();

// Live ring — faint full ring + slowly rotating accent arc (no "end")
function LiveRing({ size = 244, stroke = 7, isPlaying = true, children }) {
  const r = (size - stroke) / 2;
  const c = 2 * Math.PI * r;
  const arc = c * 0.28; // ~100deg visible arc
  return (
    <div style={{ position: "relative", width: size, height: size }}>
      <svg width={size} height={size} style={{ display: "block", animation: isPlaying ? "spin 6s linear infinite" : "none" }}>
        <defs>
          <linearGradient id="liveGrad" x1="0" y1="0" x2="1" y2="1">
            <stop offset="0%" stopColor="oklch(0.70 0.13 230)" />
            <stop offset="100%" stopColor="oklch(0.76 0.11 165)" />
          </linearGradient>
        </defs>
        <circle cx={size/2} cy={size/2} r={r} stroke="oklch(0.6 0.02 250 / 0.14)" strokeWidth={stroke} fill="none" />
        <circle cx={size/2} cy={size/2} r={r} stroke="url(#liveGrad)" strokeWidth={stroke} fill="none"
          strokeLinecap="round" strokeDasharray={arc + " " + (c - arc)} transform={`rotate(-90 ${size/2} ${size/2})`} />
      </svg>
      <div style={{ position: "absolute", inset: 0, display: "grid", placeItems: "center" }}>{children}</div>
    </div>
  );
}

// pulsing "ถ่ายทอดสด" pill
function LivePill({ playing = true }) {
  return (
    <div style={{ display: "inline-flex", alignItems: "center", gap: 8, padding: "7px 15px", borderRadius: 999,
      background: "oklch(0.96 0.03 20 / 0.7)", border: "1px solid oklch(0.7 0.14 25 / 0.3)" }}>
      <span style={{ width: 9, height: 9, borderRadius: "50%", background: "oklch(0.62 0.20 25)",
        animation: playing ? "livedot 1.4s ease-in-out infinite" : "none" }} />
      <span style={{ fontSize: 13, fontWeight: 700, color: "oklch(0.55 0.16 25)", letterSpacing: "0.02em" }}>ถ่ายทอดสด</span>
    </div>
  );
}

Object.assign(window, { Icon, Cover, EQ, ProgressRing, LiveRing, LivePill, Bar, Chip,
  useState, useEffect, useRef, useMemo });
