/* ===========================================================================
   Azure Helper — shared primitives  (exported to window)
   =========================================================================== */
const { useState, useEffect, useRef, useMemo } = React;

/* ---------- Icons (inline stroke SVG, 1.6 stroke) ---------- */
function Icon({ name, size = 16, color = "currentColor", style, className }) {
  const p = { width: size, height: size, viewBox: "0 0 24 24", fill: "none",
    stroke: color, strokeWidth: 1.7, strokeLinecap: "round", strokeLinejoin: "round", style, className };
  const paths = {
    home: <><path d="M3 11l9-7 9 7" /><path d="M5 10v10h14V10" /></>,
    agent: <><rect x="4" y="7" width="16" height="12" rx="2" /><path d="M12 7V4" /><circle cx="12" cy="3" r="1" /><path d="M9 13h.01M15 13h.01" /><path d="M9 16.5c1 .7 5 .7 6 0" /></>,
    graph: <><circle cx="6" cy="6" r="2.4" /><circle cx="18" cy="7" r="2.4" /><circle cx="9" cy="18" r="2.4" /><path d="M8 7.5l8 .2M7.5 8l1.2 8M11 17l5.5-8.5" /></>,
    book: <><path d="M4 5a2 2 0 0 1 2-2h13v16H6a2 2 0 0 0-2 2z" /><path d="M19 17H6a2 2 0 0 0-2 2" /></>,
    bolt: <><path d="M13 2L4 14h7l-1 8 9-12h-7z" /></>,
    check: <><path d="M20 6L9 17l-5-5" /></>,
    x: <><path d="M18 6L6 18M6 6l12 12" /></>,
    play: <><path d="M6 4l14 8-14 8z" /></>,
    shield: <><path d="M12 3l8 3v6c0 4.5-3.2 7.8-8 9-4.8-1.2-8-4.5-8-9V6z" /></>,
    user: <><circle cx="12" cy="8" r="4" /><path d="M4 21c1.5-4 14.5-4 16 0" /></>,
    users: <><circle cx="9" cy="8" r="3.4" /><path d="M2.5 20c1-3.5 11-3.5 13 0" /><path d="M16 6a3.4 3.4 0 0 1 0 6.5M22 20c-.5-2-2-3-4-3.5" /></>,
    key: <><circle cx="8" cy="14" r="4" /><path d="M11 11l9-9M17 5l2 2M14 8l2 2" /></>,
    plug: <><path d="M9 2v6M15 2v6" /><path d="M7 8h10v3a5 5 0 0 1-10 0z" /><path d="M12 16v6" /></>,
    arrow: <><path d="M5 12h14M13 6l6 6-6 6" /></>,
    chevron: <><path d="M9 6l6 6-6 6" /></>,
    spark: <><path d="M12 3v4M12 17v4M3 12h4M17 12h4M6 6l2.5 2.5M15.5 15.5L18 18M18 6l-2.5 2.5M8.5 15.5L6 18" /></>,
    eye: <><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7z" /><circle cx="12" cy="12" r="3" /></>,
    alert: <><path d="M12 3l9 16H3z" /><path d="M12 10v4M12 17h.01" /></>,
    copy: <><rect x="9" y="9" width="11" height="11" rx="2" /><path d="M5 15V5a2 2 0 0 1 2-2h8" /></>,
    send: <><path d="M22 2L11 13M22 2l-7 20-4-9-9-4z" /></>,
    refresh: <><path d="M21 12a9 9 0 1 1-3-6.7L21 8" /><path d="M21 3v5h-5" /></>,
    list: <><path d="M8 6h13M8 12h13M8 18h13M3 6h.01M3 12h.01M3 18h.01" /></>,
    layers: <><path d="M12 3l9 5-9 5-9-5z" /><path d="M3 13l9 5 9-5M3 17l9 5 9-5" /></>,
    folder: <><path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z" /></>,
    gear: <><circle cx="12" cy="12" r="3.2" /><path d="M12 2v3M12 19v3M2 12h3M19 12h3M5 5l2 2M17 17l2 2M19 5l-2 2M7 17l-2 2" /></>,
  };
  return <svg {...p}>{paths[name] || null}</svg>;
}

/* ---------- // SECTION label ---------- */
function SecLabel({ children }) { return <div className="seclabel">{children}</div>; }

