import { jsx as r, jsxs as d } from "react/jsx-runtime";
import { useState as l, useEffect as S, useCallback as _ } from "react";
import z from "react-markdown";
import H from "remark-gfm";
import M from "remark-math";
import j from "rehype-katex";
import T from "rehype-raw";
import { Check as y, Copy as N } from "lucide-react";
import { u as c, h as B } from "./index-Bk6WqXgW.mjs";
import { u as w } from "./useShikiHighlight-CRlVgtPy.mjs";
import "katex/dist/katex.min.css";
const v = (t) => {
  const [p, f] = l(!1), a = _(async () => {
    try {
      await navigator.clipboard.writeText(t);
    } catch {
      const s = document.createElement("textarea");
      s.value = t, document.body.appendChild(s), s.select(), document.execCommand("copy"), document.body.removeChild(s);
    }
    f(!0), setTimeout(() => f(!1), 2e3);
  }, [t]);
  return { copied: p, handleCopy: a };
}, E = ({ text: t }) => {
  const p = c(), { copied: f, handleCopy: a } = v(t);
  return /* @__PURE__ */ r(
    "button",
    {
      onClick: a,
      className: "rfp-p-1 rfp-rounded rfp-text-fg-muted hover:rfp-text-fg-secondary rfp-transition-colors rfp-flex rfp-items-center rfp-gap-1",
      title: p(f ? "markdown.copied" : "markdown.copy_code"),
      children: f ? /* @__PURE__ */ r(y, { size: 13 }) : /* @__PURE__ */ r(N, { size: 13 })
    }
  );
}, I = ({ text: t }) => {
  const p = c(), { copied: f, handleCopy: a } = v(t);
  return /* @__PURE__ */ r(
    "button",
    {
      onClick: a,
      className: "rfp-absolute rfp-top-2 rfp-right-2 rfp-p-1.5 rfp-rounded-md rfp-bg-surface-2 hover:rfp-bg-surface-3 rfp-text-fg-tertiary hover:rfp-text-fg-secondary rfp-transition-colors rfp-opacity-0 group-hover:rfp-opacity-100 rfp-border rfp-border-line",
      title: p(f ? "markdown.copied" : "markdown.copy_code"),
      children: f ? /* @__PURE__ */ r(y, { size: 14 }) : /* @__PURE__ */ r(N, { size: 14 })
    }
  );
}, L = ({ code: t, lang: p }) => {
  const { html: f } = w(t, p);
  return /* @__PURE__ */ d("div", { className: "rfp-relative rfp-group rfp-my-4", children: [
    /* @__PURE__ */ d("div", { className: "rfp-flex rfp-items-center rfp-justify-between rfp-px-4 rfp-py-1.5 rfp-bg-surface-1 rfp-border rfp-border-line-weak rfp-rounded-t-md rfp-border-b-0", children: [
      /* @__PURE__ */ r("span", { className: "rfp-text-xs rfp-text-fg-secondary rfp-font-mono rfp-select-none", children: p }),
      /* @__PURE__ */ r(E, { text: t })
    ] }),
    f ? /* @__PURE__ */ r(
      "div",
      {
        className: "rfp-shiki-wrapper rfp-rounded-b-md rfp-border rfp-border-line-weak rfp-border-t-0 rfp-overflow-x-auto",
        dangerouslySetInnerHTML: { __html: f }
      }
    ) : /* @__PURE__ */ r(
      "pre",
      {
        className: "rfp-m-0 rfp-rounded-b-md rfp-border rfp-border-line-weak rfp-border-t-0 rfp-overflow-x-auto rfp-p-4 rfp-bg-code-bg",
        style: { fontSize: "13px", lineHeight: "1.5" },
        children: /* @__PURE__ */ r("code", { className: "rfp-font-mono rfp-text-code-fg rfp-text-sm", children: t })
      }
    )
  ] });
}, Q = ({ url: t, viewMode: p = "preview" }) => {
  const f = c(), [a, s] = l(""), [k, m] = l(!0), [u, h] = l(null), { html: x } = w(
    p === "source" ? a : "",
    "markdown"
  );
  return S(() => {
    (async () => {
      try {
        m(!0), h(null);
        const o = await B(t);
        s(o);
      } catch (o) {
        h(f("markdown.load_failed")), console.error(o);
      } finally {
        m(!1);
      }
    })();
  }, [t]), k ? /* @__PURE__ */ r("div", { className: "rfp-flex rfp-items-center rfp-justify-center rfp-w-full rfp-h-full", children: /* @__PURE__ */ r("div", { className: "rfp-w-12 rfp-h-12 rfp-border-4 rfp-border-line-strong rfp-border-t-spinner-head rfp-rounded-full rfp-animate-spin" }) }) : u ? /* @__PURE__ */ r("div", { className: "rfp-flex rfp-items-center rfp-justify-center rfp-w-full rfp-h-full", children: /* @__PURE__ */ r("div", { className: "rfp-text-fg-secondary rfp-text-center", children: /* @__PURE__ */ r("p", { className: "rfp-text-lg", children: u }) }) }) : p === "source" ? /* @__PURE__ */ r("div", { className: "rfp-w-full rfp-h-full rfp-overflow-auto rfp-bg-code-bg", children: x ? /* @__PURE__ */ r(
    "div",
    {
      className: "rfp-shiki-wrapper with-line-numbers",
      dangerouslySetInnerHTML: { __html: x }
    }
  ) : /* @__PURE__ */ r("pre", { className: "rfp-p-6 rfp-text-fg-primary rfp-font-mono rfp-text-sm rfp-whitespace-pre-wrap rfp-break-words", children: a }) }) : /* @__PURE__ */ r("div", { className: "rfp-w-full rfp-h-full rfp-overflow-auto rfp-p-6 md:rfp-p-10", children: /* @__PURE__ */ r("div", { className: "rfp-max-w-full md:rfp-max-w-4xl rfp-mx-auto", children: /* @__PURE__ */ r(
    z,
    {
      remarkPlugins: [H, M],
      rehypePlugins: [T, j],
      components: {
        code({ node: e, inline: o, className: n, children: i, ...C }) {
          const b = /language-(\w+)/.exec(n || ""), g = String(i).replace(/\n$/, "");
          return !o && b ? /* @__PURE__ */ r(L, { code: g, lang: b[1] }) : o ? /* @__PURE__ */ r(
            "code",
            {
              className: "rfp-bg-surface-2 rfp-px-1.5 rfp-py-0.5 rfp-rounded rfp-text-sm rfp-font-mono rfp-text-fg-primary rfp-border rfp-border-line-weak",
              ...C,
              children: i
            }
          ) : /* @__PURE__ */ d("div", { className: "rfp-relative rfp-group rfp-my-4", children: [
            /* @__PURE__ */ r(I, { text: g }),
            /* @__PURE__ */ r(
              "pre",
              {
                className: "rfp-m-0 rfp-rounded-md rfp-border rfp-border-line-weak rfp-overflow-x-auto rfp-p-4 rfp-bg-code-bg",
                style: { fontSize: "13px", lineHeight: "1.5" },
                children: /* @__PURE__ */ r("code", { className: "rfp-font-mono rfp-text-code-fg rfp-text-sm", children: i })
              }
            )
          ] });
        },
        h1: ({ children: e }) => /* @__PURE__ */ r("h1", { className: "rfp-text-3xl rfp-font-semibold rfp-mb-4 rfp-mt-6 rfp-text-fg-primary first:rfp-mt-0", children: e }),
        h2: ({ children: e }) => /* @__PURE__ */ r("h2", { className: "rfp-text-2xl rfp-font-semibold rfp-mb-3 rfp-mt-8 rfp-text-fg-primary", children: e }),
        h3: ({ children: e }) => /* @__PURE__ */ r("h3", { className: "rfp-text-xl rfp-font-semibold rfp-mb-2 rfp-mt-6 rfp-text-fg-primary", children: e }),
        h4: ({ children: e }) => /* @__PURE__ */ r("h4", { className: "rfp-text-lg rfp-font-semibold rfp-mb-2 rfp-mt-4 rfp-text-fg-primary", children: e }),
        p: ({ children: e }) => /* @__PURE__ */ r("p", { className: "rfp-text-fg-secondary rfp-mb-4 rfp-leading-7 rfp-text-base", children: e }),
        a: ({ href: e, children: o }) => /* @__PURE__ */ r(
          "a",
          {
            href: e,
            className: "rfp-text-indigo-400 hover:rfp-text-indigo-300 rfp-underline rfp-decoration-indigo-600 hover:rfp-decoration-indigo-400",
            target: "_blank",
            rel: "noopener noreferrer",
            children: o
          }
        ),
        ul: ({ children: e }) => /* @__PURE__ */ r("ul", { className: "rfp-list-disc rfp-pl-6 rfp-mb-4 rfp-text-fg-secondary rfp-space-y-1", children: e }),
        ol: ({ children: e }) => /* @__PURE__ */ r("ol", { className: "rfp-list-decimal rfp-pl-6 rfp-mb-4 rfp-text-fg-secondary rfp-space-y-1", children: e }),
        li: ({ children: e }) => /* @__PURE__ */ r("li", { className: "rfp-leading-7", children: e }),
        blockquote: ({ children: e }) => /* @__PURE__ */ r("blockquote", { className: "rfp-border-l-4 rfp-border-line-strong rfp-pl-4 rfp-text-fg-tertiary rfp-my-4 rfp-italic", children: e }),
        table: ({ children: e }) => /* @__PURE__ */ r("div", { className: "rfp-overflow-x-auto rfp-my-4 rfp-rounded-md rfp-border rfp-border-line", children: /* @__PURE__ */ r("table", { className: "rfp-min-w-full rfp-divide-y rfp-divide-divide", children: e }) }),
        thead: ({ children: e }) => /* @__PURE__ */ r("thead", { className: "rfp-bg-surface-1", children: e }),
        tbody: ({ children: e }) => /* @__PURE__ */ r("tbody", { className: "rfp-divide-y rfp-divide-divide rfp-bg-transparent", children: e }),
        tr: ({ children: e }) => /* @__PURE__ */ r("tr", { className: "hover:rfp-bg-surface-1 rfp-transition-colors", children: e }),
        th: ({ children: e }) => /* @__PURE__ */ r("th", { className: "rfp-px-4 rfp-py-3 rfp-text-left rfp-text-xs rfp-font-semibold rfp-text-fg-tertiary rfp-uppercase rfp-tracking-wider", children: e }),
        td: ({ children: e }) => /* @__PURE__ */ r("td", { className: "rfp-px-4 rfp-py-3 rfp-text-sm rfp-text-fg-secondary", children: e }),
        hr: () => /* @__PURE__ */ r("hr", { className: "rfp-border-line rfp-my-6" }),
        img: ({ src: e, alt: o }) => /* @__PURE__ */ r(
          "img",
          {
            src: e,
            alt: o,
            className: "rfp-rounded-md rfp-max-w-full rfp-h-auto rfp-my-4 rfp-mx-auto rfp-block rfp-shadow-sm"
          }
        ),
        input: ({ type: e, checked: o, ...n }) => e === "checkbox" ? /* @__PURE__ */ r(
          "input",
          {
            type: "checkbox",
            checked: o,
            readOnly: !0,
            className: "rfp-mr-2 rfp-rounded rfp-border-line",
            ...n
          }
        ) : /* @__PURE__ */ r("input", { type: e, ...n }),
        strong: ({ children: e }) => /* @__PURE__ */ r("strong", { className: "rfp-font-semibold rfp-text-fg-primary", children: e }),
        em: ({ children: e }) => /* @__PURE__ */ r("em", { className: "rfp-italic", children: e }),
        del: ({ children: e }) => /* @__PURE__ */ r("del", { className: "rfp-text-fg-muted rfp-line-through", children: e })
      },
      children: a
    }
  ) }) });
};
export {
  Q as MarkdownRenderer
};
//# sourceMappingURL=index-BMVH5kfz.mjs.map
