import React, { useMemo } from "react";
import partialParse from "../../MarkdownEditor/editor/parser/json-parse";
import { SchemaRenderer } from "../../Schema";
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 '';
};
/**
 * 解析 schema/apaasify JSON 内容，与 Slate parseCode 的 processSchemaLanguage 对齐
 */ var parseSchemaValue = function parseSchemaValue(code) {
    try {
        return JSON.parse(code);
    } catch (unused) {
        try {
            return partialParse(code || '[]');
        } catch (unused) {
            return null;
        }
    }
};
/**
 * Schema / Apaasify 渲染器
 * 对齐 MarkdownEditor 的 ReadonlySchema 组件。
 *
 * 处理以下代码块语言：
 * - ```schema     → SchemaRenderer
 * - ```apaasify   → SchemaRenderer / 自定义 render
 * - ```apassify   → 同 apaasify（兼容旧版）
 * - ```agentar-card → SchemaRenderer
 */ export var SchemaBlockRenderer = function SchemaBlockRenderer(props) {
    var children = props.children, language = props.language, apaasifyRender = props.apaasifyRender, editorCodeProps = props.editorCodeProps;
    var code = extractTextContent(children);
    var schemaValue = useMemo(function() {
        return parseSchemaValue(code);
    }, [
        code
    ]);
    var applyCodeRender = function applyCodeRender(defaultDom, valueForElement) {
        var customRender = editorCodeProps === null || editorCodeProps === void 0 ? void 0 : editorCodeProps.render;
        if (!customRender) return defaultDom;
        var slateLike = {
            attributes: {},
            children: null,
            element: {
                type: language === 'agentar-card' ? 'card' : 'schema',
                value: valueForElement,
                language: language
            }
        };
        try {
            var rendered = customRender(slateLike, defaultDom, editorCodeProps);
            if (rendered === undefined) return defaultDom;
            return rendered;
        } catch (error) {
            debugInfo('SchemaBlockRenderer - codeProps.render 异常，回退默认', {
                error: (error === null || error === void 0 ? void 0 : error.message) || String(error)
            });
            return defaultDom;
        }
    };
    if (!schemaValue) {
        var fallbackDom = /*#__PURE__*/ React.createElement("pre", {
            "data-testid": "schema-fallback",
            style: {
                background: 'rgb(242, 241, 241)',
                color: 'rgb(27, 27, 27)',
                padding: '1em',
                borderRadius: '0.5em',
                margin: '1em 0',
                fontSize: '0.8em',
                fontFamily: 'monospace',
                lineHeight: '1.5',
                overflowX: 'auto',
                whiteSpace: 'pre-wrap',
                wordBreak: 'break-all',
                wordWrap: 'break-word'
            }
        }, /*#__PURE__*/ React.createElement("code", null, code));
        return applyCodeRender(fallbackDom, code);
    }
    if (apaasifyRender) {
        var rendered = apaasifyRender(schemaValue);
        if (rendered !== undefined) {
            var apaasifyDom = /*#__PURE__*/ React.createElement("div", {
                "data-testid": "schema-container",
                contentEditable: false,
                style: {
                    display: 'flex',
                    flexDirection: 'column',
                    userSelect: 'text',
                    WebkitUserSelect: 'text'
                }
            }, rendered, /*#__PURE__*/ React.createElement("div", {
                "data-testid": "schema-hidden-json",
                style: {
                    height: 1,
                    opacity: 0,
                    userSelect: 'none',
                    pointerEvents: 'none',
                    overflow: 'hidden'
                }
            }, JSON.stringify(schemaValue, null, 2)));
            return applyCodeRender(apaasifyDom, schemaValue);
        }
    }
    if (language === 'agentar-card') {
        var cardDom = /*#__PURE__*/ React.createElement("div", {
            "data-testid": "agentar-card-container",
            style: {
                padding: '0.5em'
            },
            "data-agentar-card": "true"
        }, /*#__PURE__*/ React.createElement(SchemaRenderer, {
            schema: schemaValue,
            values: (schemaValue === null || schemaValue === void 0 ? void 0 : schemaValue.initialValues) || {},
            useDefaultValues: false,
            debug: false,
            fallbackContent: null
        }));
        return applyCodeRender(cardDom, schemaValue);
    }
    var schemaDom = /*#__PURE__*/ React.createElement("div", {
        "data-testid": "schema-renderer",
        style: {
            padding: '0.5em'
        }
    }, /*#__PURE__*/ React.createElement(SchemaRenderer, {
        schema: schemaValue,
        values: (schemaValue === null || schemaValue === void 0 ? void 0 : schemaValue.initialValues) || {},
        useDefaultValues: false,
        debug: false,
        fallbackContent: null
    }));
    return applyCodeRender(schemaDom, schemaValue);
};
SchemaBlockRenderer.displayName = 'SchemaBlockRenderer';
