/* Pipeline prototype — icons, atoms, mention parser, frame thumbnails */
const { useState, useEffect, useRef } = React;

function CIcon({ name, size = 16, className = "" }) {
  const p = { width: size, height: size, fill: "none", stroke: "currentColor", strokeWidth: 1.6, strokeLinecap: "round", strokeLinejoin: "round" };
  const G = {
    gallery: <g><rect x="3" y="3" width="7" height="7" rx="1.5"/><rect x="14" y="3" width="7" height="7" rx="1.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><rect x="14" y="14" width="7" height="7" rx="1.5"/></g>,
    bible: <g><path d="M4 5a2 2 0 0 1 2-2h13v16H6a2 2 0 0 0-2 2z"/><path d="M4 5v14"/><path d="M9 7h6M9 10h6"/></g>,
    mywork: <g><rect x="4" y="4" width="16" height="16" rx="2.5"/><path d="M8.5 12l2.5 2.5L16 9"/></g>,
    review: <g><rect x="4" y="4" width="16" height="16" rx="2.5"/><path d="M8 9h8M8 13h5"/></g>,
    shots: <g><rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 9h18M8 5v14"/></g>,
    chars: <g><circle cx="9" cy="8" r="3"/><path d="M4 19a5 5 0 0 1 10 0"/><path d="M16 6.5a2.8 2.8 0 0 1 0 5.4"/><path d="M16.5 13.5A4.6 4.6 0 0 1 20 18"/></g>,
    env: <g><path d="M3 19l6-9 4 5 3-4 5 8z"/><circle cx="8" cy="7" r="1.6"/></g>,
    assets: <g><path d="M12 3l8 4.5v9L12 21l-8-4.5v-9z"/><path d="M12 3v18M4 7.5l8 4.5 8-4.5"/></g>,
    rnd: <g><rect x="6" y="3" width="12" height="18" rx="2"/><path d="M9 7h6M9 11h6M9 15h3"/></g>,
    library: <g><path d="M3 7l1.6-2.2A2 2 0 0 1 6.2 4h3.4a2 2 0 0 1 1.6.8L12 6h6a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/></g>,
    sync: <g><path d="M4 12a8 8 0 0 1 13.7-5.6L20 8"/><path d="M20 4v4h-4"/><path d="M20 12a8 8 0 0 1-13.7 5.6L4 16"/><path d="M4 20v-4h4"/></g>,
    members: <g><circle cx="10" cy="8" r="3.2"/><path d="M3.5 19a6.5 6.5 0 0 1 13 0"/><path d="M19 8v5M21.5 10.5h-5"/></g>,
    chev: <path d="M9 6l6 6-6 6"/>,
    back: <path d="M15 6l-6 6 6 6"/>,
    plus: <path d="M12 5v14M5 12h14"/>,
    x: <path d="M6 6l12 12M18 6L6 18"/>,
    check: <path d="M4 12l5 5L20 6"/>,
    clock: <g><circle cx="12" cy="12" r="8"/><path d="M12 8v4l3 2"/></g>,
    plane: <g><path d="M21 4L3 10.5l6.5 2.5L12 20l3-6 6-10z"/><path d="M9.5 13L21 4"/></g>,
    heart: <path d="M12 20S4 14.5 4 8.8A4 4 0 0 1 12 7a4 4 0 0 1 8 1.8C20 14.5 12 20 12 20z"/>,
    link: <g><path d="M9 15l6-6"/><path d="M11 7l1-1a4 4 0 0 1 6 6l-1 1"/><path d="M13 17l-1 1a4 4 0 0 1-6-6l1-1"/></g>,
    download: <g><path d="M12 4v11"/><path d="M7 11l5 5 5-5"/><path d="M5 20h14"/></g>,
    play: <path d="M8 5l11 7-11 7z"/>,
    sparkle: <path d="M12 4l1.7 4.6L18 10l-4.3 1.4L12 16l-1.7-4.6L6 10l4.3-1.4z"/>,
    wand: <g><path d="M5 19l9-9"/><path d="M14 6l1.5 1.5"/><path d="M17 4l.7 1.7L19.5 6l-1.8.7L17 8.5l-.7-1.8L14.5 6z"/></g>,
    search: <g><circle cx="11" cy="11" r="6"/><path d="M20 20l-3.5-3.5"/></g>,
    grid: <g><rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/></g>,
    sliders: <g><path d="M4 6h10M18 6h2M4 12h2M10 12h10M4 18h7M15 18h5"/><circle cx="16" cy="6" r="2"/><circle cx="8" cy="12" r="2"/><circle cx="13" cy="18" r="2"/></g>,
    folder: <path d="M3 7l1.6-2.2A2 2 0 0 1 6.2 4h3.4a2 2 0 0 1 1.6.8L12 6h6a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/>,
    bolt: <path d="M13 3L5 13h6l-1 8 8-10h-6z"/>,
    refs: <g><rect x="4" y="4" width="16" height="12" rx="2"/><path d="M8 20h8"/><path d="M8 11l2.5-2.5 2 2L16 7"/></g>,
    timeline: <g><path d="M4 7h16M4 12h16M4 17h16"/><circle cx="9" cy="7" r="1.6" fill="currentColor"/><circle cx="15" cy="12" r="1.6" fill="currentColor"/><circle cx="7" cy="17" r="1.6" fill="currentColor"/></g>
  };
  return <svg viewBox="0 0 24 24" className={className} style={p}>{G[name] || null}</svg>;
}

