"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const index = require("../../icons/index.cjs");
;/* empty css                                                                                                                                                                                        */
;/* empty css                                                                                                                                                                                         */
;/* empty css                                                                                                                                                                                                 */
;/* empty css                                                                                                                                                                                            */
;/* empty css                                                                                                                                                                                            */
const CopyButton = require("../../primitive/CopyButton/CopyButton.cjs");
require("clsx");
const styles = require("../../utils/styles.cjs");
const Box = require("../../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.cjs");
const Prism = require("../../primitive/Prism/Prism.cjs");
const Group = require("../../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/Group/Group.cjs");
const HoverCard = require("../../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/HoverCard/HoverCard.cjs");
const ActionIcon = require("../../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.cjs");
const Code = require("../../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/Code/Code.cjs");
const useLocalStorage = require("../../node_modules/.pnpm/@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-local-storage/use-local-storage.cjs");
function useFold(persistenceKey) {
  const foldPersistenceKey = `${persistenceKey}.codeblock.fold`;
  const [persistentFolded, setPersistentFolded] = useLocalStorage.useLocalStorage({
    key: foldPersistenceKey,
    defaultValue: true,
    getInitialValueInEffect: false
  });
  const [memoryFolded, setMemoryFolded] = React.useState(true);
  const persistent = !!persistenceKey;
  const folded = persistent ? persistentFolded : memoryFolded;
  const setFolded = persistent ? setPersistentFolded : setMemoryFolded;
  return { folded, setFolded };
}
const CodeBlock = ({
  language = "bash",
  codeRender,
  children,
  copyContent,
  onCopyClick,
  codeHighlightProps,
  foldProps,
  withBorder = true,
  ...rest
}) => {
  const { defaultHeight, persistenceKey, iconVisible: foldIconVisible, onIconClick: onFoldIconClick } = foldProps || {};
  const { folded, setFolded } = useFold(persistenceKey);
  const mah = React.useMemo(() => {
    if (defaultHeight) {
      return folded ? defaultHeight : void 0;
    }
    if (foldIconVisible) {
      return folded ? 200 : void 0;
    }
    return void 0;
  }, [foldIconVisible, folded, defaultHeight]);
  const codeScrollAreaPadding = foldIconVisible ? 48 : 24;
  return /* @__PURE__ */ jsxRuntime.jsxs(Box.Box, { ...rest, sx: styles.mergeSxList([{ position: "relative" }, rest == null ? void 0 : rest.sx]), children: [
    /* @__PURE__ */ jsxRuntime.jsx(
      Box.Box,
      {
        p: "md",
        mah,
        bg: "carbon.2",
        sx: (theme) => ({
          border: `1px solid ${withBorder ? theme.colors.carbon[4] : "transparent"}`,
          borderRadius: theme.defaultRadius,
          overflow: "auto"
        }),
        children: /* @__PURE__ */ jsxRuntime.jsx(Box.Box, { sx: { paddingRight: codeScrollAreaPadding }, children: codeRender ? codeRender(children) : /* @__PURE__ */ jsxRuntime.jsx(
          Prism.Prism,
          {
            ...codeHighlightProps,
            noCopy: true,
            language,
            styles: styles.mergeStylesList([
              {
                code: {
                  padding: 0,
                  backgroundColor: `transparent !important`,
                  wordBreak: "break-all"
                },
                line: {
                  paddingLeft: 0
                },
                lineContent: {
                  whiteSpace: "pre-wrap"
                }
              },
              codeHighlightProps == null ? void 0 : codeHighlightProps.styles
            ]),
            children
          }
        ) })
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsxs(Group.Group, { gap: 4, sx: (theme) => ({ position: "absolute", top: 16, right: 16, color: theme.colors.carbon[8] }), children: [
      foldIconVisible && /* @__PURE__ */ jsxRuntime.jsxs(HoverCard.HoverCard, { withArrow: true, position: "top", shadow: "xs", children: [
        /* @__PURE__ */ jsxRuntime.jsx(HoverCard.HoverCard.Target, { children: /* @__PURE__ */ jsxRuntime.jsx(
          ActionIcon.ActionIcon,
          {
            size: "sm",
            variant: "subtle",
            onClick: () => {
              const v = !folded;
              setFolded(v);
              onFoldIconClick == null ? void 0 : onFoldIconClick(v);
            },
            children: folded ? /* @__PURE__ */ jsxRuntime.jsx(index.IconChevronVerticalExpand, { size: 14, strokeWidth: 2.5 }) : /* @__PURE__ */ jsxRuntime.jsx(index.IconChevronVerticalShrink, { size: 14, strokeWidth: 2.5 })
          }
        ) }),
        /* @__PURE__ */ jsxRuntime.jsx(HoverCard.HoverCard.Dropdown, { p: "xs", children: folded ? "Expand" : "Collapse" })
      ] }),
      /* @__PURE__ */ jsxRuntime.jsx(CopyButton.CopyButton, { value: copyContent ?? children, timeout: 2e3, children: ({ copied, copy }) => /* @__PURE__ */ jsxRuntime.jsxs(HoverCard.HoverCard, { withArrow: true, position: "top", shadow: "xs", children: [
        /* @__PURE__ */ jsxRuntime.jsx(HoverCard.HoverCard.Target, { children: /* @__PURE__ */ jsxRuntime.jsx(
          ActionIcon.ActionIcon,
          {
            "aria-label": "Copy",
            size: "sm",
            variant: "subtle",
            onClick: () => {
              copy();
              onCopyClick == null ? void 0 : onCopyClick();
            },
            children: copied ? /* @__PURE__ */ jsxRuntime.jsx(index.IconCheck, { size: 14 }) : /* @__PURE__ */ jsxRuntime.jsx(index.IconCopy01, { size: 14, strokeWidth: 2.5 })
          }
        ) }),
        /* @__PURE__ */ jsxRuntime.jsx(HoverCard.HoverCard.Dropdown, { p: "xs", children: copied ? "Copied" : "Copy" })
      ] }) })
    ] })
  ] });
};
const CopyText = ({ children, value, ...rest }) => {
  return /* @__PURE__ */ jsxRuntime.jsxs(
    Code.Code,
    {
      bg: "carbon.3",
      ...rest,
      p: 8,
      sx: (theme, u) => {
        return styles.mergeSxList([
          {
            display: "inline-flex",
            position: "relative",
            justifyContent: "center",
            alignItems: "center",
            borderRadius: theme.defaultRadius
          },
          rest == null ? void 0 : rest.sx
        ])(theme, u);
      },
      children: [
        children,
        /* @__PURE__ */ jsxRuntime.jsx(CopyButton.CopyButton, { value, timeout: 2e3, children: ({ copied, copy }) => /* @__PURE__ */ jsxRuntime.jsxs(HoverCard.HoverCard, { withArrow: true, position: "top", shadow: "xs", children: [
          /* @__PURE__ */ jsxRuntime.jsx(HoverCard.HoverCard.Target, { children: /* @__PURE__ */ jsxRuntime.jsx(
            ActionIcon.ActionIcon,
            {
              "aria-label": "Copy",
              variant: "subtle",
              size: "sm",
              ml: 8,
              display: "inline-block",
              onClick: () => {
                copy();
              },
              children: copied ? /* @__PURE__ */ jsxRuntime.jsx(index.IconCheck, { size: 14 }) : /* @__PURE__ */ jsxRuntime.jsx(index.IconCopy01, { size: 14, strokeWidth: 2.5 })
            }
          ) }),
          /* @__PURE__ */ jsxRuntime.jsx(HoverCard.HoverCard.Dropdown, { p: "xs", children: copied ? "Copied" : "Copy" })
        ] }) })
      ]
    }
  );
};
exports.CodeBlock = CodeBlock;
exports.CopyText = CopyText;
