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 _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 _type_of(obj) {
    "@swc/helpers - typeof";
    return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
import React, { useContext, useMemo } from "react";
import { BubbleConfigContext } from "../../../../Bubble/BubbleConfigProvide";
import { SchemaRenderer } from "../../../../Schema";
import { debugInfo } from "../../../../Utils/debugUtils";
import { useEditorStore } from "../../store";
/**
 * Schema 组件 - 模式渲染组件
 *
 * 该组件用于渲染 Schema 类型的代码节点，支持自定义渲染、AgentAR 卡片渲染和默认渲染模式。
 * 根据不同的配置和节点类型提供不同的渲染方式。
 *
 * 功能特性：
 * - 支持自定义 apaasify/apassify 渲染模式
 * - 支持 AgentAR 卡片渲染
 * - 提供默认的 JSON 字符串渲染
 * - 包含隐藏的 JSON 数据用于调试和编辑
 * - 支持点击和键盘事件处理
 * - 响应式布局设计
 * - 提供测试 ID 支持
 *
 * @param props - Slate.js 渲染元素属性
 * @param props.element - 代码节点元素，包含 Schema 数据
 * @param props.children - 子组件，通常是文本内容
 * @param props.attributes - 元素的 DOM 属性
 * @returns 渲染的模式组件
 *
 * @example
 * ```tsx
 * <Schema
 *   element={codeNode}
 *   attributes={attributes}
 * >
 *   {children}
 * </Schema>
 * ```
 *
 * @remarks
 * 渲染优先级：
 * 1. 自定义 apaasify 渲染器
 * 2. AgentAR 卡片渲染
 * 3. 默认 JSON 字符串渲染
 */ export var Schema = function(props) {
    debugInfo('Schema - 渲染 Schema', {
        language: props.element.language,
        valueType: _type_of(props.element.value),
        hasApaasify: !!props.element.apaasify
    });
    var node = props.element;
    var editorProps = useEditorStore().editorProps;
    var apaasify = (editorProps === null || editorProps === void 0 ? void 0 : editorProps.apaasify) || (editorProps === null || editorProps === void 0 ? void 0 : editorProps.apassify);
    var bubble = (useContext(BubbleConfigContext) || {}).bubble;
    return useMemo(function() {
        if ((apaasify === null || apaasify === void 0 ? void 0 : apaasify.enable) && apaasify.render) {
            debugInfo('Schema - 使用自定义 apaasify 渲染');
            var renderedContent = apaasify.render(props, bubble === null || bubble === void 0 ? void 0 : bubble.originData);
            return /*#__PURE__*/ React.createElement("div", _object_spread_props(_object_spread({}, node.attributes), {
                "data-testid": "schema-container",
                contentEditable: false,
                style: {
                    display: 'flex',
                    flexDirection: 'column',
                    userSelect: 'text',
                    WebkitUserSelect: 'text'
                }
            }), renderedContent, /*#__PURE__*/ React.createElement("div", {
                "data-testid": "schema-hidden-json",
                style: {
                    height: 1,
                    opacity: 0,
                    userSelect: 'none',
                    pointerEvents: 'none',
                    overflow: 'hidden'
                }
            }, JSON.stringify(props.element.value, null, 2)));
        }
        if (node.language === 'agentar-card') {
            var _props_element_value;
            debugInfo('Schema - 使用 AgentAR 卡片渲染');
            return /*#__PURE__*/ React.createElement("div", {
                "data-testid": "agentar-card-container",
                style: {
                    padding: '0.5em'
                },
                "data-agentar-card": true
            }, /*#__PURE__*/ React.createElement(SchemaRenderer, {
                debug: false,
                fallbackContent: null,
                schema: props.element.value,
                values: ((_props_element_value = props.element.value) === null || _props_element_value === void 0 ? void 0 : _props_element_value.initialValues) || {},
                useDefaultValues: false
            }));
        }
        debugInfo('Schema - 使用默认 JSON 渲染');
        return /*#__PURE__*/ React.createElement("div", _object_spread_props(_object_spread({}, node.attributes), {
            "data-testid": "schema-container",
            style: {
                display: 'flex',
                flexDirection: 'column',
                width: '100%'
            }
        }), /*#__PURE__*/ React.createElement("div", {
            "data-testid": "schema-clickable",
            style: {
                padding: 8,
                width: '100%',
                cursor: 'pointer',
                position: 'relative',
                display: 'flex',
                borderRadius: 8,
                flex: 1,
                border: '1px solid rgb(209 213 219 / 0.8)',
                alignItems: 'center'
            },
            onClick: function(e) {
                e.stopPropagation();
            },
            onMouseMove: function(e) {
                e.stopPropagation();
            },
            onKeyDown: function(e) {
                e.stopPropagation();
            },
            "data-be": node === null || node === void 0 ? void 0 : node.type
        }, typeof (node === null || node === void 0 ? void 0 : node.value) === 'string' ? node.value : JSON.stringify(node === null || node === void 0 ? void 0 : node.value, null, 2)), /*#__PURE__*/ React.createElement("span", {
            "data-testid": "schema-hidden-children",
            style: {
                display: 'none'
            }
        }, props.children));
    }, [
        node.value,
        bubble,
        apaasify
    ]);
};
