"use client";
import { jsx as r, jsxs as b } from "react/jsx-runtime";
import { createContext as T, useState as j, useEffect as q, useMemo as w, useContext as B, useCallback as E } from "react";
import { B as F } from "./chunks/button-ebfdg9mkhojue36n.js";
import { c as C } from "./chunks/vendor-styling-ofvnbcuht98q88oo.js";
const A = T(null), H = {
  js: "javascript",
  cjs: "javascript",
  mjs: "javascript",
  ts: "typescript",
  cts: "typescript",
  mts: "typescript",
  sh: "bash",
  zsh: "bash",
  yml: "yaml",
  py: "python",
  md: "markdown",
  gql: "graphql"
}, P = {
  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")
};
function z(e) {
  return e in P ? e : e in H ? H[e] : null;
}
const I = {
  copy: "Copy",
  copied: "Copied!"
};
function K({
  engine: e,
  languages: o,
  labels: l,
  children: s
}) {
  const [i, h] = j({
    highlighter: null,
    isLoading: !0,
    error: null,
    languages: []
  });
  q(() => {
    let n = !1;
    async function c() {
      try {
        const { createHighlighterCore: a } = 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")
        ]), v = [
          ...new Set(
            o.map((t) => z(t)).filter((t) => t !== null)
          )
        ], f = await Promise.all(
          v.map((t) => P[t]())
        ), y = await a({
          themes: [x.default, d.default],
          langs: f.map((t) => t.default),
          engine: u
        });
        n || h({
          highlighter: y,
          isLoading: !1,
          error: null,
          languages: v
        });
      } catch (a) {
        n || h({
          highlighter: null,
          isLoading: !1,
          error: a instanceof Error ? a : new Error("Failed to load Shiki"),
          languages: []
        });
      }
    }
    return c(), () => {
      n = !0;
    };
  }, [e, o]);
  const p = w(
    () => ({ ...I, ...l }),
    [l]
  ), g = w(
    () => ({
      highlighter: i.highlighter,
      isLoading: i.isLoading,
      error: i.error,
      languages: i.languages,
      labels: p
    }),
    [i.highlighter, i.isLoading, i.error, i.languages, p]
  );
  return /* @__PURE__ */ r(A.Provider, { value: g, children: s });
}
K.displayName = "ShikiProvider";
function M() {
  const e = B(A);
  if (!e)
    throw new Error(
      "useShikiHighlighter must be used within a ShikiProvider. Wrap your app with <ShikiProvider> from '@cloudflare/kumo/code'."
    );
  const { highlighter: o, isLoading: l, error: s, languages: i, labels: h } = e;
  return {
    highlight: E(
      (g, n) => {
        if (!o)
          return null;
        const c = z(n);
        if (!c || !i.includes(c))
          return console.warn(
            `[Kumo CodeHighlighted] Language "${n}" is not in the ShikiProvider's languages list. Add it to the languages array: languages={[...existing, '${c || n}']}. Rendering as plain text.`
          ), null;
        try {
          return o.codeToHtml(g, {
            lang: c,
            themes: {
              light: "github-light",
              dark: "vesper"
            }
          });
        } catch (a) {
          return console.warn(
            `[Kumo CodeHighlighted] Failed to highlight code with language "${n}":`,
            a
          ), null;
        }
      },
      [o, i]
    ),
    isLoading: l,
    isReady: !l && o !== null,
    error: s,
    labels: h
  };
}
function D({
  code: e,
  lang: o,
  showLineNumbers: l = !1,
  highlightLines: s,
  showCopyButton: i = !1,
  labels: h,
  className: p
}) {
  const {
    highlight: g,
    isLoading: n,
    error: c,
    labels: a
  } = M(), [u, x] = j(!1), d = w(
    () => ({ ...a, ...h }),
    [a, h]
  ), v = E(async () => {
    try {
      await navigator.clipboard.writeText(e), x(!0), setTimeout(() => x(!1), 2e3);
    } catch (L) {
      console.error("[Kumo CodeHighlighted] Failed to copy to clipboard:", L);
    }
  }, [e]), f = g(e, o), y = w(() => e.split(`
`).length, [e]), t = y === 1, k = C(
    "group relative m-0 w-full min-w-0 rounded-md border border-kumo-fill bg-kumo-base p-0",
    i && t && "flex items-center",
    p
  ), S = i ? /* @__PURE__ */ r(
    "div",
    {
      className: C(
        t ? "shrink-0 px-2" : "absolute right-2 top-2",
        !u && "opacity-0 transition-opacity group-hover:opacity-100"
      ),
      children: /* @__PURE__ */ r(
        F,
        {
          variant: "secondary",
          size: "sm",
          onClick: v,
          "aria-label": u ? d.copied : d.copy,
          children: u ? d.copied : d.copy
        }
      )
    }
  ) : null, m = l && !t ? /* @__PURE__ */ r(
    "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: y }, (L, N) => /* @__PURE__ */ r("div", { className: "leading-relaxed", children: N + 1 }, N + 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__ */ r("pre", { className: "!m-0 min-w-0 flex-1 overflow-x-auto !p-4 font-mono text-sm leading-relaxed text-kumo-subtle", children: /* @__PURE__ */ r("code", { className: "!m-0 !p-0", children: e }) })
    ] }),
    !m && /* @__PURE__ */ r("pre", { className: "!m-0 min-w-0 flex-1 overflow-x-auto !p-4 font-mono text-sm leading-relaxed text-kumo-subtle", children: /* @__PURE__ */ r("code", { className: "!m-0 !p-0", children: e }) }),
    S
  ] }) : /* @__PURE__ */ b("div", { className: k, children: [
    m && /* @__PURE__ */ b("div", { className: "flex w-full", children: [
      m,
      /* @__PURE__ */ r("div", { className: "min-w-0 flex-1 overflow-x-auto", children: /* @__PURE__ */ r(
        "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, s)
          }
        }
      ) })
    ] }),
    !m && /* @__PURE__ */ r("div", { className: "overflow-x-auto", children: /* @__PURE__ */ r(
      "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, s)
        }
      }
    ) }),
    S
  ] });
}
D.displayName = "CodeHighlighted";
function _(e, o) {
  if (!o?.length)
    return e;
  const l = new Set(o);
  let s = 0;
  return e.replace(/<span class="line">/g, () => (s++, l.has(s) ? '<span class="line line-highlighted">' : '<span class="line">'));
}
export {
  D as CodeHighlighted,
  H as LANGUAGE_ALIASES,
  K as ShikiProvider,
  z as normalizeLanguage,
  M as useShikiHighlighter
};
//# sourceMappingURL=code.js.map
