import { jsxs as p, jsx as o, Fragment as I } from "react/jsx-runtime";
import { cn as O } from "../../utils/tailwindUtil.es.js";
import { getContentString as q } from "../../utils/utils.es.js";
import { BotMessageSquare as J, LoaderCircle as F, Sparkles as W, ChevronRight as D, Wrench as G } from "lucide-react";
import H, { useState as Q, useEffect as U } from "react";
import V from "../TodoList.es.js";
import { AgentMarkdown as R } from "../ui/AgentMarkdown.es.js";
import { BranchSwitcher as X } from "./BranchSwitcher.es.js";
import { MessageActions as Y } from "./MessageActions.es.js";
function Z(t) {
  if (!t) return null;
  const e = t.additional_kwargs;
  return e && typeof e.reasoning_content == "string" && e.reasoning_content.length > 0 ? e.reasoning_content : null;
}
function P({
  reasoningText: t,
  toolStatuses: e,
  isStreaming: n,
  fontSize: y
}) {
  const [u, a] = Q(!1);
  U(() => {
    a(!!n);
  }, [n]);
  const b = e && e.length > 0, x = !!t && t.length > 0;
  return !b && !x && !n ? null : /* @__PURE__ */ p("div", { className: "my-1", children: [
    /* @__PURE__ */ p(
      "button",
      {
        type: "button",
        onClick: () => a((f) => !f),
        className: "inline-flex items-center gap-1.5 text-xs text-zinc-400/80 transition-colors hover:text-zinc-300",
        children: [
          n ? /* @__PURE__ */ o(F, { className: "size-3 animate-spin" }) : /* @__PURE__ */ o(W, { className: "size-3" }),
          /* @__PURE__ */ o("span", { children: n ? "Thinking" : "Thought" }),
          /* @__PURE__ */ o(
            D,
            {
              className: O(
                "size-3 transition-transform duration-200",
                u && "rotate-90"
              )
            }
          )
        ]
      }
    ),
    u && /* @__PURE__ */ p("div", { className: "mt-1.5 border-l-2 border-white/10 pl-4 space-y-1", children: [
      b && e.map((f) => /* @__PURE__ */ p("div", { className: "flex items-center gap-2 text-sm text-zinc-300 py-0.5", children: [
        !f.isCompleted && /* @__PURE__ */ o(F, { className: "size-3.5 animate-spin text-zinc-400" }),
        /* @__PURE__ */ o(G, { className: "size-3.5 text-zinc-500" }),
        /* @__PURE__ */ o("span", { children: f.label })
      ] }, f.key)),
      x && /* @__PURE__ */ o("div", { className: "text-sm text-zinc-300", children: /* @__PURE__ */ o(R, { fontSize: y, children: t }) })
    ] })
  ] });
}
function ee(t) {
  if (!t || typeof t != "object") return null;
  const e = t, n = typeof e.visible == "boolean" ? e.visible : typeof e.show == "boolean" ? e.show : typeof e.display == "boolean" ? e.display : typeof e.hidden == "boolean" ? !e.hidden : !0;
  return {
    id: typeof e.id == "string" ? e.id : void 0,
    name: typeof e.name == "string" ? e.name : void 0,
    label: typeof e.label == "string" ? e.label : void 0,
    event: typeof e.event == "string" ? e.event : void 0,
    visible: n
  };
}
function te(t) {
  if (!t) return [];
  const e = t.additional_kwargs;
  return !e || !Array.isArray(e.tool_status) ? [] : e.tool_status.map(ee).filter((n) => n !== null);
}
function ne(t) {
  if (!t) return !1;
  const e = t.additional_kwargs;
  return e ? Object.prototype.hasOwnProperty.call(e, "tool_status") : !1;
}
function _(t) {
  return t ? t.toLowerCase().replace(/\s+/g, " ").replace(/\s*event:\s*metadata\s*$/i, "").trim() : "";
}
function oe(t) {
  return !(t.visible === !1 || _(t.label).length === 0 || t.event?.toLowerCase() === "metadata" || /\bevent:\s*metadata\b/i.test(t.label ?? ""));
}
function se(t, e) {
  if (!e) return t;
  if (typeof e == "string") {
    const n = e.trim();
    return n.length === 0 ? t : `${t} ${n.slice(0, 80)}${n.length > 80 ? "..." : ""}`;
  }
  if (typeof e == "object")
    try {
      const n = JSON.stringify(e);
      return `${t} ${n.length > 80 ? `${n.slice(0, 80)}...` : n}`;
    } catch {
      return t;
    }
  return t;
}
function ie(t, e, n, y) {
  if (!t || t.length === 0) return null;
  const u = t, a = u.filter((s) => s.type === "tool"), b = u.some((s) => ne(s)), x = new Set(
    a.map((s) => s.tool_call_id).filter((s) => typeof s == "string" && s.length > 0)
  ), f = new Set(
    a.map((s) => s.name).filter((s) => typeof s == "string" && s.length > 0)
  ), d = [], h = [], g = [], m = /* @__PURE__ */ new Set();
  let c = 0;
  const N = (s) => {
    if (e)
      for (const i of te(s)) {
        if (!oe(i)) continue;
        const r = _(i.label) || _(i.name) || "tool";
        if (m.has(r)) continue;
        m.add(r);
        const v = i.label ?? i.name ?? "Running tool", k = (i.id ? x.has(i.id) : !1) || (i.name ? f.has(i.name) : !1) || !n;
        d.push({ key: r, label: v, isCompleted: k });
      }
  };
  for (const s of u) {
    if (s.type !== "ai") continue;
    const i = s.content, r = Z(s);
    if (N(s), typeof i == "string") {
      r ? h.push(r) : i.length > 0 && (g.push(
        /* @__PURE__ */ o("div", { className: "py-1", children: /* @__PURE__ */ o(R, { fontSize: y, children: i }) }, `text-content-${s.id ?? c}`)
      ), c++);
      continue;
    }
    if (!Array.isArray(i)) {
      r && h.push(r);
      continue;
    }
    const v = i;
    let k = "", j = !1;
    const C = () => {
      k.length > 0 && (g.push(
        /* @__PURE__ */ o("div", { className: "py-1", children: /* @__PURE__ */ o(R, { fontSize: y, children: k }) }, `text-${s.id ?? c}-${c}`)
      ), k = "", c++);
    };
    for (let T = 0; T < v.length; T++) {
      const l = v[T];
      if (l.type === "text" && typeof l.text == "string") {
        k += l.text;
        continue;
      }
      if (l.type === "reasoning" && typeof l.reasoning == "string" || l.type === "thinking" && typeof l.thinking == "string") {
        C(), j = !0;
        const z = l.reasoning ?? l.thinking;
        h.push(z);
        continue;
      }
      if (e && !b && l.type === "tool_use") {
        C();
        const z = typeof l.id == "string" ? l.id : void 0, S = typeof l.name == "string" ? l.name : "tool", $ = _(S) || `tool-${T}`;
        if (!m.has($)) {
          m.add($);
          const B = se(S, l.input), E = z ? x.has(z) : !n;
          d.push({ key: $, label: B, isCompleted: E });
        }
      }
    }
    !j && r ? (k = "", h.push(r)) : C();
  }
  if (e && !n && !b && d.length === 0 && g.length === 0 && a.length > 0)
    for (const s of a)
      d.push({
        key: `tool-msg-${s.id ?? c}`,
        label: "Tool call completed",
        isCompleted: !0
      }), c++;
  const w = [], M = g.length > 0, A = h.join(`

`), L = e && d.length > 0 || A.length > 0, K = !!n && !M;
  return (L || K) && w.push(
    /* @__PURE__ */ o(
      P,
      {
        reasoningText: A || void 0,
        toolStatuses: e ? d : void 0,
        isStreaming: K,
        fontSize: y
      },
      "agent-activity"
    )
  ), w.push(...g), w.length > 0 ? /* @__PURE__ */ o(I, { children: w }) : null;
}
function le({
  agentName: t,
  fontSize: e,
  message: n,
  groupedMessages: y,
  showToolActivity: u = !0,
  isStreaming: a = !1,
  onRegenerate: b,
  feedback: x,
  onFeedback: f,
  branch: d,
  branchOptions: h,
  onBranchSelect: g,
  todos: m
}) {
  const c = q(n?.content), N = ie(y ?? [n], u, a, e);
  return /* @__PURE__ */ p("div", { className: "agent-message flex flex-col gap-1 w-full group", children: [
    /* @__PURE__ */ p("div", { className: "flex items-center gap-3 w-full", children: [
      /* @__PURE__ */ o(
        "div",
        {
          className: "rounded-full size-8 shrink-0 bg-zinc-800 flex items-center justify-center p-2",
          "data-alt": "AI Avatar",
          children: /* @__PURE__ */ o(J, { className: "text-xs", color: "white" })
        }
      ),
      /* @__PURE__ */ o("span", { className: "text-zinc-500 text-sm", children: t || "Agent" })
    ] }),
    /* @__PURE__ */ p("div", { className: "flex flex-1 flex-col gap-1 items-start min-w-0", children: [
      m && m.length > 0 && /* @__PURE__ */ o(V, { todos: m }),
      /* @__PURE__ */ o("div", { className: "text-content text-foreground", style: e ? { fontSize: e } : void 0, children: N || /* @__PURE__ */ p(I, { children: [
        /* @__PURE__ */ o("div", { className: "flex items-center gap-2 text-zinc-500", children: /* @__PURE__ */ o("span", { children: "Thinking..." }) }),
        a && c && /* @__PURE__ */ o("span", { className: "inline-block w-2 h-4 ml-1 bg-zinc-400 animate-pulse" })
      ] }) }),
      !a && d && h && g && h.length > 1 && /* @__PURE__ */ o(
        X,
        {
          branch: d,
          branchOptions: h,
          onSelect: g,
          isLoading: a
        }
      ),
      !a && c && /* @__PURE__ */ o(
        Y,
        {
          message: n,
          onRegenerate: b,
          feedback: x,
          onFeedback: f,
          className: ""
        }
      )
    ] })
  ] });
}
const pe = H.memo(le, (t, e) => e.isStreaming ? !1 : t.message.id === e.message.id && t.groupedMessages?.length === e.groupedMessages?.length && t.showToolActivity === e.showToolActivity && t.isStreaming === e.isStreaming && t.feedback === e.feedback && t.onRegenerate === e.onRegenerate && t.onFeedback === e.onFeedback && t.branch === e.branch && t.branchOptions?.length === e.branchOptions?.length && t.onBranchSelect === e.onBranchSelect && t.todos === e.todos);
export {
  pe as default
};
//# sourceMappingURL=AgentMessage.es.js.map
