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 parseBody = function parseBody(body) {
    try {
        return json5.parse(body || '{}');
    } catch (unused) {
        try {
            return partialParse(body || '{}');
        } catch (unused) {
            return null;
        }
    }
};
var FilemapItem = function FilemapItem(param) {
    var body = param.body, fileViewConfig = param.fileViewConfig, fileViewEvents = param.fileViewEvents, fileMapConfig = param.fileMapConfig, placement = param.placement;
    var _ref, _ref1, _ref2;
    var parsed = useMemo(function() {
        return parseBody(body);
    }, [
        body
    ]);
    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
    ]);
    var defaultHandlers = useMemo(function() {
        return {
            onPreview: function onPreview(file) {
                var url = (file === null || file === void 0 ? void 0 : file.previewUrl) || (file === null || file === void 0 ? void 0 : file.url);
                if (url && typeof window !== 'undefined') window.open(url, '_blank');
            },
            onDownload: function onDownload(file) {
                var url = (file === null || file === void 0 ? void 0 : file.url) || (file === null || file === void 0 ? void 0 : file.previewUrl);
                if (!url || typeof document === 'undefined') return;
                var link = document.createElement('a');
                link.href = url;
                link.download = (file === null || file === void 0 ? void 0 : file.name) || 'download';
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            },
            onViewAll: function onViewAll() {}
        };
    }, []);
    var events = {};
    try {
        events = (fileViewEvents === null || fileViewEvents === void 0 ? void 0 : fileViewEvents(defaultHandlers)) || {};
    } catch (unused) {}
    if (parsed === null || fileMap.size === 0) return null;
    return /*#__PURE__*/ React.createElement(FileMapView, {
        fileMap: fileMap,
        className: className !== null && className !== void 0 ? className : fileViewConfig === null || fileViewConfig === void 0 ? void 0 : fileViewConfig.className,
        style: fileViewConfig === null || fileViewConfig === void 0 ? void 0 : fileViewConfig.style,
        placement: placement,
        onPreview: (_ref = (_ref1 = events === null || events === void 0 ? void 0 : events.onPreview) !== null && _ref1 !== void 0 ? _ref1 : fileMapConfig === null || fileMapConfig === void 0 ? void 0 : fileMapConfig.onPreview) !== null && _ref !== void 0 ? _ref : defaultHandlers.onPreview,
        onDownload: events === null || events === void 0 ? void 0 : events.onDownload,
        itemRender: (_ref2 = fileViewConfig === null || fileViewConfig === void 0 ? void 0 : fileViewConfig.itemRender) !== null && _ref2 !== void 0 ? _ref2 : fileMapConfig === null || fileMapConfig === void 0 ? void 0 : fileMapConfig.itemRender,
        maxDisplayCount: fileViewConfig === null || fileViewConfig === void 0 ? void 0 : fileViewConfig.maxDisplayCount,
        showMoreButton: fileViewConfig === null || fileViewConfig === void 0 ? void 0 : fileViewConfig.showMoreButton,
        customSlot: fileViewConfig === null || fileViewConfig === void 0 ? void 0 : fileViewConfig.customSlot,
        renderMoreAction: fileViewConfig === null || fileViewConfig === void 0 ? void 0 : fileViewConfig.renderFileMoreAction
    });
};
/**
 * 将从 markdown content 中提取出的 agentic-ui-filemap 块渲染为 FileMapView 列表。
 * 渲染在气泡内容框外部，避免图片被包在带样式的气泡背景里。
 */ export var ContentFilemapView = function ContentFilemapView(param) {
    var blocks = param.blocks, fileViewConfig = param.fileViewConfig, fileViewEvents = param.fileViewEvents, fileMapConfig = param.fileMapConfig, placement = param.placement, style = param.style;
    if (blocks.length === 0) return null;
    return /*#__PURE__*/ React.createElement("div", {
        style: style,
        "data-testid": "content-filemap-view"
    }, blocks.map(function(block, i) {
        return /*#__PURE__*/ React.createElement(FilemapItem, {
            key: i,
            body: block.body,
            fileViewConfig: fileViewConfig,
            fileViewEvents: fileViewEvents,
            fileMapConfig: fileMapConfig,
            placement: placement
        });
    }));
};
