"use client";
import { jsx, jsxs } from 'react/jsx-runtime';
import { cx } from '../../node_modules/.pnpm/class-variance-authority@0.7.1/node_modules/class-variance-authority/dist/index.js';
import { Highlight as Highlight2 } from '../../node_modules/.pnpm/prism-react-renderer@2.4.1_react@19.1.1/node_modules/prism-react-renderer/dist/index.js';
import { forwardRef, memo } from 'react';
import { useIsomorphicLayoutEffect } from '../../hooks/useIsomorphicLayoutEffect.js';
import { useCodeBlockContext } from './code-block-root.js';
import styles from './code-block.module.css.js';
import code from './code.module.css.js';

const emptyTheme = { plain: {}, styles: [] };
const CodeBlockCode = forwardRef(({ children, language, className, value, ...props }, ref) => {
    const { value: contextValue, setCode, setValue } = useCodeBlockContext();
    const computedValue = value ?? language;
    const isContextValueDefined = !!contextValue;
    const shouldRender = !isContextValueDefined || contextValue === computedValue;
    const content = children.trim();
    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 (jsx("div", { ref: ref, className: cx(styles.codeContent, className), ...props, children: jsx(CodeHighlight, { content: content, language: language }) }));
});
CodeBlockCode.displayName = 'CodeBlockCode';
const CodeHighlight = memo(({ content, language }) => {
    const { hideLineNumbers, maxLines, collapsed } = useCodeBlockContext();
    const canCollapse = maxLines && maxLines > 0;
    return (jsx(Highlight2, { theme: emptyTheme, code: content, language: language, children: ({ className: highlightClassName, style, tokens, getLineProps, getTokenProps }) => {
            const renderedTokens = canCollapse && collapsed ? tokens.slice(0, maxLines) : tokens;
            return (jsx("pre", { className: cx(code.theme, styles.pre, highlightClassName), style: style, children: renderedTokens.map((line, i) => (jsxs("div", { ...getLineProps({ line }), children: [!hideLineNumbers && (jsx("span", { className: styles.lineNumber, children: i + 1 })), jsx("span", { className: styles.lineContent, children: line.map((token, key) => (jsx("span", { ...getTokenProps({ token }) }, key))) })] }, i))) }));
        } }));
});

export { CodeBlockCode };
//# sourceMappingURL=code-block-code.js.map
