import { jsxs as m, jsx as a } from "react/jsx-runtime";
import P, { useState as V, useRef as D, useCallback as E, forwardRef as H } from "react";
import { BaseCodeBlock as J } from "../code-block/base-code-block.js";
import { CopyToClipboardButton as K } from "../code-block/copy-button.js";
import { Classes as h } from "../../constants/selectors.js";
import { cn as i } from "../../utils/cn.js";
import { getNodeText as Q } from "../../utils/get-node-text.js";
import { LanguageDropdown as U } from "./language-dropdown.js";
import { Icon as $ } from "../icon/icon.js";
import { TabsRoot as W } from "../../node_modules/.pnpm/@base-ui_react@1.1.0_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/react/esm/tabs/root/TabsRoot.js";
import { TabsList as X } from "../../node_modules/.pnpm/@base-ui_react@1.1.0_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/react/esm/tabs/list/TabsList.js";
import { TabsPanel as Y } from "../../node_modules/.pnpm/@base-ui_react@1.1.0_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/react/esm/tabs/panel/TabsPanel.js";
import { TabsTab as Z } from "../../node_modules/.pnpm/@base-ui_react@1.1.0_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/react/esm/tabs/tab/TabsTab.js";
const ur = ({
  children: g,
  isSmallText: f,
  className: c,
  noMargins: l,
  dropdown: p,
  feedbackModalOpen: n,
  anchorRef: b,
  codeBlockTheme: o = "system",
  codeBlockThemeObject: F,
  initialSelectedTab: _ = 0,
  onSelectedTabChange: u,
  askAiButton: y,
  feedbackButton: v,
  copyButtonProps: q
}) => {
  var N, C, I, j, R, A, G;
  const t = P.Children.toArray(g), z = Math.min(
    Math.max(0, _),
    Math.max(0, t.length - 1)
  ), [w, O] = V(z), x = D(/* @__PURE__ */ new Map()), k = E(
    (e) => {
      var L;
      const r = Number(e), d = typeof document < "u" && ((L = document.activeElement) == null ? void 0 : L.getAttribute("role")) === "tab";
      typeof window < "u" && window.location.hash && window.history.replaceState(
        null,
        "",
        window.location.pathname + window.location.search
      ), O(r), u == null || u(r), d && typeof requestAnimationFrame < "u" && requestAnimationFrame(() => {
        const M = x.current.get(r);
        M && M.focus();
      });
    },
    [u]
  );
  if (!g)
    return null;
  if (t.length === 0)
    return console.warn(
      "CodeGroup has no children, expected at least one CodeBlock child."
    ), null;
  const s = Number(w);
  return /* @__PURE__ */ m(
    W,
    {
      className: i(
        h.CodeGroup,
        "not-prose relative mt-5 mb-8 flex flex-col overflow-hidden rounded-2xl border border-stone-950/10 p-0.5 dark:border-white/10",
        l && "my-0",
        o === "system" && "dark:codeblock-dark codeblock-light bg-stone-50 text-stone-950 dark:bg-white/5 dark:text-stone-50",
        o === "dark" && "codeblock-dark border-transparent bg-codeblock text-stone-50 dark:bg-white/5",
        n && "border border-primary dark:border-primary-light",
        c
      ),
      onValueChange: (e) => k(String(e)),
      ref: b,
      value: w,
      children: [
        /* @__PURE__ */ m(
          "div",
          {
            className: i(
              "relative flex items-center justify-between gap-2",
              p ? "px-2.5" : "pr-2.5"
            ),
            "data-component-part": "code-group-tab-bar",
            children: [
              p ? /* @__PURE__ */ m("div", { className: "flex min-w-0 items-center gap-1.5 font-medium text-xs", children: [
                ((N = t[s]) == null ? void 0 : N.props.icon) && typeof ((C = t[s]) == null ? void 0 : C.props.icon) == "string" && /* @__PURE__ */ a(
                  $,
                  {
                    className: i(
                      "size-3.5 bg-stone-500 dark:bg-stone-400",
                      h.CodeBlockIcon
                    ),
                    icon: (I = t[s]) == null ? void 0 : I.props.icon,
                    iconType: "regular",
                    overrideColor: !0
                  }
                ),
                /* @__PURE__ */ a(
                  "span",
                  {
                    className: i(
                      "truncate",
                      o === "system" && "text-stone-950 dark:text-stone-50",
                      o === "dark" && "text-stone-50"
                    ),
                    children: (j = t[s]) == null ? void 0 : j.props.filename
                  }
                )
              ] }) : /* @__PURE__ */ a(
                X,
                {
                  className: i(
                    "flex w-0 flex-1 gap-1 overflow-x-auto overflow-y-hidden rounded-tl-xl text-xs leading-6",
                    o === "system" ? "scrollbar-code-system" : "scrollbar-code-dark"
                  ),
                  children: t.map((e, r) => /* @__PURE__ */ m(
                    S,
                    {
                      codeBlockTheme: o,
                      isSelected: s === r,
                      ref: (d) => {
                        d ? x.current.set(r, d) : x.current.delete(r);
                      },
                      tabsLength: t.length,
                      value: r,
                      children: [
                        e.props.icon && typeof e.props.icon == "string" && /* @__PURE__ */ a(
                          $,
                          {
                            className: i(
                              "h-3.5 w-3.5 bg-stone-500 dark:bg-stone-400",
                              o === "system" ? "group-hover:bg-primary dark:group-hover:bg-primary-light" : "group-hover:bg-stone-700/70 group-hover:text-primary-light",
                              h.CodeBlockIcon
                            ),
                            color: s === r ? "currentColor" : void 0,
                            icon: e.props.icon,
                            iconType: "regular",
                            overrideColor: !0
                          }
                        ),
                        e.props.filename
                      ]
                    },
                    `${e.props.filename}TabItem${r}`
                  ))
                }
              ),
              /* @__PURE__ */ m("div", { className: "flex shrink-0 items-center justify-end gap-1.5", children: [
                p && /* @__PURE__ */ a(
                  U,
                  {
                    codeBlockTheme: o,
                    languages: t.map((e) => e.props.language || ""),
                    selectedLanguage: ((R = t[s]) == null ? void 0 : R.props.language) || "",
                    setSelectedLanguage: (e, r) => {
                      k(String(r));
                    }
                  }
                ),
                v && v,
                /* @__PURE__ */ a(
                  K,
                  {
                    codeBlockTheme: o,
                    textToCopy: Q((G = (A = t[s]) == null ? void 0 : A.props) == null ? void 0 : G.children),
                    ...q
                  }
                ),
                y && y
              ] })
            ]
          }
        ),
        /* @__PURE__ */ a("div", { className: "flex flex-1 overflow-hidden", children: t.map((e, r) => /* @__PURE__ */ a(
          Y,
          {
            className: "relative h-full max-h-full w-full min-w-full max-w-full",
            tabIndex: -1,
            value: r,
            children: /* @__PURE__ */ a(
              J,
              {
                ...e.props,
                codeBlockTheme: o,
                codeBlockThemeObject: F ?? o,
                expandable: e.props.expandable && r === s,
                isParentCodeGroup: !0,
                isSmallText: f,
                shouldHighlight: r === s
              }
            )
          },
          `${e.props.filename}Content${r}`
        )) })
      ]
    }
  );
}, S = H(
  function({ children: f, value: c, isSelected: l, tabsLength: p, codeBlockTheme: n }, b) {
    return /* @__PURE__ */ m(
      Z,
      {
        className: i(
          "group relative my-1 mb-1.5 ml-0! flex items-center gap-1.5 whitespace-nowrap font-medium outline-0 first:ml-2.5! focus-visible:outline-2",
          l && n === "system" && "text-primary dark:text-primary-light",
          l && n === "dark" && "text-primary-light",
          !l && n === "system" && "text-stone-500 dark:text-stone-400",
          !l && n === "dark" && "text-stone-400"
        ),
        ref: b,
        value: c,
        children: [
          /* @__PURE__ */ a(
            "div",
            {
              className: i(
                "z-10 flex items-center gap-1.5 rounded-lg px-1.5 focus:outline-none",
                p > 1 && n === "system" && "group-hover:bg-stone-200/50 group-hover:text-primary dark:group-hover:bg-stone-700/70 dark:group-hover:text-primary-light",
                p > 1 && n === "dark" && "group-hover:bg-stone-700/70 group-hover:text-primary-light"
              ),
              children: f
            }
          ),
          l && /* @__PURE__ */ a("div", { className: "absolute right-0 -bottom-1.5 left-0 h-0.5 rounded-full bg-primary dark:bg-primary-light" })
        ]
      }
    );
  }
);
export {
  ur as CodeGroup
};
