import { C as v, D as N, u as h, c as w, a as y, b as $ } from "./sdk-entry-DgIWYKoN.js";
import { useContext as C, useMemo as f, useState as S, useEffect as j, memo as M } from "react";
import { jsx as s, jsxs as p } from "react/jsx-runtime";
var T = h("block", "before:content-[counter(line)]", "before:inline-block", "before:[counter-increment:line]", "before:w-4", "before:mr-4", "before:text-[13px]", "before:text-right", "before:text-muted-foreground/50", "before:font-mono", "before:select-none"), E = M(({ children: e, result: t, language: a, className: l, ...n }) => {
  let r = f(() => ({ backgroundColor: t.bg, color: t.fg }), [t.bg, t.fg]);
  return s("pre", { className: h(l, "p-4 text-sm dark:bg-(--shiki-dark-bg)!"), "data-language": a, "data-streamdown": "code-block-body", style: r, ...n, children: s("code", { className: "[counter-increment:line_0] [counter-reset:line]", children: t.tokens.map((i, d) => s("span", { className: T, children: i.map((o, c) => s("span", { className: "dark:bg-(--shiki-dark-bg)! dark:text-(--shiki-dark)!", style: { color: o.color, backgroundColor: o.bgColor, ...o.htmlStyle }, ...o.htmlAttrs, children: o.content }, c)) }, d)) }) });
}, (e, t) => e.result === t.result && e.language === t.language && e.className === t.className), A = ({ className: e, language: t, style: a, ...l }) => s("div", { className: h("my-4 w-full overflow-hidden rounded-xl border border-border", e), "data-language": t, "data-streamdown": "code-block", style: { contentVisibility: "auto", containIntrinsicSize: "auto 200px", ...a }, ...l }), D = ({ language: e, children: t }) => p("div", { className: "flex items-center justify-between bg-muted/80 p-3 text-muted-foreground text-xs", "data-language": e, "data-streamdown": "code-block-header", children: [s("span", { className: "ml-1 font-mono lowercase", children: e }), s("div", { className: "flex items-center gap-2", children: t })] }), L = y({ forgiving: !0 }), m = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map(), g = /* @__PURE__ */ new Map(), O = (e, t) => `${e}-${t[0]}-${t[1]}`, z = (e, t, a) => {
  let l = e.slice(0, 100), n = e.length > 100 ? e.slice(-100) : "";
  return `${t}:${a[0]}:${a[1]}:${e.length}:${l}:${n}`;
}, k = (e) => Object.hasOwn($, e), B = (e, t) => {
  let a = k(e) ? e : "text", l = O(a, t);
  if (m.has(l)) return m.get(l);
  let n = w({ themes: t, langs: [a], engine: L });
  return m.set(l, n), n;
}, b = (e, t, a, l) => {
  let n = k(t) ? t : "text", r = z(e, n, a);
  return u.has(r) ? u.get(r) : (l && (g.has(r) || g.set(r, /* @__PURE__ */ new Set()), g.get(r).add(l)), B(n, a).then((i) => {
    let d = i.codeToTokens(e, { lang: n, themes: { light: a[0], dark: a[1] } });
    u.set(r, d);
    let o = g.get(r);
    if (o) {
      for (let c of o) c(d);
      g.delete(r);
    }
  }).catch((i) => {
    console.error("Failed to highlight code:", i), g.delete(r);
  }), null);
}, J = ({ code: e, language: t, className: a, children: l, ...n }) => {
  let { shikiTheme: r } = C(v), i = f(() => ({ bg: "transparent", fg: "inherit", tokens: e.split(`
`).map((c) => [{ content: c, color: "inherit", bgColor: "transparent", htmlStyle: {}, offset: 0 }]) }), [e]), [d, o] = S(i);
  return j(() => {
    let c = b(e, t, r);
    if (c) {
      o(c);
      return;
    }
    b(e, t, r, (x) => {
      o(x);
    });
  }, [e, t, r]), s(N.Provider, { value: { code: e }, children: p(A, { language: t, children: [s(D, { language: t, children: l }), s(E, { className: a, language: t, result: d, ...n })] }) });
};
export {
  J as CodeBlock
};
