// ui.jsx — small shared primitives

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

function Logo({ size = 18 }) {
  return (
    <span style={{ display: "inline-flex", alignItems: "center", flexShrink: 0 }}>
      <img src="disene_logo_light.svg" className="logo-light" alt="disene" style={{ height: size }} />
      <img src="disene_logo_dark.svg"  className="logo-dark"  alt="disene" style={{ height: size }} />
    </span>
  );
}

// Type tag — "CONSIST", "PERSPECTIVE 02", etc.
function Tag({ kind, children, style }) {
  return (
    <span className={`tag ${kind}`} style={style}>{children}</span>
  );
}

// 5-dot progress indicator — Consist, Function, Perspective, Pattern, Essence
function Dots({ concept, size = 6 }) {
  const f = window.levelsFilled(concept);
  const order = [
    { key: "consist",     cls: "f-consist",     filled: f.consist },
    { key: "function",    cls: "f-function",    filled: f.function },
    { key: "perspective", cls: "f-perspective", filled: f.perspective },
    { key: "pattern",     cls: "f-pattern",     filled: f.pattern },
    { key: "essence",     cls: "f-essence",     filled: f.essence },
  ];
  return (
    <span className="dots" style={{ "--s": size + "px" }}>
      {order.map((d) => (
        <span
          key={d.key}
          className={`d ${d.filled ? d.cls : ""}`}
          style={{ width: size, height: size }}
          title={d.key}
        />
      ))}
    </span>
  );
}

// IconButton with svg slot
function IconBtn({ children, onClick, title, active }) {
  return (
    <button
      className={`btn icon ghost`}
      onClick={onClick}
      title={title}
      style={active ? { background: "var(--bg-sunk)", color: "var(--fg)" } : null}
    >
      {children}
    </button>
  );
}

// Tiny icons (stroke-based; system prompt says no complex SVG, simple geometric ok)
const Icons = {
  Sun: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round">
      <circle cx="12" cy="12" r="4" /><path d="M12 3v2M12 19v2M3 12h2M19 12h2M5.6 5.6l1.4 1.4M17 17l1.4 1.4M5.6 18.4 7 17M17 7l1.4-1.4"/>
    </svg>
  ),
  Moon: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
      <path d="M21 12.8A8.5 8.5 0 1 1 11.2 3a6.5 6.5 0 0 0 9.8 9.8Z" />
    </svg>
  ),
  Search: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round">
      <circle cx="11" cy="11" r="6" /><path d="m20 20-3.5-3.5" />
    </svg>
  ),
  Plus: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round">
      <path d="M12 5v14M5 12h14" />
    </svg>
  ),
  ArrowL: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
      <path d="M15 5l-7 7 7 7"/>
    </svg>
  ),
  Edit: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
      <path d="M4 20h4l10-10-4-4L4 16v4Z"/><path d="m14 6 4 4"/>
    </svg>
  ),
  Eye: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
      <path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12Z"/><circle cx="12" cy="12" r="3"/>
    </svg>
  ),
  Planet: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round">
      <circle cx="12" cy="12" r="4"/>
      <ellipse cx="12" cy="12" rx="10" ry="3.5" transform="rotate(-20 12 12)"/>
    </svg>
  ),
  Pause: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor"><rect x="6" y="5" width="4" height="14" rx="1"/><rect x="14" y="5" width="4" height="14" rx="1"/></svg>
  ),
  Play: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor"><path d="M7 5v14l12-7L7 5Z"/></svg>
  ),
  Grid: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7"><rect x="4" y="4" width="7" height="7" rx="1"/><rect x="13" y="4" width="7" height="7" rx="1"/><rect x="4" y="13" width="7" height="7" rx="1"/><rect x="13" y="13" width="7" height="7" rx="1"/></svg>
  ),
  Trash: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round">
      <path d="M4 7h16M9 7V5a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2M6 7l1 12a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2l1-12"/>
    </svg>
  ),
  Link: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
      <path d="M10 14a4 4 0 0 0 5.66 0l3-3a4 4 0 0 0-5.66-5.66l-1.4 1.4"/>
      <path d="M14 10a4 4 0 0 0-5.66 0l-3 3a4 4 0 1 0 5.66 5.66l1.4-1.4"/>
    </svg>
  ),
  Sliders: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round">
      <line x1="4" y1="6" x2="20" y2="6"/><circle cx="9" cy="6" r="2.5" fill="var(--bg-elev)" stroke="currentColor" strokeWidth="1.7"/>
      <line x1="4" y1="12" x2="20" y2="12"/><circle cx="16" cy="12" r="2.5" fill="var(--bg-elev)" stroke="currentColor" strokeWidth="1.7"/>
      <line x1="4" y1="18" x2="20" y2="18"/><circle cx="10" cy="18" r="2.5" fill="var(--bg-elev)" stroke="currentColor" strokeWidth="1.7"/>
    </svg>
  ),
  Graph: (s = 14) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round">
      <circle cx="5" cy="12" r="2.5"/>
      <circle cx="19" cy="6" r="2.5"/>
      <circle cx="19" cy="18" r="2.5"/>
      <line x1="7.3" y1="11" x2="16.7" y2="7"/>
      <line x1="7.3" y1="13" x2="16.7" y2="17"/>
    </svg>
  ),
};

// Minimal placeholder image (subtly-striped) used in onboarding / empty states
function StripePlaceholder({ label, h = 120 }) {
  return (
    <div style={{
      height: h,
      borderRadius: 10,
      border: "1px solid var(--border-soft)",
      background:
        "repeating-linear-gradient(135deg, transparent 0 7px, color-mix(in oklab, var(--fg-3) 12%, transparent) 7px 8px), var(--bg-sunk)",
      display: "flex", alignItems: "center", justifyContent: "center",
      color: "var(--fg-3)", fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: ".1em", textTransform: "uppercase"
    }}>{label}</div>
  );
}

Object.assign(window, { Logo, Tag, Dots, IconBtn, Icons, StripePlaceholder });
