function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import { ChevronsUpDown, Copy, Moon } from "@sofa-design/icons";
import copy from "copy-to-clipboard";
import React, { useCallback, useContext, useMemo, useState } from "react";
import { ActionIconBox } from "../../Components/ActionIconBox";
import { I18nContext } from "../../I18n";
import { CodeContainer } from "../../Plugins/code/components/CodeContainer";
import { LoadImage } from "../../Plugins/code/components/LoadImage";
import { langIconMap } from "../../Plugins/code/langIconMap";
import { debugInfo } from "../../Utils/debugUtils";
var extractTextContent = function extractTextContent1(children) {
    var _children_props;
    if (typeof children === 'string') return children;
    if (typeof children === 'number') return String(children);
    if (Array.isArray(children)) return children.map(extractTextContent).join('');
    if (/*#__PURE__*/ React.isValidElement(children) && ((_children_props = children.props) === null || _children_props === void 0 ? void 0 : _children_props.children)) {
        return extractTextContent(children.props.children);
    }
    return '';
};
/**
 * 代码块渲染器——复用 MarkdownEditor 的 CodeContainer 和样式体系。
 * 不依赖 Slate 上下文，提供与 CodeRenderer readonly 模式一致的视觉效果。
 */ export var CodeBlockRenderer = function CodeBlockRenderer(props) {
    var _i18n_locale, _i18n_locale1, _i18n_locale2;
    var language = props.language, children = props.children, editorCodeProps = props.editorCodeProps;
    var _useState = _sliced_to_array(useState(function() {
        return (editorCodeProps === null || editorCodeProps === void 0 ? void 0 : editorCodeProps.theme) || 'github';
    }), 2), theme = _useState[0], setTheme = _useState[1];
    var _useState1 = _sliced_to_array(useState(true), 2), isExpanded = _useState1[0], setIsExpanded = _useState1[1];
    var i18n = useContext(I18nContext);
    var code = useMemo(function() {
        return extractTextContent(children);
    }, [
        children
    ]);
    var fakeElement = useMemo(function() {
        return {
            type: 'code',
            language: language || '',
            value: code,
            children: [
                {
                    text: code
                }
            ]
        };
    }, [
        language,
        code
    ]);
    var handleCopy = useCallback(function() {
        try {
            copy(code);
        } catch (error) {
            console.error('复制失败:', error);
        }
    }, [
        code
    ]);
    var langIcon = langIconMap.get((language === null || language === void 0 ? void 0 : language.toLowerCase()) || '');
    var defaultDom = /*#__PURE__*/ React.createElement(CodeContainer, {
        element: fakeElement,
        showBorder: false,
        hide: false,
        onEditorClick: function onEditorClick() {},
        theme: theme
    }, /*#__PURE__*/ React.createElement("div", {
        "data-testid": "code-toolbar",
        contentEditable: false,
        style: {
            borderTopLeftRadius: 'inherit',
            borderTopRightRadius: 'inherit',
            backgroundColor: 'transparent',
            paddingLeft: '0.25em',
            paddingRight: '0.25em',
            width: '100%',
            position: 'sticky',
            left: 0,
            top: 0,
            fontSize: '1em',
            font: 'var(--font-text-h6-base)',
            color: 'inherit',
            justifyContent: 'space-between',
            zIndex: 50,
            height: '38px',
            display: 'flex',
            alignItems: 'center',
            padding: '9px 12px',
            gap: '16px',
            alignSelf: 'stretch',
            boxSizing: 'border-box',
            userSelect: 'none',
            borderBottom: isExpanded ? theme === 'chaos' ? '1px solid #161616' : '1px solid var(--color-gray-border-light)' : 'none'
        }
    }, /*#__PURE__*/ React.createElement("div", {
        style: {
            display: 'flex',
            alignItems: 'center',
            gap: 8
        }
    }, /*#__PURE__*/ React.createElement("div", {
        style: {
            display: 'flex',
            alignItems: 'center',
            cursor: 'pointer',
            gap: 4,
            font: 'inherit',
            color: 'inherit',
            userSelect: 'none'
        }
    }, langIcon && /*#__PURE__*/ React.createElement("div", {
        style: {
            height: '1em',
            width: '1em',
            fontSize: '16px',
            display: 'flex'
        }
    }, /*#__PURE__*/ React.createElement(LoadImage, {
        style: {
            height: '1em',
            width: '1em'
        },
        src: langIcon
    })), /*#__PURE__*/ React.createElement("span", null, language || 'plain text'))), /*#__PURE__*/ React.createElement("div", {
        style: {
            display: 'flex',
            gap: 5,
            alignItems: 'center'
        }
    }, /*#__PURE__*/ React.createElement(ActionIconBox, {
        title: (i18n === null || i18n === void 0 ? void 0 : (_i18n_locale = i18n.locale) === null || _i18n_locale === void 0 ? void 0 : _i18n_locale.theme) || '主题',
        theme: theme === 'chaos' ? 'dark' : 'light',
        onClick: function onClick() {
            return setTheme(theme === 'github' ? 'chaos' : 'github');
        }
    }, /*#__PURE__*/ React.createElement(Moon, null)), /*#__PURE__*/ React.createElement(ActionIconBox, {
        theme: theme === 'chaos' ? 'dark' : 'light',
        title: (i18n === null || i18n === void 0 ? void 0 : (_i18n_locale1 = i18n.locale) === null || _i18n_locale1 === void 0 ? void 0 : _i18n_locale1.copy) || '复制',
        style: {
            fontSize: '1em',
            lineHeight: '1.75em',
            marginLeft: '0.125em'
        },
        onClick: handleCopy
    }, /*#__PURE__*/ React.createElement(Copy, null)), /*#__PURE__*/ React.createElement(ActionIconBox, {
        title: (i18n === null || i18n === void 0 ? void 0 : (_i18n_locale2 = i18n.locale) === null || _i18n_locale2 === void 0 ? void 0 : _i18n_locale2.expandCollapse) || '展开/收起',
        theme: theme === 'chaos' ? 'dark' : 'light',
        onClick: function onClick() {
            return setIsExpanded(!isExpanded);
        }
    }, /*#__PURE__*/ React.createElement(ChevronsUpDown, null)))), /*#__PURE__*/ React.createElement("div", {
        className: "code-editor-content",
        style: {
            borderBottomLeftRadius: 'inherit',
            borderBottomRightRadius: 'inherit',
            display: isExpanded ? 'block' : 'none'
        }
    }, /*#__PURE__*/ React.createElement("div", {
        style: {
            height: '100%',
            width: '100%',
            borderRadius: 'inherit',
            padding: '12px 16px',
            overflow: 'auto',
            fontSize: '0.9em',
            lineHeight: 1.6,
            fontFamily: "'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace",
            whiteSpace: 'pre-wrap',
            wordBreak: 'break-all'
        }
    }, /*#__PURE__*/ React.createElement("code", {
        className: language ? "language-".concat(language) : undefined
    }, children))));
    var customRender = editorCodeProps === null || editorCodeProps === void 0 ? void 0 : editorCodeProps.render;
    if (!customRender) {
        return defaultDom;
    }
    try {
        var renderElementProps = {
            attributes: {},
            children: null,
            element: fakeElement
        };
        var rendered = customRender(renderElementProps, defaultDom, editorCodeProps);
        if (rendered === undefined) {
            return defaultDom;
        }
        return rendered;
    } catch (error) {
        debugInfo('CodeBlockRenderer - codeProps.render 异常，回退默认', {
            error: (error === null || error === void 0 ? void 0 : error.message) || String(error)
        });
        return defaultDom;
    }
};
CodeBlockRenderer.displayName = 'CodeBlockRenderer';
