import { jsxs as $, Fragment as k, jsx as s } from "react/jsx-runtime";
import { useExpandable as J, SMALL_EXPANDABLE_NUMBER_OF_LINES as K, SMALL_EXPANDABLE_CODE_BLOCK_HEIGHT as R, DEFAULT_EXPANDABLE_CODE_BLOCK_HEIGHT as U } from "../../hooks/use-expandable.js";
import { useGetShikiHighlightedHtml as W } from "../../hooks/use-get-shiki-highlighted-html.js";
import { cn as m } from "../../utils/cn.js";
import { getShikiBackgroundColors as j } from "../../utils/shiki/get-shiki-background-colors.js";
import { getCodeString as O, useCalculateCodeLines as T } from "../../utils/shiki/lib.js";
import { CodeFooter as V } from "./code-footer.js";
const P = ({
  expandable: t = !1,
  shouldHighlight: u = !0,
  children: h,
  isParentCodeGroup: a,
  isSmallText: x,
  numberOfLines: o,
  forceExtract: v = !1,
  codeBlockTheme: i = "system",
  codeBlockThemeObject: w = "system",
  ...f
  // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO
}) => {
  const { focus: y, highlight: E, lang: H, language: b, className: S, wrap: c, lines: D } = f, L = O(h, f.className, v), d = {};
  let r = [], n = [];
  if (y)
    try {
      const l = JSON.parse(y);
      r = Array.isArray(l) ? l : [];
    } catch {
      r = [];
    }
  if (E)
    try {
      const l = JSON.parse(E);
      n = Array.isArray(l) ? l : [];
    } catch {
      n = [];
    }
  n.length > 0 && (d.highlightedLines = n), r.length > 0 && (d.focusedLines = r);
  const _ = H || b, g = W(
    _ ? {
      codeString: L,
      codeBlockTheme: w,
      language: u ? _ : "text",
      opts: d
    } : {
      codeString: L,
      codeBlockTheme: w,
      className: u ? S : "lang-text",
      opts: d
    }
  );
  o = T(g, o);
  const { isExpanded: p, calculatedHeight: F, contentRef: A, toggleExpanded: I } = J(t, o), e = j(
    i,
    g,
    h
  ), M = r.length > 0, N = n.length > 0, X = t ? p ? `${F}px` : o && o < K ? `${R}px` : `${U}px` : a ? "100%" : "auto";
  return /* @__PURE__ */ $(k, { children: [
    /* @__PURE__ */ s(
      "div",
      {
        className: m(
          // prevent code blocks from growing beyond their container, setting width to 0 to recalculate the width
          "children:!my-0 children:!shadow-none children:!bg-transparent relative h-full w-0 min-w-full max-w-full rounded-[14px] px-4 py-3.5 text-sm leading-6 dark:bg-codeblock",
          "code-block-background overflow-x-auto transition-[height] duration-300 ease-in-out",
          "**:outline-0 **:ring-0 **:focus:outline-0 **:focus:ring-0",
          f.filename ? "rounded-[14px]" : "rounded-2xl",
          i === "system" ? "bg-white" : "bg-codeblock",
          (t || a) && "overflow-auto",
          t && (p ? "overflow-y-auto" : "overflow-y-hidden"),
          c ? "code-block-wrap overflow-x-hidden whitespace-pre-wrap" : "overflow-x-auto",
          D && "has-line-numbers",
          M && "has-focused",
          N && "has-highlighted",
          i === "system" ? "scrollbar-code-system" : "scrollbar-code-dark"
        ),
        "data-component-part": "code-block-root",
        style: {
          fontVariantLigatures: "none",
          height: X,
          backgroundColor: e == null ? void 0 : e.light,
          "--shiki-dark-bg": e == null ? void 0 : e.dark
        },
        tabIndex: 0,
        children: g ? /* @__PURE__ */ s(
          "div",
          {
            className: m(
              "font-mono",
              c ? "whitespace-pre-wrap" : "whitespace-pre",
              a && "h-full flex-none text-sm",
              x ? "text-xs leading-[1.35rem]" : "leading-6"
            ),
            ref: A,
            suppressHydrationWarning: !0,
            ...a && {
              "data-component-part": "code-group-tab-content"
            },
            dangerouslySetInnerHTML: { __html: g }
          },
          i
        ) : /* @__PURE__ */ s(
          "div",
          {
            className: m(
              "font-mono",
              c ? "whitespace-pre-wrap" : "whitespace-pre",
              a && "h-full flex-none text-sm",
              x ? "text-xs leading-[1.35rem]" : "leading-6"
            ),
            ref: A,
            suppressHydrationWarning: !0,
            ...a && {
              "data-component-part": "code-group-tab-content"
            },
            children: h
          }
        )
      }
    ),
    !(c || N || t) && r.length === 0 && /* @__PURE__ */ s(
      "div",
      {
        "aria-hidden": !0,
        "data-fade-overlay": !0,
        style: {
          "--fade-color-light": e == null ? void 0 : e.light,
          "--fade-color-dark": e == null ? void 0 : e.dark
        }
      }
    ),
    t && /* @__PURE__ */ s(
      V,
      {
        isExpanded: p,
        numberOfLines: o,
        toggleExpanded: I
      }
    )
  ] });
};
export {
  P as BaseCodeBlock
};
