"use client";
import { jsx as r, jsxs as n, Fragment as S } from "react/jsx-runtime";
import { c as y } from "../../../index-UgeK3A8s.js";
import { Table as z, Code as V, TriangleAlert as w, ChevronRight as h, Wrench as j, CheckCircle2 as I, Loader2 as E } from "lucide-react";
import * as l from "react";
import { Accordion as O, AccordionContent as R, AccordionItem as J, AccordionTrigger as $ } from "../atoms/accordion.js";
import { Collapsible as D, CollapsibleTrigger as H, CollapsibleContent as F } from "../atoms/collapsible.js";
import { CopyButton as k } from "../atoms/copy-button.js";
import { JsonHighlight as v } from "../atoms/json-highlight.js";
import { cn as c } from "../../../lib/utils.js";
const M = y(
  "font-mono text-xs transition-colors",
  {
    variants: {
      state: {
        calling: "text-muted-foreground hover:text-foreground",
        complete: "text-muted-foreground hover:text-foreground",
        error: "text-destructive hover:text-destructive/80"
      }
    },
    defaultVariants: { state: "calling" }
  }
), B = y(
  "absolute -top-0.5 right-0 size-2 shrink-0 rounded-full ring-2 ring-background",
  {
    variants: {
      state: {
        calling: "animate-pulse bg-orange-400",
        complete: "bg-green-500",
        error: "bg-destructive"
      }
    },
    defaultVariants: { state: "calling" }
  }
);
function L(e, t = 140) {
  return e.length > t ? `${e.slice(0, t).trimEnd()}...` : e;
}
function C(e) {
  if (typeof e == "string")
    try {
      return JSON.stringify(JSON.parse(e), null, 2);
    } catch {
      return e;
    }
  return JSON.stringify(e, null, 2) ?? "null";
}
function P(e) {
  if (typeof e == "string") {
    try {
      const t = JSON.parse(e);
      if (typeof t == "object" && t !== null && !Array.isArray(t))
        return t;
    } catch {
      return { value: e };
    }
    return { value: e };
  }
  return typeof e == "object" && e !== null && !Array.isArray(e) ? e : { value: e };
}
function W(e) {
  if (typeof e == "string")
    return L(e.replace(/\s+/g, " "));
  if (typeof e == "number" || typeof e == "boolean" || e === null || e === void 0)
    return String(e);
  if (Array.isArray(e))
    return `${e.length} item${e.length === 1 ? "" : "s"}`;
  if (typeof e == "object") {
    const t = Object.keys(e).length;
    return `${t} field${t === 1 ? "" : "s"}`;
  }
  return String(e);
}
function _(e) {
  return typeof e == "string" && e.length > 180 || Array.isArray(e) || typeof e == "object" && e !== null;
}
function x({
  activeView: e,
  panelView: t,
  children: o,
  lazy: a = !1
}) {
  const s = e === t, [d, i] = l.useState(s || !a);
  return l.useEffect(() => {
    s && i(!0);
  }, [s]), d ? /* @__PURE__ */ r("div", { className: c(!s && "hidden"), children: o }) : null;
}
function q({ label: e, value: t }) {
  const [, o] = l.useTransition(), [a, s] = l.useState(!1), d = _(t);
  return /* @__PURE__ */ n("div", { className: "grid gap-1 px-3 py-2.5 sm:grid-cols-[minmax(0,8rem)_minmax(0,1fr)] sm:gap-3", children: [
    /* @__PURE__ */ r("dt", { className: "font-medium text-[11px] text-muted-foreground uppercase tracking-[0.08em]", children: e }),
    /* @__PURE__ */ n("dd", { className: "min-w-0 space-y-2 text-foreground/90 text-xs", children: [
      /* @__PURE__ */ r("span", { className: "wrap-break-word block whitespace-pre-wrap", children: W(t) }),
      d && /* @__PURE__ */ r(
        "button",
        {
          className: "inline-flex items-center rounded-md border border-border/60 px-2 py-1 font-medium text-[11px] text-muted-foreground transition-colors hover:bg-muted/40 hover:text-foreground dark:border-white/10",
          onClick: () => o(() => s((i) => !i)),
          type: "button",
          children: a ? "Show less" : "Show more"
        }
      ),
      a && /* @__PURE__ */ r(
        v,
        {
          className: "rounded-lg border border-border/60 bg-background/80 dark:border-white/10",
          code: C(t)
        }
      )
    ] })
  ] });
}
const G = l.memo(function({ value: t }) {
  const o = l.useMemo(
    () => Object.entries(P(t)),
    [t]
  );
  return /* @__PURE__ */ r("div", { className: "overflow-hidden rounded-lg border border-border/60 bg-background/80 dark:border-white/10", children: /* @__PURE__ */ r("dl", { className: "divide-y divide-border/50 dark:divide-white/10", children: o.map(([a, s]) => /* @__PURE__ */ r(q, { label: a, value: s }, a)) }) });
}), N = l.memo(function({
  label: t,
  value: o,
  view: a = "table",
  className: s,
  ...d
}) {
  const i = l.useMemo(() => C(o), [o]);
  return /* @__PURE__ */ n("section", { className: c("space-y-2", s), ...d, children: [
    /* @__PURE__ */ n("div", { className: "flex items-center justify-between gap-3", children: [
      /* @__PURE__ */ r("h4", { className: "font-medium text-foreground text-xs", children: t }),
      /* @__PURE__ */ r(
        k,
        {
          className: "size-7 shrink-0 text-muted-foreground hover:text-foreground",
          content: i,
          size: "icon",
          variant: "ghost"
        }
      )
    ] }),
    /* @__PURE__ */ r(x, { activeView: a, panelView: "table", children: /* @__PURE__ */ r(G, { value: o }) }),
    /* @__PURE__ */ r(x, { activeView: a, lazy: !0, panelView: "code", children: /* @__PURE__ */ r(
      v,
      {
        className: "rounded-lg border border-border/60 bg-background/80 dark:border-white/10",
        code: i
      }
    ) })
  ] });
});
function K({
  title: e,
  description: t,
  details: o
}) {
  return /* @__PURE__ */ n(
    D,
    {
      className: "rounded-lg border border-destructive/25 bg-destructive/[0.04] dark:border-destructive/20 dark:bg-destructive/[0.06]",
      "data-slot": "tool-call-error-notice",
      disabled: !o,
      children: [
        /* @__PURE__ */ n(
          H,
          {
            className: c(
              "group flex w-full items-start gap-2.5 px-3 py-2.5 text-left",
              o && "cursor-pointer"
            ),
            children: [
              /* @__PURE__ */ r(w, { className: "mt-0.5 size-3.5 shrink-0 text-destructive" }),
              /* @__PURE__ */ n("div", { className: "min-w-0 flex-1 space-y-0.5", children: [
                /* @__PURE__ */ r("p", { className: "font-medium text-foreground text-sm", children: e }),
                /* @__PURE__ */ r("p", { className: "text-muted-foreground text-xs", children: t })
              ] }),
              o && /* @__PURE__ */ n(S, { children: [
                /* @__PURE__ */ n("span", { className: "mt-0.5 inline-flex shrink-0 items-center gap-1 text-[11px] text-muted-foreground group-data-[state=open]:hidden", children: [
                  "Details",
                  /* @__PURE__ */ r(h, { className: "size-3" })
                ] }),
                /* @__PURE__ */ n("span", { className: "mt-0.5 hidden shrink-0 items-center gap-1 text-[11px] text-muted-foreground group-data-[state=open]:inline-flex", children: [
                  "Hide",
                  /* @__PURE__ */ r(h, { className: "size-3 rotate-90" })
                ] })
              ] })
            ]
          }
        ),
        o && /* @__PURE__ */ r(F, { children: /* @__PURE__ */ n("div", { className: "relative rounded-b-lg border-border/50 border-t bg-card px-3 py-2.5 dark:border-white/10", children: [
          /* @__PURE__ */ r(
            k,
            {
              className: "absolute top-2 right-2 size-6 text-muted-foreground/60 hover:text-muted-foreground",
              content: o,
              size: "icon",
              variant: "ghost"
            }
          ),
          /* @__PURE__ */ r("pre", { className: "wrap-break-word overflow-auto whitespace-pre-wrap pr-8 font-mono text-[11px] text-muted-foreground leading-relaxed", children: o })
        ] }) })
      ]
    }
  );
}
function oe({ className: e, ...t }) {
  return /* @__PURE__ */ r(
    O,
    {
      className: c("w-full", e),
      "data-slot": "tool-call-accordion",
      ...t
    }
  );
}
function ne({
  className: e,
  ...t
}) {
  return /* @__PURE__ */ r(
    J,
    {
      className: c(
        "w-full overflow-hidden rounded-xl border border-border/50 bg-card/75 shadow-sm last:border-b dark:border-white/10 dark:shadow-black/20",
        e
      ),
      "data-slot": "tool-call-accordion-item",
      ...t
    }
  );
}
function ie({
  className: e,
  state: t,
  toolName: o,
  icon: a,
  showStateIndicator: s = !0,
  ...d
}) {
  const i = t ?? "calling", m = a ?? /* @__PURE__ */ r(j, { className: "size-3.5 text-muted-foreground" }), g = {
    calling: /* @__PURE__ */ r(E, { className: "size-3 shrink-0 animate-spin text-orange-500" }),
    complete: /* @__PURE__ */ r(I, { className: "size-3 shrink-0 text-green-500" }),
    error: /* @__PURE__ */ r(w, { className: "size-3 shrink-0 text-destructive" })
  }, f = {
    calling: "Running",
    complete: "Completed",
    error: "Failed"
  };
  return /* @__PURE__ */ n(
    $,
    {
      className: c(
        "items-center gap-3 px-3 py-3 hover:bg-muted/15 hover:no-underline [&>svg]:translate-y-0 [&>svg]:self-center",
        M({ state: i }),
        e
      ),
      "data-slot": "tool-call-accordion-trigger",
      ...d,
      children: [
        /* @__PURE__ */ n("div", { className: "relative shrink-0", children: [
          /* @__PURE__ */ r("div", { className: "flex size-8 items-center justify-center rounded-full bg-muted/45 ring-1 ring-border/50", children: m }),
          s && /* @__PURE__ */ r(
            "span",
            {
              "aria-hidden": "true",
              className: B({
                state: i
              })
            }
          )
        ] }),
        /* @__PURE__ */ r("div", { className: "min-w-0 flex-1 text-left", children: /* @__PURE__ */ r("span", { className: "block truncate text-foreground text-xs", children: o }) }),
        /* @__PURE__ */ n(
          "span",
          {
            className: c(
              "inline-flex shrink-0 items-center gap-1.5 self-center text-[11px] text-muted-foreground leading-none",
              i === "error" && "text-destructive"
            ),
            children: [
              g[i],
              f[i]
            ]
          }
        )
      ]
    }
  );
}
function ae({
  className: e,
  children: t,
  view: o,
  onViewChange: a,
  showViewToggle: s = !0,
  args: d,
  result: i,
  errorNotice: m,
  ...g
}) {
  const [f, T] = l.useState("table"), [, A] = l.useTransition(), u = o ?? f, p = (b) => {
    a?.(b), o === void 0 && A(() => T(b));
  };
  return /* @__PURE__ */ r(
    R,
    {
      className: "w-full border-border/50 border-t bg-muted/10 dark:border-white/10",
      "data-slot": "tool-call-accordion-content",
      ...g,
      children: /* @__PURE__ */ n("div", { className: c("space-y-3 overflow-auto px-3 py-3", e), children: [
        s && /* @__PURE__ */ n("div", { className: "flex items-center justify-between gap-3", children: [
          /* @__PURE__ */ r("p", { className: "truncate text-[11px] text-muted-foreground", children: "Inspect payload and response" }),
          /* @__PURE__ */ n("div", { className: "inline-flex shrink-0 items-center gap-0.5 rounded-lg border border-border/60 bg-background/70 p-0.5 shadow-sm dark:border-white/10", children: [
            /* @__PURE__ */ n(
              "button",
              {
                className: c(
                  "inline-flex h-6.5 items-center justify-center gap-1.5 rounded-md px-2.5 text-[11px] transition-colors",
                  u === "table" ? "bg-background text-foreground shadow-sm ring-1 ring-border/60" : "text-muted-foreground hover:bg-background/60 hover:text-foreground"
                ),
                onClick: () => p("table"),
                type: "button",
                children: [
                  /* @__PURE__ */ r(z, { className: "size-3.5" }),
                  "Table"
                ]
              }
            ),
            /* @__PURE__ */ n(
              "button",
              {
                className: c(
                  "inline-flex h-6.5 items-center justify-center gap-1.5 rounded-md px-2.5 text-[11px] transition-colors",
                  u === "code" ? "bg-background text-foreground shadow-sm ring-1 ring-border/60" : "text-muted-foreground hover:bg-background/60 hover:text-foreground"
                ),
                onClick: () => p("code"),
                type: "button",
                children: [
                  /* @__PURE__ */ r(V, { className: "size-3.5" }),
                  "JSON"
                ]
              }
            )
          ] })
        ] }),
        /* @__PURE__ */ n("div", { className: "space-y-3", children: [
          t,
          m && /* @__PURE__ */ r(K, { ...m }),
          d !== void 0 && /* @__PURE__ */ r(N, { label: "Parameters", value: d, view: u }),
          i !== void 0 && /* @__PURE__ */ r(N, { label: "Response", value: i, view: u })
        ] })
      ] })
    }
  );
}
export {
  oe as ToolCallAccordion,
  ae as ToolCallAccordionContent,
  ne as ToolCallAccordionItem,
  ie as ToolCallAccordionTrigger
};
