/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.98.7
 * Released under the MIT license.
 */

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Highlight } from 'prism-react-renderer';
import { forwardRef } from 'react';
import { CONTAINER_STYLES, tasty, } from '../../../tasty';
import { ensureYamlSqlExtensions, Prism } from './prismSetup';
import 'prismjs/components/prism-javascript';
import 'prismjs/components/prism-yaml';
import 'prismjs/components/prism-bash';
import 'prismjs/components/prism-sql';
const PreElement = tasty({
    as: 'pre',
    qa: 'CodeBlock',
    styleProps: CONTAINER_STYLES.concat([]),
    className: 'cube-prism-code',
    styles: {
        margin: 0,
        padding: 0,
        overflow: 'auto',
        scrollbar: 'styled',
        Code: {
            display: 'block',
        },
    },
});
function isDiffCode(code) {
    // Split the code into lines
    const lines = code.split('\n');
    // Define patterns to check for diff characteristics
    const additionPattern = /^\+/; // Lines starting with '+'
    const deletionPattern = /^-+/; // Lines starting with '-'
    const headerPattern = /^(diff --git|---|\+\+\+)/; // Diff headers
    // Check each line for diff-specific patterns
    for (const line of lines) {
        if (additionPattern.test(line) ||
            deletionPattern.test(line) ||
            headerPattern.test(line)) {
            return true; // Code matches a diff pattern
        }
    }
    // No diff-specific patterns found
    return false;
}
function PrismCode(props, ref) {
    let { code = '', language = 'javascript', ...otherProps } = props;
    if (!code) {
        code = '';
    }
    if (typeof code !== 'string' && code) {
        throw new Error('UIKit: code prop should be a string in PrismCode. Found: ' + typeof code);
    }
    const isDiff = isDiffCode(code || '');
    const grammarLang = isDiff ? `diff-${language}` : language;
    // Ensure the diff language exists before rendering
    if (isDiff && !Prism.languages[grammarLang]) {
        Prism.languages[grammarLang] = Prism.languages.diff;
    }
    // Ensure YAML SQL extensions are applied for YAML content
    if (language === 'yaml' || grammarLang === 'diff-yaml') {
        ensureYamlSqlExtensions();
    }
    return (_jsx(PreElement, { ref: ref, ...otherProps, children: _jsx(Highlight, { prism: Prism, code: code, language: grammarLang, children: ({ className, style, tokens, getLineProps, getTokenProps }) => {
                return (_jsx("code", { "data-element": "Code", className: `${className}${isDiff ? ' diff-highlight' : ''}`, style: {
                        ...style,
                        color: undefined,
                        backgroundColor: undefined,
                    }, children: tokens.map((line, i) => {
                        const { key: _lineKey, ...lineProps } = getLineProps({
                            line,
                            key: i,
                        });
                        return (_jsxs("span", { ...lineProps, style: {
                                ...lineProps.style,
                                color: undefined,
                            }, children: [line.map((token, key) => {
                                    const { key: _tokenKey, ...tokenProps } = getTokenProps({
                                        token,
                                        key,
                                    });
                                    return (_jsx("span", { ...tokenProps, style: {
                                            ...tokenProps.style,
                                            color: undefined,
                                            backgroundColor: undefined,
                                        } }, key));
                                }), '\n'] }, i));
                    }) }));
            } }) }));
}
/**
 * Code block with syntax highlighting
 */
const _PrismCode = forwardRef(PrismCode);
_PrismCode.displayName = 'PrismCode';
export { _PrismCode as PrismCode };

        