import { T as T$1, p as po, f as f$1, C as Cn, F as Ft, a as F$1 } from "./index-P1RimRZR.js";
import { useContext, useMemo, useState, useEffect, memo } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { c as createHighlighterCore, a as createJavaScriptRegexEngine } from "./engine-compile-a_vH22t2.js";
import q from "./github-dark-Db8tlBd1.js";
import Q from "./github-light-DI9P3GPc.js";
var _ = f$1("block", "before:content-[counter(line)]", "before:inline-block", "before:[counter-increment:line]", "before:w-6", "before:mr-4", "before:text-[13px]", "before:text-right", "before:text-muted-foreground/50", "before:font-mono", "before:select-none"), H = memo(({ children: e, result: t, language: o, className: l, ...a$1 }) => {
  let n = useMemo(() => ({ backgroundColor: t.bg, color: t.fg }), [t.bg, t.fg]);
  return jsx("pre", { className: f$1(l, "p-4 text-sm dark:bg-(--shiki-dark-bg)!"), "data-language": o, "data-streamdown": "code-block-body", style: n, ...a$1, children: jsx("code", { className: "[counter-increment:line_0] [counter-reset:line]", children: t.tokens.map((r, i) => jsx("span", { className: _, children: r.map((g, s) => jsx("span", { className: "dark:bg-(--shiki-dark-bg)! dark:text-(--shiki-dark)!", style: { color: g.color, backgroundColor: g.bgColor, ...g.htmlStyle }, ...g.htmlAttrs, children: g.content }, s)) }, i)) }) });
}, (e, t) => e.result === t.result && e.language === t.language && e.className === t.className);
var M = ({ className: e, language: t, style: o, ...l }) => jsx("div", { className: f$1("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", ...o }, ...l });
var F = ({ language: e, children: t }) => jsxs("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: [jsx("span", { className: "ml-1 font-mono lowercase", children: e }), jsx("div", { className: "flex items-center gap-2", children: t })] });
var p = { "github-light": Q, "github-dark": q }, D = (e) => e in p;
var W = F$1.dependencies.shiki.replace(/^\^/, ""), A = 5e3, K = (e) => `${e}/shiki/${W}`, f = /* @__PURE__ */ new Map(), C = /* @__PURE__ */ new Map(), y = /* @__PURE__ */ new Set(), O = /* @__PURE__ */ new Set(), J = /JSON\.parse\(("(?:[^"\\]|\\.)*")\)/, I = /import\s+\w+\s+from\s+['"]\.\/([\w-]+)\.mjs['"]/g, T = /* @__PURE__ */ new Set();
async function X(e, t, o) {
  let l = `${t}/${e}.mjs`, a = new AbortController(), n = setTimeout(() => a.abort(), o), r = await fetch(l, { signal: a.signal });
  if (clearTimeout(n), !r.ok) throw new Error(`HTTP ${r.status}: ${r.statusText}`);
  let i = await r.text(), g = [], s;
  for (; (s = I.exec(i)) !== null; ) g.push(s[1]);
  I.lastIndex = 0;
  let d = i.match(J);
  if (!d) throw new Error("Could not find JSON.parse() in CDN response");
  let c = JSON.parse(d[1]);
  return { grammar: JSON.parse(c), dependencies: g };
}
async function w(e, t, o = A) {
  if (t == null) return null;
  let a = `${K(t)}/langs`, n = `${a}/${e}`;
  if (f.has(n)) return f.get(n);
  if (y.has(n) || T.has(n)) return null;
  try {
    T.add(n);
    let r = await X(e, a, o);
    if (!r) throw new Error("Failed to load language");
    let { grammar: i, dependencies: g } = r, s = [];
    for (let d of g) {
      let c = `${a}/${d}`;
      if (f.has(c)) {
        let k = f.get(c);
        s.push(...k);
        continue;
      }
      if (T.has(c) || y.has(c)) continue;
      let m = await w(d, t, o);
      m && s.push(...m);
    }
    return s.push(i), f.set(n, [i]), s;
  } catch (r) {
    y.add(n);
    let i = r instanceof Error ? r.message : "Unknown error";
    return console.warn(`[Streamdown] Failed to load language "${e}" from CDN: ${i}`), null;
  } finally {
    T.delete(n);
  }
}
async function j(e, t, o = A) {
  if (t == null) return null;
  let a = `${K(t)}/themes`, n = `${a}/${e}`;
  if (C.has(n)) return C.get(n);
  if (O.has(n)) return null;
  try {
    let r = `${a}/${e}.mjs`, i = new AbortController(), g = setTimeout(() => i.abort(), o), s = await fetch(r, { signal: i.signal });
    if (clearTimeout(g), !s.ok) throw new Error(`HTTP ${s.status}: ${s.statusText}`);
    let d = await s.text();
    try {
      let c = d.match(J);
      if (!c) throw new Error("Could not find JSON.parse() in CDN response");
      let m = JSON.parse(c[1]), k = JSON.parse(m);
      return C.set(n, k), k;
    } catch (c) {
      throw new Error(`Failed to parse theme: ${c instanceof Error ? c.message : "Unknown error"}`);
    }
  } catch (r) {
    O.add(n);
    let i = r instanceof Error ? r.message : "Unknown error";
    return console.warn(`[Streamdown] Failed to load theme "${e}" from CDN: ${i}`), null;
  }
}
var ee = createJavaScriptRegexEngine({ forgiving: true }), R = /* @__PURE__ */ new Map(), B = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map(), te = (e, t) => `${e}-${t[0]}-${t[1]}`, ne = (e, t, o) => {
  let l = e.slice(0, 100), a = e.length > 100 ? e.slice(-100) : "";
  return `${t}:${o[0]}:${o[1]}:${e.length}:${l}:${a}`;
};
async function oe(e, t) {
  if (D(e)) return p[e];
  let o = await j(e, t);
  return o || (console.warn(`[Streamdown] Theme "${e}" not found. Falling back to ${e.includes("dark") ? "github-dark" : "github-light"}.`), e.includes("dark") ? p["github-dark"] : p["github-light"]);
}
async function re(e$1, t) {
  if (Cn(e$1)) return Ft[e$1];
  let o = await w(e$1, t);
  return o || console.warn(`[Streamdown] Language "${e$1}" not found in bundled languages or CDN. Falling back to plain text.`), o;
}
var se = (e, t, o) => {
  let l = te(e, t);
  if (R.has(l)) return R.get(l);
  let a = (async () => {
    let n = await re(e, o), r = n ? [n] : ["text"], i = await Promise.all(t.map((s) => oe(s, o)));
    return await createHighlighterCore({ themes: i, langs: r, engine: ee });
  })();
  return R.set(l, a), a;
}, x = ({ code: e, language: t, shikiTheme: o, cdnUrl: l, callback: a }) => {
  let n = ne(e, t, o);
  return B.has(n) ? B.get(n) : (a && (u.has(n) || u.set(n, /* @__PURE__ */ new Set()), u.get(n).add(a)), se(t, o, l).then((r) => {
    let g = r.getLoadedLanguages().includes(t) ? t : "text", s = r.codeToTokens(e, { lang: g, themes: { light: o[0], dark: o[1] } });
    B.set(n, s);
    let d = u.get(n);
    if (d) {
      for (let c of d) c(s);
      u.delete(n);
    }
  }).catch((r) => {
    console.error("Failed to highlight code:", r), u.delete(n);
  }), null);
};
var Ie = ({ code: e, language: t, className: o, children: l, ...a }) => {
  let { shikiTheme: n, cdnUrl: r } = useContext(T$1), i = useMemo(() => ({ bg: "transparent", fg: "inherit", tokens: e.split(`
`).map((d) => [{ content: d, color: "inherit", bgColor: "transparent", htmlStyle: {}, offset: 0 }]) }), [e]), [g, s] = useState(i);
  return useEffect(() => {
    let d = x({ code: e, language: t, shikiTheme: n, cdnUrl: r });
    if (d) {
      s(d);
      return;
    }
    x({ code: e, language: t, shikiTheme: n, cdnUrl: r, callback: (c) => {
      s(c);
    } });
  }, [e, t, n, r]), jsx(po.Provider, { value: { code: e }, children: jsxs(M, { language: t, children: [jsx(F, { language: t, children: l }), jsx(H, { className: o, language: t, result: g, ...a })] }) });
};
export {
  Ie as CodeBlock
};
//# sourceMappingURL=code-block-545L3TSB-BdFFFRHc.js.map
