"use client";
import { jsx } 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 { createContext, forwardRef, useState, useCallback, useContext } from 'react';
import styles from './code-block.module.css.js';

const CodeBlockContext = createContext(undefined);
const useCodeBlockContext = () => {
    const context = useContext(CodeBlockContext);
    if (!context) {
        throw new Error('useCodeBlockContext must be used within a CodeBlock');
    }
    return context;
};
const CodeBlockRoot = forwardRef(({ children, value: providedValue, onValueChange, defaultValue, hideLineNumbers = false, maxLines, maxHeight, className, collapsed: providedCollapsed, defaultCollapsed = true, onCollapseChange, ...props }, ref) => {
    const [internalValue, setInternalValue] = useState(defaultValue);
    const [code, setCode] = useState();
    const [internalCollapsed, setInternalCollapsed] = useState(defaultCollapsed ?? false);
    const collapsed = providedCollapsed ?? internalCollapsed;
    const value = providedValue ?? internalValue ?? defaultValue;
    const setValue = useCallback((newValue) => {
        setInternalValue(newValue);
        onValueChange?.(newValue);
    }, [onValueChange]);
    const toggleCollapsed = useCallback(() => {
        setInternalCollapsed(_internalCollapsed => {
            onCollapseChange?.(!_internalCollapsed);
            return !_internalCollapsed;
        });
    }, [onCollapseChange]);
    return (jsx(CodeBlockContext.Provider, { value: {
            hideLineNumbers,
            value,
            setValue,
            code,
            setCode,
            maxLines: maxLines && maxLines + 1, // to compensate for the absolute collapse trigger
            collapsed,
            toggleCollapsed
        }, children: jsx("div", { ref: ref, className: cx(styles.container, className), ...props, children: children }) }));
});
CodeBlockRoot.displayName = 'CodeBlock';

export { CodeBlockRoot, useCodeBlockContext };
//# sourceMappingURL=code-block-root.js.map
