import { jsx as r, jsxs as a, Fragment as v } from "react/jsx-runtime";
/* empty css                       */
import k from "react-markdown";
import { memo as z, useMemo as T, createContext as f, useContext as b, useState as y } from "react";
import { CopyIcon as M, CheckIcon as $, ChevronLeft as S, ChevronRight as I } from "lucide-react";
import { HoverCard as R, HoverCardTrigger as U, HoverCardContent as L } from "./hover-card.es.js";
/* empty css                                                                              */
import { cn as o } from "../../utils/tailwindUtil.es.js";
import { SyntaxHighlighter as P } from "./syntax-highlighter.es.js";
import { TooltipIconButton as j } from "./tooltip-icon-button.es.js";
import G from "../../node_modules/.pnpm/rehype-katex@7.0.1/node_modules/rehype-katex/lib/index.es.js";
import H from "../../node_modules/.pnpm/remark-gfm@4.0.1/node_modules/remark-gfm/lib/index.es.js";
import D from "../../node_modules/.pnpm/remark-math@6.0.0/node_modules/remark-math/lib/index.es.js";
const w = f(/* @__PURE__ */ new Map()), N = f(void 0);
function _(t) {
  try {
    const s = new URL(t).pathname.replace(/\/$/, "");
    if (!s || s === "/") return "";
    const l = s.split("/").filter(Boolean).pop() ?? "";
    return decodeURIComponent(l).replace(/[-_]/g, " ").replace(/\.\w+$/, "").replace(/\b\w/g, (n) => n.toUpperCase());
  } catch {
    return "";
  }
}
function q({
  href: t,
  domain: e,
  displayLabel: s,
  titleText: l,
  className: n,
  ...c
}) {
  const i = b(w).get(e) ?? [{ url: t, title: l }], m = i.length - 1, [d, g] = y(0), x = i[d] ?? i[0], C = x.title && x.title !== e ? x.title : _(x.url) || e, h = (() => {
    try {
      const p = new URL(x.url);
      return p.hostname + p.pathname;
    } catch {
      return "";
    }
  })();
  return /* @__PURE__ */ a(R, { openDelay: 200, closeDelay: 100, children: [
    /* @__PURE__ */ r(U, { asChild: !0, children: /* @__PURE__ */ a(
      "a",
      {
        className: o(
          "inline-flex items-center gap-1.5 rounded-md border border-border/30 bg-muted px-2 py-0.5 text-xs font-medium text-foreground/80 no-underline transition-colors hover:bg-muted/70",
          n
        ),
        href: t,
        target: "_blank",
        rel: "noopener noreferrer",
        ...c,
        children: [
          /* @__PURE__ */ r(
            "img",
            {
              src: `https://www.google.com/s2/favicons?domain=${e}&sz=32`,
              alt: "",
              className: "size-3.5 shrink-0 rounded-sm",
              loading: "lazy"
            }
          ),
          /* @__PURE__ */ r("span", { className: "max-w-[140px] truncate", children: s }),
          m > 0 && /* @__PURE__ */ a("span", { className: "ml-0.5 rounded-full bg-muted-foreground/20 px-1.5 py-px text-[10px] font-semibold text-muted-foreground", children: [
            "+",
            m
          ] })
        ]
      }
    ) }),
    /* @__PURE__ */ a(L, { side: "top", align: "start", className: "w-[320px] p-0", children: [
      m > 0 && /* @__PURE__ */ a("div", { className: "flex items-center justify-between border-b border-border/40 px-3 py-2", children: [
        /* @__PURE__ */ a("div", { className: "flex items-center gap-1.5", children: [
          /* @__PURE__ */ r(
            "button",
            {
              type: "button",
              onClick: () => g((p) => Math.max(0, p - 1)),
              disabled: d === 0,
              className: "flex size-5 items-center justify-center rounded text-muted-foreground transition-colors hover:text-foreground disabled:opacity-30",
              children: /* @__PURE__ */ r(S, { className: "size-3.5" })
            }
          ),
          /* @__PURE__ */ a("span", { className: "text-xs tabular-nums text-muted-foreground", children: [
            d + 1,
            "/",
            i.length
          ] }),
          /* @__PURE__ */ r(
            "button",
            {
              type: "button",
              onClick: () => g((p) => Math.min(i.length - 1, p + 1)),
              disabled: d === i.length - 1,
              className: "flex size-5 items-center justify-center rounded text-muted-foreground transition-colors hover:text-foreground disabled:opacity-30",
              children: /* @__PURE__ */ r(I, { className: "size-3.5" })
            }
          )
        ] }),
        /* @__PURE__ */ a("div", { className: "flex items-center gap-1.5", children: [
          i.slice(0, 2).map((p) => /* @__PURE__ */ r(
            "img",
            {
              src: `https://www.google.com/s2/favicons?domain=${e}&sz=32`,
              alt: "",
              className: "size-3.5 rounded-sm",
              loading: "lazy"
            },
            p.url
          )),
          /* @__PURE__ */ a("span", { className: "text-xs text-muted-foreground", children: [
            i.length,
            " sources"
          ] })
        ] })
      ] }),
      /* @__PURE__ */ a("div", { className: "flex flex-col gap-1.5 p-3", children: [
        /* @__PURE__ */ a("div", { className: "flex items-center gap-2", children: [
          /* @__PURE__ */ r(
            "img",
            {
              src: `https://www.google.com/s2/favicons?domain=${e}&sz=32`,
              alt: "",
              className: "size-4 shrink-0 rounded-sm",
              loading: "lazy"
            }
          ),
          /* @__PURE__ */ r("span", { className: "text-xs text-muted-foreground", children: e })
        ] }),
        /* @__PURE__ */ r(
          "a",
          {
            href: x.url,
            target: "_blank",
            rel: "noopener noreferrer",
            className: "line-clamp-2 text-sm font-semibold leading-snug text-foreground no-underline transition-colors hover:text-teal-700 dark:hover:text-teal-400",
            children: C
          }
        ),
        h && /* @__PURE__ */ r("p", { className: "line-clamp-1 text-xs text-muted-foreground/60", children: h })
      ] })
    ] })
  ] });
}
const A = ({
  copiedDuration: t = 3e3
} = {}) => {
  const [e, s] = y(!1);
  return { isCopied: e, copyToClipboard: (n) => {
    n && navigator.clipboard.writeText(n).then(() => {
      s(!0), setTimeout(() => s(!1), t);
    });
  } };
}, B = ({ language: t, code: e }) => {
  const { isCopied: s, copyToClipboard: l } = A();
  return /* @__PURE__ */ a("div", { className: "flex items-center justify-between gap-4 rounded-t-lg bg-zinc-900 px-4 py-2 text-sm font-semibold text-white", children: [
    /* @__PURE__ */ r("span", { className: "lowercase [&>span]:text-xs", children: t }),
    /* @__PURE__ */ a(
      j,
      {
        tooltip: "Copy",
        onClick: () => {
          !e || s || l(e);
        },
        children: [
          !s && /* @__PURE__ */ r(M, {}),
          s && /* @__PURE__ */ r($, {})
        ]
      }
    )
  ] });
}, F = {
  h1: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "h1",
    {
      className: o(
        "mb-8 scroll-m-20 text-base font-extrabold tracking-tight sm:text-xl md:text-2xl last:mb-0",
        t
      ),
      ...e
    }
  ),
  h2: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "h2",
    {
      className: o(
        "mt-8 mb-4 scroll-m-20 text-sm font-semibold tracking-tight sm:text-base md:text-xl first:mt-0 last:mb-0",
        t
      ),
      ...e
    }
  ),
  h3: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "h3",
    {
      className: o(
        "mt-6 mb-4 scroll-m-20 text-xs font-semibold tracking-tight sm:text-sm md:text-base first:mt-0 last:mb-0",
        t
      ),
      ...e
    }
  ),
  h4: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "h4",
    {
      className: o(
        "mt-6 mb-4 scroll-m-20 text-xs font-semibold tracking-tight sm:text-xs md:text-sm first:mt-0 last:mb-0",
        t
      ),
      ...e
    }
  ),
  h5: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "h5",
    {
      className: o(
        "my-4 text-xs font-semibold sm:text-xs md:text-xs first:mt-0 last:mb-0",
        t
      ),
      ...e
    }
  ),
  h6: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "h6",
    {
      className: o("my-4 text-xs font-semibold sm:text-xs md:text-xs first:mt-0 last:mb-0", t),
      ...e
    }
  ),
  p: ({ className: t, ...e }) => {
    const s = b(N);
    return /* @__PURE__ */ r(
      "p",
      {
        className: o("mt-5 mb-5 leading-7 first:mt-0 last:mb-0", !s && "text-sm sm:text-base", t),
        style: s ? { fontSize: s } : void 0,
        ...e
      }
    );
  },
  a: ({
    className: t,
    href: e,
    children: s,
    ...l
  }) => {
    let n = "";
    const c = e ?? "";
    try {
      n = new URL(c).hostname.replace(/^www\./, "");
    } catch {
      n = String(s ?? e ?? "");
    }
    const u = n || String(s ?? ""), i = String(s ?? n);
    return /* @__PURE__ */ r(
      q,
      {
        href: c,
        domain: n,
        displayLabel: u,
        titleText: i,
        className: t,
        ...l
      }
    );
  },
  blockquote: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "blockquote",
    {
      className: o("border-l-2 pl-6 italic", t),
      ...e
    }
  ),
  ul: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "ul",
    {
      className: o("my-5 ml-6 list-disc [&>li]:mt-2", t),
      ...e
    }
  ),
  ol: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "ol",
    {
      className: o("my-5 ml-6 list-decimal [&>li]:mt-2", t),
      ...e
    }
  ),
  hr: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "hr",
    {
      className: o("my-5 border-b", t),
      ...e
    }
  ),
  table: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "table",
    {
      className: o(
        "my-5 w-full border-separate border-spacing-0 overflow-y-auto",
        t
      ),
      ...e
    }
  ),
  th: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "th",
    {
      className: o(
        "bg-muted px-4 py-2 text-left font-bold first:rounded-tl-lg last:rounded-tr-lg [&[align=center]]:text-center [&[align=right]]:text-right",
        t
      ),
      ...e
    }
  ),
  td: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "td",
    {
      className: o(
        "border-b border-l px-4 py-2 text-left last:border-r [&[align=center]]:text-center [&[align=right]]:text-right",
        t
      ),
      ...e
    }
  ),
  tr: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "tr",
    {
      className: o(
        "m-0 border-b p-0 first:border-t [&:last-child>td:first-child]:rounded-bl-lg [&:last-child>td:last-child]:rounded-br-lg",
        t
      ),
      ...e
    }
  ),
  sup: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "sup",
    {
      className: o("[&>a]:text-xs [&>a]:no-underline", t),
      ...e
    }
  ),
  pre: ({ className: t, ...e }) => /* @__PURE__ */ r(
    "pre",
    {
      className: o(
        "max-w-4xl overflow-x-auto rounded-lg ",
        t
      ),
      ...e
    }
  ),
  code: ({
    className: t,
    children: e,
    ...s
  }) => {
    const l = /language-(\w+)/.exec(t || "");
    if (l) {
      const n = l[1], c = String(e).replace(/\n$/, "");
      return /* @__PURE__ */ a(v, { children: [
        /* @__PURE__ */ r(
          B,
          {
            language: n,
            code: c
          }
        ),
        /* @__PURE__ */ r(
          P,
          {
            language: n,
            className: t,
            children: c
          }
        )
      ] });
    }
    return /* @__PURE__ */ r(
      "code",
      {
        className: o("rounded font-semibold", t),
        ...s,
        children: e
      }
    );
  }
};
function K(t) {
  let e = t.replace(/([^\n])(#{1,6}\s)/g, `$1

$2`);
  return e = e.replace(/\(\[([^\]]+)\]\((https?:\/\/[^)]+)\)\)/g, "[$1]($2)"), e;
}
const E = ({ children: t, fontSize: e }) => {
  const s = T(() => {
    const l = /* @__PURE__ */ new Map(), n = /\[([^\]]*)\]\((https?:\/\/[^)]+)\)/g, c = /* @__PURE__ */ new Set();
    let u;
    for (; (u = n.exec(t)) !== null; ) {
      const i = u[1], m = u[2];
      if (c.has(m)) continue;
      c.add(m);
      let d;
      try {
        d = new URL(m).hostname.replace(/^www\./, "");
      } catch {
        d = m;
      }
      const g = l.get(d) ?? [];
      g.push({ url: m, title: i }), l.set(d, g);
    }
    return l;
  }, [t]);
  return /* @__PURE__ */ r(N.Provider, { value: e, children: /* @__PURE__ */ r(w.Provider, { value: s, children: /* @__PURE__ */ r("div", { className: "markdown-content", children: /* @__PURE__ */ r(
    k,
    {
      remarkPlugins: [H, D],
      rehypePlugins: [G],
      components: F,
      children: K(t)
    }
  ) }) }) });
}, ne = z(E);
export {
  ne as AgentMarkdown
};
//# sourceMappingURL=AgentMarkdown.es.js.map
