import { jsxs, jsx } from "react/jsx-runtime";
import { useMemo, useState } from "react";
import { IconChevronVerticalExpand, IconChevronVerticalShrink, IconCheck, IconCopy01 } from "../../icons/index.mjs";
/* empty css                                                                                                                                                                                       */
/* empty css                                                                                                                                                                                        */
/* empty css                                                                                                                                                                                                */
/* empty css                                                                                                                                                                                           */
/* empty css                                                                                                                                                                                           */
import { CopyButton } from "../../primitive/CopyButton/CopyButton.mjs";
import "clsx";
import { mergeSxList, mergeStylesList } from "../../utils/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 { Prism } from "../../primitive/Prism/Prism.mjs";
import { Group } 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/Group/Group.mjs";
import { HoverCard } 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/HoverCard/HoverCard.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 { Code } 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/Code/Code.mjs";
import { useLocalStorage } from "../../node_modules/.pnpm/@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-local-storage/use-local-storage.mjs";
function useFold(persistenceKey) {
  const foldPersistenceKey = `${persistenceKey}.codeblock.fold`;
  const [persistentFolded, setPersistentFolded] = useLocalStorage({
    key: foldPersistenceKey,
    defaultValue: true,
    getInitialValueInEffect: false
  });
  const [memoryFolded, setMemoryFolded] = 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 = 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__ */ jsxs(Box, { ...rest, sx: mergeSxList([{ position: "relative" }, rest == null ? void 0 : rest.sx]), children: [
    /* @__PURE__ */ jsx(
      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__ */ jsx(Box, { sx: { paddingRight: codeScrollAreaPadding }, children: codeRender ? codeRender(children) : /* @__PURE__ */ jsx(
          Prism,
          {
            ...codeHighlightProps,
            noCopy: true,
            language,
            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__ */ jsxs(Group, { gap: 4, sx: (theme) => ({ position: "absolute", top: 16, right: 16, color: theme.colors.carbon[8] }), children: [
      foldIconVisible && /* @__PURE__ */ jsxs(HoverCard, { withArrow: true, position: "top", shadow: "xs", children: [
        /* @__PURE__ */ jsx(HoverCard.Target, { children: /* @__PURE__ */ jsx(
          ActionIcon,
          {
            size: "sm",
            variant: "subtle",
            onClick: () => {
              const v = !folded;
              setFolded(v);
              onFoldIconClick == null ? void 0 : onFoldIconClick(v);
            },
            children: folded ? /* @__PURE__ */ jsx(IconChevronVerticalExpand, { size: 14, strokeWidth: 2.5 }) : /* @__PURE__ */ jsx(IconChevronVerticalShrink, { size: 14, strokeWidth: 2.5 })
          }
        ) }),
        /* @__PURE__ */ jsx(HoverCard.Dropdown, { p: "xs", children: folded ? "Expand" : "Collapse" })
      ] }),
      /* @__PURE__ */ jsx(CopyButton, { value: copyContent ?? children, timeout: 2e3, children: ({ copied, copy }) => /* @__PURE__ */ jsxs(HoverCard, { withArrow: true, position: "top", shadow: "xs", children: [
        /* @__PURE__ */ jsx(HoverCard.Target, { children: /* @__PURE__ */ jsx(
          ActionIcon,
          {
            "aria-label": "Copy",
            size: "sm",
            variant: "subtle",
            onClick: () => {
              copy();
              onCopyClick == null ? void 0 : onCopyClick();
            },
            children: copied ? /* @__PURE__ */ jsx(IconCheck, { size: 14 }) : /* @__PURE__ */ jsx(IconCopy01, { size: 14, strokeWidth: 2.5 })
          }
        ) }),
        /* @__PURE__ */ jsx(HoverCard.Dropdown, { p: "xs", children: copied ? "Copied" : "Copy" })
      ] }) })
    ] })
  ] });
};
const CopyText = ({ children, value, ...rest }) => {
  return /* @__PURE__ */ jsxs(
    Code,
    {
      bg: "carbon.3",
      ...rest,
      p: 8,
      sx: (theme, u) => {
        return mergeSxList([
          {
            display: "inline-flex",
            position: "relative",
            justifyContent: "center",
            alignItems: "center",
            borderRadius: theme.defaultRadius
          },
          rest == null ? void 0 : rest.sx
        ])(theme, u);
      },
      children: [
        children,
        /* @__PURE__ */ jsx(CopyButton, { value, timeout: 2e3, children: ({ copied, copy }) => /* @__PURE__ */ jsxs(HoverCard, { withArrow: true, position: "top", shadow: "xs", children: [
          /* @__PURE__ */ jsx(HoverCard.Target, { children: /* @__PURE__ */ jsx(
            ActionIcon,
            {
              "aria-label": "Copy",
              variant: "subtle",
              size: "sm",
              ml: 8,
              display: "inline-block",
              onClick: () => {
                copy();
              },
              children: copied ? /* @__PURE__ */ jsx(IconCheck, { size: 14 }) : /* @__PURE__ */ jsx(IconCopy01, { size: 14, strokeWidth: 2.5 })
            }
          ) }),
          /* @__PURE__ */ jsx(HoverCard.Dropdown, { p: "xs", children: copied ? "Copied" : "Copy" })
        ] }) })
      ]
    }
  );
};
export {
  CodeBlock,
  CopyText
};