/* ---------- PS prompt line ---------- */
function PS({ user = "jamie", host = "contoso", path = "Helper", cmd, caret }) {
  return (
    <div className="ps">
      <span className="ps-user">PS {user}@{host}</span>
      <span style={{ color: "var(--text-faint)" }}>:~/{path}&gt;</span>
      {cmd && <span className="ps-cmd">{cmd}</span>}
      {caret && <span className="caret" />}
    </div>
  );
}

/* ---------- breadcrumb crumbs ---------- */
function Crumbs({ items }) {
  return (
    <div className="crumbs">
      <span className="chev">&gt;</span>
      {items.map((it, i) => (
        <React.Fragment key={i}>
          {i > 0 && <span className="sep">·</span>}
          <span className={it.on ? "on" : ""}>{it.label}</span>
        </React.Fragment>
      ))}
    </div>
  );
}

/* ---------- Method tag ---------- */
function Method({ m }) {
  const cls = { GET: "m-get", POST: "m-post", PATCH: "m-patch", DELETE: "m-delete" }[m] || "m-get";
  return <span className={"method " + cls}>{m}</span>;
}

/* ---------- JSON syntax-highlighted view ---------- */
function highlightJSON(value, indent = 0) {
  const pad = "  ".repeat(indent);
  const padIn = "  ".repeat(indent + 1);
  if (value === null) return <span className="tok-bool">null</span>;
  if (typeof value === "number") return <span className="tok-num">{String(value)}</span>;
  if (typeof value === "boolean") return <span className="tok-bool">{String(value)}</span>;
  if (typeof value === "string") return <span className="tok-str">"{value}"</span>;
  if (Array.isArray(value)) {
    if (value.length === 0) return <span className="tok-punc">[]</span>;
    return (
      <>
        <span className="tok-punc">[</span>{"\n"}
        {value.map((v, i) => (
          <React.Fragment key={i}>
            {padIn}{highlightJSON(v, indent + 1)}
            {i < value.length - 1 ? <span className="tok-punc">,</span> : null}{"\n"}
          </React.Fragment>
        ))}
        {pad}<span className="tok-punc">]</span>
      </>
    );
  }
  const keys = Object.keys(value);
  if (keys.length === 0) return <span className="tok-punc">{"{}"}</span>;
  return (
    <>
      <span className="tok-punc">{"{"}</span>{"\n"}
      {keys.map((k, i) => (
        <React.Fragment key={k}>
          {padIn}<span className="tok-key">"{k}"</span><span className="tok-punc">: </span>
          {highlightJSON(value[k], indent + 1)}
          {i < keys.length - 1 ? <span className="tok-punc">,</span> : null}{"\n"}
        </React.Fragment>
      ))}
      {pad}<span className="tok-punc">{"}"}</span>
    </>
  );
}

function JsonView({ data, style }) {
  return <pre className="code" style={style}>{highlightJSON(data)}</pre>;
}

/* ---------- Copyable command line ---------- */
function CmdLine({ method, url, body }) {
  return (
    <div className="code" style={{ display: "flex", flexDirection: "column", gap: 8 }}>
      <div style={{ display: "flex", gap: 10, alignItems: "center" }}>
        <Method m={method} />
        <span style={{ color: "var(--text-2)" }}>https://graph.microsoft.com<span style={{ color: "var(--text)" }}>{url}</span></span>
      </div>
      {body && <div style={{ borderTop: "1px solid var(--border)", paddingTop: 8 }}>{highlightJSON(body)}</div>}
    </div>
  );
}

/* ---------- Risk pill ---------- */
function RiskPill({ risk }) {
  if (risk === "write") return <span className="chip gold" style={{ fontWeight: 700 }}>● writes data</span>;
  return <span className="chip cy">○ read-only</span>;
}

/* ---------- Typing dots ---------- */
function Thinking({ label = "thinking" }) {
  const [d, setD] = useState(1);
  useEffect(() => { const t = setInterval(() => setD(x => (x % 3) + 1), 350); return () => clearInterval(t); }, []);
  return <span className="mono dim" style={{ fontSize: 12.5 }}>{label}{".".repeat(d)}</span>;
}

Object.assign(window, { Icon, SecLabel, PS, Crumbs, Method, JsonView, CmdLine, RiskPill, Thinking, highlightJSON });
