"use client";
'use strict';

var jsxRuntime = require('react/jsx-runtime');
var index = require('../../node_modules/.pnpm/class-variance-authority@0.7.1/node_modules/class-variance-authority/dist/index.cjs');
var index$1 = require('../../node_modules/.pnpm/prism-react-renderer@2.4.1_react@19.1.1/node_modules/prism-react-renderer/dist/index.cjs');
var React = require('react');
var useIsomorphicLayoutEffect = require('../../hooks/useIsomorphicLayoutEffect.cjs');
var codeBlockRoot = require('./code-block-root.cjs');
var codeBlock_module = require('./code-block.module.css.cjs');
var code_module = require('./code.module.css.cjs');

const emptyTheme = { plain: {}, styles: [] };
const CodeBlockCode = React.forwardRef(({ children, language, className, value, ...props }, ref) => {
    const { value: contextValue, setCode, setValue } = codeBlockRoot.useCodeBlockContext();
    const computedValue = value ?? language;
    const isContextValueDefined = !!contextValue;
    const shouldRender = !isContextValueDefined || contextValue === computedValue;
    const content = children.trim();
    useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(() => {
        // if value is not defined, set the value
        if (!isContextValueDefined)
            setValue(language);
        // if should render, store the code
        if (shouldRender)
            setCode(content);
    }, [
        content,
        setCode,
        shouldRender,
        setValue,
        language,
        isContextValueDefined
    ]);
    if (!shouldRender)
        return null;
    return (jsxRuntime.jsx("div", { ref: ref, className: index.cx(codeBlock_module.default.codeContent, className), ...props, children: jsxRuntime.jsx(CodeHighlight, { content: content, language: language }) }));
});
CodeBlockCode.displayName = 'CodeBlockCode';
const CodeHighlight = React.memo(({ content, language }) => {
    const { hideLineNumbers, maxLines, collapsed } = codeBlockRoot.useCodeBlockContext();
    const canCollapse = maxLines && maxLines > 0;
    return (jsxRuntime.jsx(index$1.Highlight, { theme: emptyTheme, code: content, language: language, children: ({ className: highlightClassName, style, tokens, getLineProps, getTokenProps }) => {
            const renderedTokens = canCollapse && collapsed ? tokens.slice(0, maxLines) : tokens;
            return (jsxRuntime.jsx("pre", { className: index.cx(code_module.default.theme, codeBlock_module.default.pre, highlightClassName), style: style, children: renderedTokens.map((line, i) => (jsxRuntime.jsxs("div", { ...getLineProps({ line }), children: [!hideLineNumbers && (jsxRuntime.jsx("span", { className: codeBlock_module.default.lineNumber, children: i + 1 })), jsxRuntime.jsx("span", { className: codeBlock_module.default.lineContent, children: line.map((token, key) => (jsxRuntime.jsx("span", { ...getTokenProps({ token }) }, key))) })] }, i))) }));
        } }));
});

exports.CodeBlockCode = CodeBlockCode;
//# sourceMappingURL=code-block-code.cjs.map
