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 _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
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 _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
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 { CloseCircleOutlined } from "@ant-design/icons";
import copy from "copy-to-clipboard";
import React, { useContext, useEffect, useRef } from "react";
import { useGetSetState } from "react-use";
import { Copy } from "@sofa-design/icons";
import { ReactEditor } from "slate-react";
import { ActionIconBox } from "../../Components/ActionIconBox";
import { I18nContext } from "../../I18n";
import { useEditorStore } from "../../MarkdownEditor/editor/store";
import { useSelStatus } from "../../MarkdownEditor/hooks/editor";
import { Mermaid } from "./Mermaid";
/**
 * MermaidElement 组件 - Mermaid图表元素组件
 *
 * 该组件用于在Markdown编辑器中渲染Mermaid图表，支持流程图、时序图、甘特图等。
 * 提供图表编辑、预览、复制、拖拽等功能。
 *
 * @component
 * @description Mermaid图表元素组件，支持各种Mermaid图表类型
 * @param {ElementProps<CodeNode>} props - 组件属性
 * @param {CodeNode} props.element - 代码节点数据
 * @param {string} [props.element.language] - 图表语言类型
 * @param {string} [props.element.value] - Mermaid图表代码
 * @param {boolean} [props.element.frontmatter] - 是否为前置元数据
 * @param {Object} props.attributes - Slate元素属性
 * @param {React.ReactNode} props.children - 子元素
 *
 * @example
 * ```tsx
 * <MermaidElement
 *   element={{
 *     type: 'code',
 *     language: 'mermaid',
 *     value: 'graph TD\nA[开始] --> B[结束]'
 *   }}
 *   attributes={slateAttributes}
 *   children={slateChildren}
 * />
 * ```
 *
 * @returns {React.ReactElement} 渲染的Mermaid图表组件
 *
 * @remarks
 * - 支持多种Mermaid图表类型（流程图、时序图、甘特图等）
 * - 提供图表编辑和预览功能
 * - 支持图表代码复制
 * - 提供拖拽排序功能
 * - 支持选择状态管理
 * - 集成Ace编辑器
 * - 提供国际化支持
 * - 响应式布局适配
 */ export function MermaidElement(props) {
    var _i18n_locale, _i18n_locale1;
    var markdownEditorRef = useEditorStore().markdownEditorRef;
    var _useGetSetState = _sliced_to_array(useGetSetState({
        showBorder: false,
        htmlStr: '',
        hide: props.element.language === 'mermaid',
        lang: props.element.language || ''
    }), 2), state = _useGetSetState[0], setState = _useGetSetState[1];
    var pathRef = useRef();
    var _useSelStatus = _sliced_to_array(useSelStatus(props.element), 2), selected = _useSelStatus[0], path = _useSelStatus[1];
    pathRef.current = path;
    var editorRef = useRef();
    useEffect(function() {
        var _editorRef_current;
        if (selected && !((_editorRef_current = editorRef.current) === null || _editorRef_current === void 0 ? void 0 : _editorRef_current.isFocused()) && ReactEditor.isFocused(markdownEditorRef.current)) {
            setState({
                showBorder: true
            });
        } else if (state().showBorder) {
            setState({
                showBorder: false
            });
        }
    }, [
        selected,
        path
    ]);
    var i18n = useContext(I18nContext);
    return /*#__PURE__*/ React.createElement("div", _object_spread_props(_object_spread({}, props.attributes), {
        contentEditable: false,
        className: 'ace-el drag-el',
        "data-be": 'mermaid',
        tabIndex: -1,
        onBlur: function onBlur() {},
        "data-lang": props.element.language
    }), /*#__PURE__*/ React.createElement("div", {
        onClick: function onClick(e) {
            var _editorRef_current;
            e.stopPropagation();
            (_editorRef_current = editorRef.current) === null || _editorRef_current === void 0 ? void 0 : _editorRef_current.focus();
        },
        style: {
            padding: state().hide ? 0 : undefined,
            marginBottom: state().hide ? 0 : undefined,
            boxSizing: 'border-box',
            backgroundColor: state().showBorder ? 'rgba(59, 130, 246, 0.1)' : state().hide ? 'transparent' : 'rgb(252, 252, 252)',
            maxHeight: 400,
            overflow: 'auto',
            position: 'relative',
            height: state().hide ? 0 : 'auto',
            opacity: state().hide ? 0 : 1
        }
    }, !props.element.frontmatter && /*#__PURE__*/ React.createElement("div", {
        contentEditable: false,
        onClick: function onClick(e) {
            e.stopPropagation();
        },
        style: {
            height: '1.75em',
            backgroundColor: '#FFF',
            borderBottom: '1px solid #eee',
            paddingLeft: '0.75em',
            paddingRight: '0.375em',
            display: 'flex',
            alignItems: 'center',
            width: '100%',
            position: 'sticky',
            left: 0,
            top: 0,
            fontSize: '1em',
            color: 'rgba(0, 0, 0, 0.6)',
            justifyContent: 'space-between',
            zIndex: 50,
            userSelect: 'none'
        }
    }, /*#__PURE__*/ React.createElement("div", {
        style: {
            display: 'flex',
            gap: 5
        }
    }, /*#__PURE__*/ React.createElement(ActionIconBox, {
        title: ((_i18n_locale = i18n.locale) === null || _i18n_locale === void 0 ? void 0 : _i18n_locale.close) || '关闭',
        onClick: function onClick() {
            setState({
                hide: props.element.language === 'mermaid'
            });
        }
    }, /*#__PURE__*/ React.createElement(CloseCircleOutlined, null)), /*#__PURE__*/ React.createElement(ActionIconBox, {
        title: ((_i18n_locale1 = i18n.locale) === null || _i18n_locale1 === void 0 ? void 0 : _i18n_locale1.copy) || '复制',
        style: {
            fontSize: '0.9em',
            lineHeight: '1.75em',
            marginLeft: '0.125em'
        },
        onClick: function onClick(e) {
            e.stopPropagation();
            try {
                var code = props.element.value || '';
                copy(code);
            } catch (error) {}
        }
    }, /*#__PURE__*/ React.createElement(Copy, null)))), /*#__PURE__*/ React.createElement("div", {
        "data-hidden": true
    }, props.children)), /*#__PURE__*/ React.createElement(Mermaid, {
        element: props.element
    }));
}
