"use client";
import { jsx as o, jsxs as b } from "react/jsx-runtime";
import { createContext as T, useState as E, useEffect as A, useMemo as w, useContext as B, useCallback as P } from "react";
import { c as C } from "./chunks/cn-ct4n7r74mh8y0f48.js";
import { B as F } from "./chunks/button-6by9ntsa3nj553mq.js";
const j = T(null), H = {
  javascript: () => import("./chunks/javascript-hhx3avh9uv2o27ay.js"),
  typescript: () => import("./chunks/typescript-k4ggjrxhrhcdni6u.js"),
  jsx: () => import("./chunks/jsx-ij885l2li2lzfoss.js"),
  tsx: () => import("./chunks/tsx-ehy8lrzet6v54zur.js"),
  json: () => import("./chunks/json-jovp9qemdkp5cziz.js"),
  jsonc: () => import("./chunks/jsonc-jbkkgjer6ispw2u3.js"),
  html: () => import("./chunks/html-kl304jmqj4jxj777.js"),
  css: () => import("./chunks/css-d1mi3wy1sl3fzekh.js"),
  python: () => import("./chunks/python-gg755pb8oe9ra1d1.js"),
  yaml: () => import("./chunks/yaml-e2v22lr79tss2brj.js"),
  markdown: () => import("./chunks/markdown-n2vyyq7l68fy32wc.js"),
  graphql: () => import("./chunks/graphql-ft2h47erta7isqn4.js"),
  sql: () => import("./chunks/sql-ovukp5mrfunfk5tv.js"),
  bash: () => import("./chunks/bash-g5z23u7me5rx7bzt.js").then((e) => e.b),
  shell: () => import("./chunks/bash-g5z23u7me5rx7bzt.js").then((e) => e.s),
  diff: () => import("./chunks/diff-cpj1h9lg0bw716mj.js"),
  hcl: () => import("./chunks/hcl-hcu7d6ja6t540ijn.js"),
  toml: () => import("./chunks/toml-nczb2z9n0o23o3ci.js")
}, K = {
  copy: "Copy",
  copied: "Copied!"
};
function M({
  engine: e,
  languages: i,
  labels: l,
  children: a
}) {
  const [r, s] = E({
    highlighter: null,
    isLoading: !0,
    error: null
  });
  A(() => {
    let n = !1;
    async function c() {
      try {
        const { createHighlighterCore: h } = await import("./chunks/core-dyku7qctyc40nnhh.js"), u = e === "wasm" ? await import("./chunks/engine-oniguruma-g75h8oxao68cjmhg.js").then(
          (t) => t.createOnigurumaEngine(import("./chunks/wasm-oj22f8usru9h7dzg.js"))
        ) : await import("./chunks/engine-javascript-hi1kqifa6nkcridk.js").then(
          (t) => t.createJavaScriptRegexEngine()
        ), [x, d] = await Promise.all([
          import("./chunks/github-light-ulev8flhimfg79ob.js"),
          import("./chunks/vesper-ebfu9ns65f2v830s.js")
        ]), y = i.filter(
          (t) => t in H
        ), f = await Promise.all(
          y.map((t) => H[t]())
        ), v = await h({
          themes: [x.default, d.default],
          langs: f.map((t) => t.default),
          engine: u
        });
        n || s({
          highlighter: v,
          isLoading: !1,
          error: null
        });
      } catch (h) {
        n || s({
          highlighter: null,
          isLoading: !1,
          error: h instanceof Error ? h : new Error("Failed to load Shiki")
        });
      }
    }
    return c(), () => {
      n = !0;
    };
  }, [e, i]);
  const p = w(
    () => ({ ...K, ...l }),
    [l]
  ), g = w(
    () => ({
      highlighter: r.highlighter,
      isLoading: r.isLoading,
      error: r.error,
      languages: i,
      labels: p
    }),
    [r.highlighter, r.isLoading, r.error, i, p]
  );
  return /* @__PURE__ */ o(j.Provider, { value: g, children: a });
}
M.displayName = "ShikiProvider";
function z() {
  const e = B(j);
  if (!e)
    throw new Error(
      "useShikiHighlighter must be used within a ShikiProvider. Wrap your app with <ShikiProvider> from '@cloudflare/kumo/code'."
    );
  const { highlighter: i, isLoading: l, error: a, languages: r, labels: s } = e;
  return {
    highlight: P(
      (g, n) => {
        if (!i)
          return null;
        if (!r.includes(n))
          return console.warn(
            `[Kumo CodeHighlighted] Language "${n}" is not in the ShikiProvider's languages list. Add it to the languages array: languages={[...existing, '${n}']}. Rendering as plain text.`
          ), null;
        try {
          return i.codeToHtml(g, {
            lang: n,
            themes: {
              light: "github-light",
              dark: "vesper"
            }
          });
        } catch (c) {
          return console.warn(
            `[Kumo CodeHighlighted] Failed to highlight code with language "${n}":`,
            c
          ), null;
        }
      },
      [i, r]
    ),
    isLoading: l,
    isReady: !l && i !== null,
    error: a,
    labels: s
  };
}
function D({
  code: e,
  lang: i,
  showLineNumbers: l = !1,
  highlightLines: a,
  showCopyButton: r = !1,
  labels: s,
  className: p
}) {
  const {
    highlight: g,
    isLoading: n,
    error: c,
    labels: h
  } = z(), [u, x] = E(!1), d = w(
    () => ({ ...h, ...s }),
    [h, s]
  ), y = P(async () => {
    try {
      await navigator.clipboard.writeText(e), x(!0), setTimeout(() => x(!1), 2e3);
    } catch (S) {
      console.error("[Kumo CodeHighlighted] Failed to copy to clipboard:", S);
    }
  }, [e]), f = g(e, i), v = w(() => e.split(`
`).length, [e]), t = v === 1, k = C(
    "group relative m-0 w-full min-w-0 rounded-md border border-kumo-fill bg-kumo-base p-0",
    r && t && "flex items-center",
    p
  ), N = r ? /* @__PURE__ */ o(
    "div",
    {
      className: C(
        t ? "shrink-0 px-2" : "absolute right-2 top-2",
        !u && "opacity-0 transition-opacity group-hover:opacity-100"
      ),
      children: /* @__PURE__ */ o(
        F,
        {
          variant: "secondary",
          size: "sm",
          onClick: y,
          "aria-label": u ? d.copied : d.copy,
          children: u ? d.copied : d.copy
        }
      )
    }
  ) : null, m = l && !t ? /* @__PURE__ */ o(
    "div",
    {
      className: "kumo-line-numbers shrink-0 select-none py-4 pr-4 text-right font-mono text-sm opacity-40",
      "aria-hidden": "true",
      children: Array.from({ length: v }, (S, L) => /* @__PURE__ */ o("div", { className: "leading-relaxed", children: L + 1 }, L + 1))
    }
  ) : null;
  return c && console.error("[Kumo CodeHighlighted] Shiki initialization error:", c), n || f === null ? /* @__PURE__ */ b("div", { className: k, children: [
    m && /* @__PURE__ */ b("div", { className: "flex", children: [
      m,
      /* @__PURE__ */ o("pre", { className: "!m-0 min-w-0 flex-1 overflow-x-auto !p-4 font-mono text-sm leading-relaxed text-kumo-strong", children: /* @__PURE__ */ o("code", { className: "!m-0 !p-0", children: e }) })
    ] }),
    !m && /* @__PURE__ */ o("pre", { className: "!m-0 min-w-0 flex-1 overflow-x-auto !p-4 font-mono text-sm leading-relaxed text-kumo-strong", children: /* @__PURE__ */ o("code", { className: "!m-0 !p-0", children: e }) }),
    N
  ] }) : /* @__PURE__ */ b("div", { className: k, children: [
    m && /* @__PURE__ */ b("div", { className: "flex w-full", children: [
      m,
      /* @__PURE__ */ o("div", { className: "min-w-0 flex-1 overflow-x-auto", children: /* @__PURE__ */ o(
        "div",
        {
          className: "kumo-shiki [&>pre]:!m-0 [&>pre]:!border-0 [&>pre]:!rounded-none [&>pre]:!bg-transparent [&>pre]:!p-4 [&>pre]:font-mono [&>pre]:text-sm [&>pre]:leading-relaxed [&_code]:!m-0 [&_code]:!p-0 [&_code]:!bg-transparent [&_code]:!border-0",
          dangerouslySetInnerHTML: {
            __html: _(f, a)
          }
        }
      ) })
    ] }),
    !m && /* @__PURE__ */ o("div", { className: "overflow-x-auto", children: /* @__PURE__ */ o(
      "div",
      {
        className: "kumo-shiki [&>pre]:!m-0 [&>pre]:!border-0 [&>pre]:!rounded-none [&>pre]:!bg-transparent [&>pre]:!p-4 [&>pre]:font-mono [&>pre]:text-sm [&>pre]:leading-relaxed [&_code]:!m-0 [&_code]:!p-0 [&_code]:!bg-transparent [&_code]:!border-0",
        dangerouslySetInnerHTML: {
          __html: _(f, a)
        }
      }
    ) }),
    N
  ] });
}
D.displayName = "CodeHighlighted";
function _(e, i) {
  if (!i?.length)
    return e;
  const l = new Set(i);
  let a = 0;
  return e.replace(/<span class="line">/g, () => (a++, l.has(a) ? '<span class="line line-highlighted">' : '<span class="line">'));
}
export {
  D as CodeHighlighted,
  M as ShikiProvider,
  z as useShikiHighlighter
};
//# sourceMappingURL=code.js.map
