function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
import json5 from "json5";
import React, { useMemo } from "react";
import { normalizeFileMapPropsFromJson } from "../../MarkdownEditor/editor/elements/AgenticUiBlocks/agenticUiEmbedUtils";
import partialParse from "../../MarkdownEditor/editor/parser/json-parse";
import { FileMapView } from "../../MarkdownInputField/FileMapView";
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-filemap``` 代码块 → FileMapView
 */ export var AgenticUiFileMapBlockRenderer = function AgenticUiFileMapBlockRenderer(props) {
    var fileMapConfig = props.fileMapConfig, rest = _object_without_properties(props, [
        "fileMapConfig"
    ]);
    var code = useMemo(function() {
        return extractTextContent(rest.children);
    }, [
        rest.children
    ]);
    var parsed = useMemo(function() {
        return parseJsonBody(code);
    }, [
        code
    ]);
    var _useMemo = useMemo(function() {
        return normalizeFileMapPropsFromJson(parsed, fileMapConfig === null || fileMapConfig === void 0 ? void 0 : fileMapConfig.normalizeFile);
    }, [
        parsed,
        fileMapConfig === null || fileMapConfig === void 0 ? void 0 : fileMapConfig.normalizeFile
    ]), fileList = _useMemo.fileList, className = _useMemo.className;
    var fileMap = useMemo(function() {
        return new Map(fileList.map(function(f) {
            return [
                f.uuid || f.name,
                f
            ];
        }));
    }, [
        fileList
    ]);
    if (parsed === null) {
        return /*#__PURE__*/ React.createElement("pre", {
            "data-testid": "agentic-ui-filemap-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-filemap-block",
        style: {
            margin: '0.75em 0'
        }
    }, /*#__PURE__*/ React.createElement(FileMapView, {
        fileMap: fileMap,
        className: className,
        onPreview: fileMapConfig === null || fileMapConfig === void 0 ? void 0 : fileMapConfig.onPreview,
        itemRender: fileMapConfig === null || fileMapConfig === void 0 ? void 0 : fileMapConfig.itemRender
    }));
};
AgenticUiFileMapBlockRenderer.displayName = 'AgenticUiFileMapBlockRenderer';