function Avt({ m, size = "s26" }) { return <span className={"avt " + size + " " + (m.av || "a")}>{m.initials}</span>; }
function Status({ s }) { return <span className={"st " + s}>{window.CL.statusLabel[s] || s}</span>; }

function memberById(id) { return window.CL.members.find((m) => m.id === id); }
function entityByName(name) {
  const c = window.CL;
  const all = [...c.chars.map((x) => ({ ...x, kind: "char" })), ...c.envs.map((x) => ({ ...x, kind: "env" })), ...c.assets.map((x) => ({ ...x, kind: "asset" }))];
  return all.find((e) => e.name.toLowerCase() === name.toLowerCase());
}

/* render text with @Mentions as colored links */
function Mentions({ text, onClick }) {
  const parts = String(text).split(/(@[A-Za-z][A-Za-z0-9]*)/g);
  return <>{parts.map((p, i) => {
    if (p[0] === "@") {
      const e = entityByName(p.slice(1));
      const kind = e ? e.kind : "char";
      return <span key={i} className={"cl-link " + kind} onClick={(ev) => { ev.stopPropagation(); onClick && onClick(e, kind); }}>{p.slice(1)}</span>;
    }
    return <span key={i}>{p}</span>;
  })}</>;
}

/* procedural cinematic frame — deterministic from seed */
function frameStyle(seed, hue) {
  let h = 0; for (let i = 0; i < seed.length; i++) h = (h * 31 + seed.charCodeAt(i)) >>> 0;
  const base = hue != null ? hue : (h % 360);
  const ang = 90 + (h % 60);
  const gx = 25 + (h % 50), gy = 30 + ((h >> 3) % 40);
  const l1 = 30 + (h % 12);
  return {
    background:
      `radial-gradient(120% 90% at ${gx}% ${gy}%, hsl(${base} 55% ${l1}% / .9), transparent 60%),` +
      `radial-gradient(80% 70% at ${100 - gx}% ${100 - gy}%, hsl(${(base + 30) % 360} 40% 22% / .8), transparent 55%),` +
      `linear-gradient(${ang}deg, hsl(${base} 30% 10%), hsl(${(base + 20) % 360} 35% 16%))`
  };
}

function Frame({ seed, hue, children, className = "", style = {} }) {
  return <div className={className} style={{ ...frameStyle(seed, hue), ...style }}>{children}</div>;
}

Object.assign(window, { CIcon, Avt, Status, memberById, entityByName, Mentions, frameStyle, Frame });
