function _type_of(obj) {
    "@swc/helpers - typeof";
    return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
import React, { useMemo } from "react";
import { SchemaRenderer } from "../../Schema";
import { debugInfo } from "../../Utils/debugUtils";
import { extractBlockTextContent } from "../extractBlockTextContent";
import { parseSchemaJson } from "./utils/parseJsonBody";
/** 从 schemaValue 安全提取 `initialValues`（仅当为对象且字段存在时返回） */ var extractInitialValues = function extractInitialValues(value) {
    if (value && !Array.isArray(value) && _type_of(value.initialValues) === 'object' && value.initialValues !== null) {
        return value.initialValues;
    }
    return {};
};
export var SchemaBlockRenderer = function SchemaBlockRenderer(props) {
    var children = props.children, language = props.language, apaasifyRender = props.apaasifyRender, editorCodeProps = props.editorCodeProps;
    var code = extractBlockTextContent(children);
    var schemaValue = useMemo(function() {
        return parseSchemaJson(code);
    }, [
        code
    ]);
    var applyCodeRender = function applyCodeRender(defaultDom, valueForElement) {
        var customRender = editorCodeProps === null || editorCodeProps === void 0 ? void 0 : editorCodeProps.render;
        if (!customRender) return defaultDom;
        // customRender 接收 Slate-like 结构。MarkdownRenderer 不依赖 Slate，构造一个
        // 形状兼容的对象即可；因 customRender 的 `props` 类型为 `CustomLeaf<...> & { children }`，
        // 与此处 `element` 字段类型并不严格匹配，故在调用边界保留一次必要的类型断言。
        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: extractInitialValues(schemaValue),
            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: extractInitialValues(schemaValue),
        useDefaultValues: false,
        debug: false,
        fallbackContent: null
    }));
    return applyCodeRender(schemaDom, schemaValue);
};
SchemaBlockRenderer.displayName = 'SchemaBlockRenderer';
