import React from "react";
import { FileMapView } from "../MarkdownInputField/FileMapView";
var DEFAULT_DOWNLOAD_FILENAME = 'download';
var openFileInNewWindow = function(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') return;
    window.open(url, '_blank');
};
var downloadFile = function(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) || DEFAULT_DOWNLOAD_FILENAME;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
};
var defaultHandlers = {
    onPreview: openFileInNewWindow,
    onDownload: downloadFile,
    onViewAll: function() {}
};
var renderMoreAction = function(cfg, file) {
    if (!cfg) return undefined;
    if (/*#__PURE__*/ React.isValidElement(cfg)) return cfg;
    if (typeof cfg !== 'function') return cfg;
    try {
        var result = cfg.length === 0 ? cfg() : cfg(file);
        return typeof result === 'function' ? result(file) : result;
    } catch (unused) {
        return undefined;
    }
};
var createViewAllHandler = function(handler) {
    if (!handler) return undefined;
    return function(files) {
        handler(files);
        return false;
    };
};
/**
 * BubbleFileView 组件
 *
 * 展示聊天气泡中的文件列表
 *
 * @example
 * ```tsx
 * <BubbleFileView bubble={bubbleData} placement="left" />
 * ```
 */ export var BubbleFileView = function(param) {
    var bubble = param.bubble, placement = param.placement;
    var originData = bubble.originData, fileViewEvents = bubble.fileViewEvents, _bubble_fileViewConfig = bubble.fileViewConfig, fileViewConfig = _bubble_fileViewConfig === void 0 ? {} : _bubble_fileViewConfig;
    if (!(originData === null || originData === void 0 ? void 0 : originData.fileMap) || originData.fileMap.size === 0) return null;
    var events = {};
    try {
        events = (fileViewEvents === null || fileViewEvents === void 0 ? void 0 : fileViewEvents(defaultHandlers)) || {};
    } catch (unused) {
        console.warn('fileViewEvents execution failed');
    }
    return /*#__PURE__*/ React.createElement(FileMapView, {
        className: fileViewConfig.className,
        style: fileViewConfig.style,
        maxDisplayCount: fileViewConfig.maxDisplayCount,
        showMoreButton: fileViewConfig.showMoreButton,
        onPreview: events.onPreview,
        onDownload: events.onDownload,
        onViewAll: createViewAllHandler(events.onViewAll),
        renderMoreAction: fileViewConfig.renderFileMoreAction ? function(file) {
            return renderMoreAction(fileViewConfig.renderFileMoreAction, file);
        } : undefined,
        customSlot: fileViewConfig.customSlot,
        placement: placement,
        fileMap: originData.fileMap,
        "data-testid": "file-item"
    });
};
