import { jsxs, jsx } from "react/jsx-runtime";
import Highlight, { defaultProps } from "prism-react-renderer";
import { CopyIcon } from "./CopyIcon.mjs";
import { getPrismTheme } from "./prism-theme.mjs";
import usePrismStyles from "./Prism.styles.mjs";
import { factory } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/core/factory/factory.mjs";
import { useProps } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/core/MantineProvider/use-props/use-props.mjs";
import { useComputedColorScheme } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/core/MantineProvider/use-mantine-color-scheme/use-computed-color-scheme.mjs";
import { useMantineTheme } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/core/MantineProvider/MantineThemeProvider/MantineThemeProvider.mjs";
import { useClipboard } from "../../node_modules/.pnpm/@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-clipboard/use-clipboard.mjs";
import { ScrollArea } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/ScrollArea/ScrollArea.mjs";
import { useStyles } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/core/styles-api/use-styles/use-styles.mjs";
import { Box } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/core/Box/Box.mjs";
import { Tooltip } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Tooltip/Tooltip.mjs";
import { ActionIcon } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/ActionIcon/ActionIcon.mjs";
import { getThemeColor } from "../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/core/MantineProvider/color-functions/get-theme-color/get-theme-color.mjs";
const prismDefaultProps = {
  noCopy: false,
  copyLabel: "Copy code",
  copiedLabel: "Copied",
  withLineNumbers: false,
  trim: true,
  highlightLines: {},
  scrollAreaComponent: ScrollArea,
  getPrismTheme
};
const Prism = factory((_props, ref) => {
  const props = useProps("CodeHighlight", prismDefaultProps, _props);
  const {
    className,
    children,
    language,
    noCopy,
    classNames,
    styles,
    copyLabel,
    copiedLabel,
    withLineNumbers,
    highlightLines,
    scrollAreaComponent: ScrollAreaComponent,
    colorScheme,
    trim,
    unstyled,
    radius,
    getPrismTheme: getPrismTheme2,
    ...others
  } = useProps("Prism", prismDefaultProps, props);
  const code = trim && typeof children === "string" ? children.trim() : children;
  const maxLineSize = code.split("\n").length.toString().length;
  const globalColorScheme = useComputedColorScheme();
  const theme = useMantineTheme();
  const clipboard = useClipboard();
  const _colorScheme = colorScheme || globalColorScheme;
  const { classes } = usePrismStyles({
    colorScheme: _colorScheme,
    native: ScrollAreaComponent !== ScrollArea,
    maxLineSize,
    radius
  });
  const getStyles = useStyles({
    name: "Prism",
    props,
    classNames,
    classes,
    styles,
    unstyled
  });
  return /* @__PURE__ */ jsxs(Box, { ...getStyles("root"), ...others, ref, translate: "no", children: [
    !noCopy && /* @__PURE__ */ jsx(
      Tooltip,
      {
        label: clipboard.copied ? copiedLabel : copyLabel,
        position: "left",
        withArrow: true,
        arrowSize: 6,
        offset: 6,
        color: clipboard.copied ? "teal" : void 0,
        unstyled,
        children: /* @__PURE__ */ jsx(
          ActionIcon,
          {
            ...getStyles("copy"),
            "aria-label": clipboard.copied ? copiedLabel : copyLabel,
            onClick: () => clipboard.copy(code),
            unstyled,
            children: /* @__PURE__ */ jsx(CopyIcon, { copied: clipboard.copied })
          }
        )
      }
    ),
    /* @__PURE__ */ jsx(Highlight, { ...defaultProps, theme: getPrismTheme2(theme, _colorScheme), code, language, children: ({ className: inheritedClassName, style: inheritedStyle, tokens, getLineProps, getTokenProps }) => /* @__PURE__ */ jsx(ScrollAreaComponent, { ...getStyles("scrollArea"), dir: "ltr", children: /* @__PURE__ */ jsx("pre", { ...getStyles("code", { className: inheritedClassName, style: inheritedStyle }), dir: "ltr", children: tokens.map((line, index) => {
      var _a, _b;
      if (index === tokens.length - 1 && line.length === 1 && line[0].content === "\n") {
        return null;
      }
      const lineNumber = index + 1;
      const { key, ...lineProps } = getLineProps({ line, key: index });
      const shouldHighlight = lineNumber in highlightLines;
      const lineThemeColor = getThemeColor((_a = highlightLines == null ? void 0 : highlightLines[lineNumber]) == null ? void 0 : _a.color, theme);
      const lineColor = lineThemeColor;
      return /* @__PURE__ */ jsxs(
        "div",
        {
          ...lineProps,
          ...getStyles("line", {
            className: lineProps.className,
            style: { ...shouldHighlight ? { backgroundColor: lineColor } : null }
          }),
          children: [
            withLineNumbers && /* @__PURE__ */ jsx(
              "div",
              {
                ...getStyles("lineNumber", {
                  style: { color: shouldHighlight ? lineThemeColor : void 0 }
                }),
                children: ((_b = highlightLines == null ? void 0 : highlightLines[lineNumber]) == null ? void 0 : _b.label) || lineNumber
              }
            ),
            /* @__PURE__ */ jsx("div", { ...getStyles("lineContent"), children: line.map((token, key2) => {
              var _a2;
              const { key: tokenKey, ...tokenProps } = getTokenProps({ token, key: key2 });
              return /* @__PURE__ */ jsx(
                "span",
                {
                  ...tokenProps,
                  style: {
                    ...tokenProps.style,
                    color: shouldHighlight ? (
                      // ? theme.f[n.themeColor(
                      //     highlightLines?.[lineNumber]?.color,
                      //     _colorScheme === 'dark' ? 5 : 8
                      //   )]
                      lineThemeColor
                    ) : (_a2 = tokenProps == null ? void 0 : tokenProps.style) == null ? void 0 : _a2.color
                  }
                },
                tokenKey
              );
            }) })
          ]
        },
        key
      );
    }).filter(Boolean) }) }) })
  ] });
});
Prism.displayName = "@mantine/prism/Prism";
export {
  Prism
};
