"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require$$0 = require("react/jsx-runtime");
const designSystem = require("@choiceform/design-system");
const React = require("react");
const useCodeBlock = require("./hooks/use-code-block.cjs");
const useLineCount = require("./hooks/use-line-count.cjs");
const useScrollDetection = require("./hooks/use-scroll-detection.cjs");
const useStickToBottom = require("../../../_vendor/use-stick-to-bottom/dist-useStickToBottom.cjs");
const CodeBlock = React.memo(function CodeBlock2(props) {
  const {
    children,
    className,
    filename,
    language = "code",
    lineThreshold = 20,
    expandable = true,
    defaultExpanded = true,
    defaultCodeExpanded = false,
    onExpandChange,
    onCodeExpandChange
  } = props;
  const { scrollRef, contentRef, scrollToBottom } = useStickToBottom.useStickToBottom({
    resize: "smooth",
    initial: "smooth"
  });
  const codeContentRef = React.useRef("");
  const {
    isExpanded,
    codeExpanded,
    copied,
    handleExpand,
    handleCodeExpand,
    handleCopy: originalHandleCopy
  } = useCodeBlock.useCodeBlock({
    defaultExpanded,
    defaultCodeExpanded,
    onExpandChange,
    onCodeExpandChange,
    scrollToBottom
  });
  const handleCopy = React.useCallback(
    (code) => {
      const codeToUse = code || codeContentRef.current;
      if (codeToUse) {
        originalHandleCopy(codeToUse);
      }
    },
    [originalHandleCopy]
  );
  React.useEffect(() => {
    React.Children.forEach(children, (child) => {
      if (React.isValidElement(child)) {
        if (child.props?.code && typeof child.props.code === "string") {
          codeContentRef.current = child.props.code;
        }
      }
    });
  }, [children]);
  const lineCount = useLineCount.useLineCount(children);
  const needsScroll = useScrollDetection.useScrollDetection({
    scrollRef,
    contentRef,
    isExpanded,
    codeExpanded,
    children
  });
  const contextValue = {
    language,
    filename,
    lineCount,
    isExpanded,
    codeExpanded,
    copied,
    needsScroll,
    expandable,
    lineThreshold,
    handleExpand,
    handleCodeExpand,
    handleCopy,
    scrollRef,
    contentRef
  };
  const injectedChildren = React.Children.map(children, (child) => {
    if (React.isValidElement(child)) {
      return React.cloneElement(
        child,
        {
          codeBlock: contextValue
        }
      );
    }
    return child;
  });
  return /* @__PURE__ */ require$$0.jsx(
    "div",
    {
      className: designSystem.tcx(
        "bg-default-background group relative overflow-hidden rounded-lg border",
        className
      ),
      children: injectedChildren
    }
  );
});
exports.CodeBlock = CodeBlock;
//# sourceMappingURL=code-block.cjs.map
