import json5 from "json5";
import React, { useMemo } from "react";
import { normalizeTaskListPropsFromJson } from "../../MarkdownEditor/editor/elements/AgenticUiBlocks/agenticUiEmbedUtils";
import partialParse from "../../MarkdownEditor/editor/parser/json-parse";
import { TaskList } from "../../TaskList";
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 '';
};
var parseJsonBody = function parseJsonBody(code) {
    try {
        return json5.parse(code || '{}');
    } catch (unused) {
        try {
            return partialParse(code || '{}');
        } catch (unused) {
            return null;
        }
    }
};
/**
 * ```agentic-ui-task``` 代码块 → TaskList（与 MarkdownEditor parseCode 对齐）
 */ export var AgenticUiTaskBlockRenderer = function AgenticUiTaskBlockRenderer(props) {
    var code = useMemo(function() {
        return extractTextContent(props.children);
    }, [
        props.children
    ]);
    var parsed = useMemo(function() {
        return parseJsonBody(code);
    }, [
        code
    ]);
    var listProps = useMemo(function() {
        return normalizeTaskListPropsFromJson(parsed);
    }, [
        parsed
    ]);
    if (parsed === null) {
        return /*#__PURE__*/ React.createElement("pre", {
            "data-testid": "agentic-ui-task-fallback",
            style: {
                background: 'rgb(242, 241, 241)',
                padding: '1em',
                borderRadius: '0.5em',
                margin: '0.75em 0',
                fontSize: '0.8em',
                whiteSpace: 'pre-wrap',
                wordBreak: 'break-all'
            }
        }, /*#__PURE__*/ React.createElement("code", null, code));
    }
    return /*#__PURE__*/ React.createElement("div", {
        "data-testid": "agentic-ui-task-block",
        style: {
            margin: '0.75em 0'
        }
    }, /*#__PURE__*/ React.createElement(TaskList, listProps));
};
AgenticUiTaskBlockRenderer.displayName = 'AgenticUiTaskBlockRenderer';
