import { transformerMetaHighlight as D, transformerNotationHighlight as P, transformerNotationFocus as j, transformerNotationDiff as B } from "../../node_modules/.pnpm/@shikijs_transformers@3.21.0/node_modules/@shikijs/transformers/dist/index.js";
import { MAX_PREVIEW_BYTES as k } from "../../constants/index.js";
import { codeStylingToThemeOrThemes as G } from "./get-code-styling.js";
import { getShikiLanguageFromPreset as p } from "./snippet-presets.js";
import { LANGS as K, SHIKI_COLOR_REPLACEMENTS as z, LINE_HIGHLIGHT_CLASS_NAME as c, LINE_FOCUS_CLASS_NAME as g, LINE_DIFF_ADD_CLASS_NAME as U, LINE_DIFF_REMOVE_CLASS_NAME as V, SHIKI_LANG_MAP as C } from "./constants.js";
import { getShikiWorker as W } from "./worker-client.js";
import { createHighlighter as $ } from "../../node_modules/.pnpm/shiki@3.21.0/node_modules/shiki/dist/bundle-full.js";
import { createJavaScriptRegexEngine as J } from "../../node_modules/.pnpm/@shikijs_engine-javascript@3.21.0/node_modules/@shikijs/engine-javascript/dist/engine-compile.js";
import { hastToHtml as x } from "../../node_modules/.pnpm/@shikijs_core@3.21.0/node_modules/@shikijs/core/dist/index.js";
const h = {
  matchAlgorithm: "v3"
}, X = [
  D({
    className: c
  }),
  P({
    ...h,
    classActiveLine: c
  }),
  j({
    ...h,
    classActiveLine: g
  }),
  B({
    ...h,
    classLineAdd: U,
    classLineRemove: V
  })
], Y = J({
  forgiving: !0,
  cache: /* @__PURE__ */ new Map()
});
let s, R, F = !1;
const w = $({
  themes: [],
  langs: K,
  engine: Y
}).then((e) => {
  s = e;
}).catch((e) => {
  console.error(e), R = e ?? new Error("Unknown error occurred initializing highlighter");
}).finally(() => {
  F = !0;
});
function q(e) {
  const t = "text";
  if (e === void 0)
    return t;
  const i = Number(e);
  if (!Number.isNaN(i) && i > 99 && i < 600)
    return "json";
  const r = e.toLowerCase(), a = p(r);
  return C[a] ?? C[r] ?? t;
}
function Q(e, t) {
  const i = /language-(\w+)/.exec(e ?? "");
  return i ? i[1] ?? "text" : t ?? "text";
}
function O(e) {
  if (typeof e == "string" || !e)
    return e === "system" || !e ? ["dark-plus", "github-light-default"] : ["dark-plus"];
  const { theme: t } = e;
  return typeof t == "string" ? [t] : typeof t == "object" ? [t.dark, t.light] : ["dark-plus", "github-light-default"];
}
function Z(e) {
  if (!s)
    return !1;
  const t = s.getLoadedThemes();
  return O(e).every((r) => r === "css-variables" || t.includes(r));
}
async function ee(e) {
  if (await w, !s)
    return;
  const t = s.getLoadedThemes(), i = O(e).filter(
    (r) => r !== "css-variables" && !t.includes(r)
  );
  i.length > 0 && await s.loadTheme(...i);
}
function M(e) {
  var a, f, m, d, u, L, S;
  if (!e.codeString || R !== void 0)
    return;
  if (s === void 0)
    return (a = e.opts) != null && a.noAsync || F ? void 0 : w.then(() => M(e)).catch(() => {
    });
  if (!Z(e.codeBlockTheme))
    return (f = e.opts) != null && f.noAsync ? void 0 : ee(e.codeBlockTheme).then(() => M(e)).catch(() => {
    });
  let t;
  if ("language" in e) {
    if (e.language === "text")
      return;
    t = e.language;
  } else {
    if (e.className === "lang-text")
      return;
    t = Q(e.className, e.fileName);
  }
  if (e.codeString.length > k * 5)
    return;
  if (e.codeString.length > k) {
    if (((m = e.opts) == null ? void 0 : m.noAsync) === !0)
      return;
    try {
      const n = W();
      return n === void 0 ? void 0 : n.highlight(e);
    } catch {
      return;
    }
  }
  let i;
  if (t)
    try {
      const n = (u = (d = e.opts) == null ? void 0 : d.highlightedLines) != null && u.length || (S = (L = e.opts) == null ? void 0 : L.focusedLines) != null && S.length ? "codeToHast" : "codeToHtml", v = e.codeString.trim(), o = G(e.codeBlockTheme);
      i = s[n](v, {
        lang: q(t),
        ...o,
        colorReplacements: { ...z },
        transformers: X,
        tabindex: !1,
        ...e.opts
      });
    } catch (n) {
      console.error("error getting shiki highlighted html", n);
    }
  if (typeof i != "object")
    return i;
  const r = i.children[0];
  if (r) {
    if (r.type === "element" && r.tagName === "pre") {
      const n = r.children[0];
      if (!(n != null && n.children))
        return x(i);
      n.children.filter(
        (o) => o.type === "element" && o.tagName === "span"
      ).forEach((o, b) => {
        var N, A, E, _, y, T, I, H;
        const l = b + 1;
        typeof o.properties.class == "string" ? ((A = (N = e.opts) == null ? void 0 : N.highlightedLines) != null && A.includes(l) && (o.properties.class += ` ${c}`), (_ = (E = e.opts) == null ? void 0 : E.focusedLines) != null && _.includes(l) && (o.properties.class += ` ${g}`)) : Array.isArray(o.properties.class) && ((T = (y = e.opts) == null ? void 0 : y.highlightedLines) != null && T.includes(l) && o.properties.class.push(c), (H = (I = e.opts) == null ? void 0 : I.focusedLines) != null && H.includes(l) && o.properties.class.push(g));
      });
    }
    return x(i);
  }
}
export {
  X as SHIKI_TRANSFORMERS,
  Q as getLanguageFromClassName,
  M as getShikiHighlightedHtml,
  q as getShikiLanguage,
  w as highlighterPromise,
  ee as loadShikiThemes
};
