function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import { Download, EllipsisVertical, Eye } from "@sofa-design/icons";
import { Tooltip } from "antd";
import classNames from "clsx";
import dayjs from "dayjs";
import { motion } from "framer-motion";
import React, { useContext } from "react";
import { ActionIconBox } from "../../Components/ActionIconBox";
import { I18nContext } from "../../I18n";
import { AttachmentFileIcon, FileMetaPlaceholder } from "../AttachmentButton/AttachmentFileList/AttachmentFileIcon";
import { isFileMetaPlaceholderState, kbToSize } from "../AttachmentButton/utils";
/**
 * FileMapViewItem 组件 - 文件映射视图项组件
 *
 * 该组件用于显示单个文件项，包括文件图标、文件名、文件大小等信息。
 * 支持文件预览和下载操作，提供动画效果和工具提示。
 *
 * @component
 * @description 文件映射视图项组件，显示单个文件信息
 * @param {Object} props - 组件属性
 * @param {AttachmentFile} props.file - 文件对象
 * @param {() => void} props.onPreview - 预览文件回调
 * @param {() => void} props.onDownload - 下载文件回调
 * @param {string} [props.className] - 自定义CSS类名
 * @param {string} [props.prefixCls] - 前缀类名
 * @param {string} [props.hashId] - 哈希ID
 *
 * @example
 * ```tsx
 * <FileMapViewItem
 *   file={fileData}
 *   onPreview={() => handlePreview(fileData)}
 *   onDownload={() => handleDownload(fileData)}
 *   className="custom-file-item"
 * />
 * ```
 *
 * @returns {React.ReactElement} 渲染的文件映射视图项组件
 *
 * @remarks
 * - 显示文件图标、名称和大小
 * - 支持文件预览和下载操作
 * - 提供动画效果
 * - 显示工具提示
 * - 自动处理文件状态
 * - 支持自定义样式
 */ export var FileMapViewItem = function FileMapViewItem(props) {
    var _ref;
    var file = props.file;
    var locale = useContext(I18nContext).locale;
    var _React_useState = _sliced_to_array(React.useState(false), 2), hovered = _React_useState[0], setHovered = _React_useState[1];
    var handleMouseEnter = function handleMouseEnter() {
        return setHovered(true);
    };
    var handleMouseLeave = function handleMouseLeave() {
        return setHovered(false);
    };
    var fileName = (_ref = file === null || file === void 0 ? void 0 : file.name) !== null && _ref !== void 0 ? _ref : '';
    var lastDotIndex = fileName.lastIndexOf('.');
    var displayName = lastDotIndex > 0 ? fileName.slice(0, lastDotIndex) : fileName;
    var displayExtension = lastDotIndex > 0 && lastDotIndex < fileName.length - 1 ? fileName.slice(lastDotIndex + 1) : '';
    var showSize = typeof file.size === 'number' && file.size > 0;
    var fileSize = showSize ? kbToSize(file.size / 1024) : '';
    var lastModifiedTime = (file === null || file === void 0 ? void 0 : file.lastModified) ? dayjs(file.lastModified).format('HH:mm') : '';
    var renderExtensionContainer = function renderExtensionContainer() {
        if (file.status === 'error' && file.errorMessage) {
            return /*#__PURE__*/ React.createElement("div", {
                "data-testid": "file-item-extension-container",
                className: classNames("".concat(props.prefixCls, "-file-name-extension-container"), props.hashId)
            }, /*#__PURE__*/ React.createElement("span", {
                className: classNames("".concat(props.prefixCls, "-file-error-msg"), props.hashId),
                "data-testid": "file-item-error-msg",
                style: {
                    color: 'var(--color-red-text-secondary)'
                }
            }, file.errorMessage));
        }
        var items = [];
        if (displayExtension) {
            items.push(/*#__PURE__*/ React.createElement("span", {
                key: "ext",
                "data-testid": "file-item-extension",
                className: classNames("".concat(props.prefixCls, "-file-name-extension"), props.hashId)
            }, displayExtension));
        }
        if (fileSize) {
            items.push(/*#__PURE__*/ React.createElement("div", {
                key: "size",
                "data-testid": "file-item-size",
                className: classNames("".concat(props.prefixCls, "-file-size"), props.hashId)
            }, fileSize));
        }
        if (lastModifiedTime) {
            items.push(/*#__PURE__*/ React.createElement("div", {
                key: "time",
                "data-testid": "file-item-time"
            }, lastModifiedTime));
        }
        if (items.length === 0) return null;
        return /*#__PURE__*/ React.createElement("div", {
            "data-testid": "file-item-extension-container",
            className: classNames("".concat(props.prefixCls, "-file-name-extension-container"), props.hashId)
        }, items.map(function(item, index) {
            return /*#__PURE__*/ React.createElement(React.Fragment, {
                key: index
            }, index > 0 && /*#__PURE__*/ React.createElement("span", {
                className: classNames("".concat(props.prefixCls, "-separator"), props.hashId)
            }, "|"), item);
        }));
    };
    // 有 status 但无 url/previewUrl：文件内容未拿到，展示大小与格式占位块（loading 状态除外）
    if (isFileMetaPlaceholderState(file)) {
        return /*#__PURE__*/ React.createElement(FileMetaPlaceholder, {
            file: file,
            className: classNames(props.className, props.prefixCls ? "".concat(props.prefixCls, "-meta-placeholder") : undefined),
            style: _object_spread({
                height: 56
            }, props.style)
        });
    }
    return /*#__PURE__*/ React.createElement(Tooltip, {
        title: file.status === 'error' && file.errorMessage ? /*#__PURE__*/ React.createElement("div", null, file.errorMessage) : /*#__PURE__*/ React.createElement("div", null, locale === null || locale === void 0 ? void 0 : locale.clickToPreview),
        placement: "topLeft",
        arrow: false
    }, /*#__PURE__*/ React.createElement(motion.div, {
        onMouseEnter: handleMouseEnter,
        onMouseLeave: handleMouseLeave,
        onClick: function onClick() {
            if (file.status === 'error') return;
            if (props.onPreview) {
                var _props_onPreview;
                (_props_onPreview = props.onPreview) === null || _props_onPreview === void 0 ? void 0 : _props_onPreview.call(props);
            } else {
                // 默认行为：在新窗口打开文件
                if (typeof window === 'undefined') return;
                window.open(file.previewUrl || file.url, '_blank');
            }
        },
        variants: {
            hidden: {
                x: 20,
                opacity: 0
            },
            visible: {
                x: 0,
                opacity: 1
            },
            exit: {
                x: -20,
                opacity: 0
            }
        },
        exit: {
            opacity: 0,
            x: -20
        },
        className: classNames(props.className, _define_property({}, "".concat(props.prefixCls, "-meta-placeholder"), isFileMetaPlaceholderState(file))),
        "data-testid": "file-item"
    }, /*#__PURE__*/ React.createElement("div", {
        "data-testid": "file-item-icon",
        className: classNames("".concat(props.prefixCls, "-file-icon"), props.hashId)
    }, /*#__PURE__*/ React.createElement(AttachmentFileIcon, {
        file: file,
        className: classNames("".concat(props.prefixCls, "-file-icon-img"), props.hashId)
    })), /*#__PURE__*/ React.createElement("div", {
        "data-testid": "file-item-info",
        className: classNames("".concat(props.prefixCls, "-file-info"), props.hashId)
    }, /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(props.prefixCls, "-file-name"), props.hashId)
    }, /*#__PURE__*/ React.createElement("span", {
        "data-testid": "file-item-name",
        className: classNames("".concat(props.prefixCls, "-file-name-text"), props.hashId),
        title: file === null || file === void 0 ? void 0 : file.name
    }, displayName)), renderExtensionContainer()), hovered ? /*#__PURE__*/ React.createElement("div", {
        "data-testid": "file-item-action-bar",
        className: classNames("".concat(props.prefixCls, "-action-bar"), props.hashId)
    }, props.customSlot ? /*#__PURE__*/ React.createElement(ActionIconBox, {
        title: '更多',
        onClick: function onClick(e) {
            e.stopPropagation();
        },
        className: classNames("".concat(props.prefixCls, "-action-btn"), props.hashId)
    }, typeof props.customSlot === 'function' ? props.customSlot(file) : props.customSlot) : /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(ActionIconBox, {
        title: (locale === null || locale === void 0 ? void 0 : locale.preview) || '预览',
        onClick: function onClick(e) {
            e.stopPropagation();
            if (props.onPreview) {
                var _props_onPreview;
                (_props_onPreview = props.onPreview) === null || _props_onPreview === void 0 ? void 0 : _props_onPreview.call(props);
            } else {
                // 默认行为：在新窗口打开文件
                if (typeof window === 'undefined') return;
                window.open(file.previewUrl || file.url, '_blank');
            }
        },
        className: classNames("".concat(props.prefixCls, "-action-btn"), props.hashId)
    }, /*#__PURE__*/ React.createElement(Eye, {
        color: "var(--color-gray-text-secondary)"
    })), props.onDownload && /*#__PURE__*/ React.createElement(ActionIconBox, {
        title: (locale === null || locale === void 0 ? void 0 : locale.download) || '下载',
        onClick: function onClick(e) {
            var _props_onDownload;
            e.stopPropagation();
            (_props_onDownload = props.onDownload) === null || _props_onDownload === void 0 ? void 0 : _props_onDownload.call(props);
        },
        className: classNames("".concat(props.prefixCls, "-action-btn"), props.hashId)
    }, /*#__PURE__*/ React.createElement(Download, {
        color: "var(--color-gray-text-secondary)"
    })), props.renderMoreAction && /*#__PURE__*/ React.createElement(ActionIconBox, {
        title: (locale === null || locale === void 0 ? void 0 : locale.moreActions) || '更多操作',
        onClick: function onClick(e) {
            e.stopPropagation();
        },
        className: classNames("".concat(props.prefixCls, "-action-btn"), props.hashId)
    }, /*#__PURE__*/ React.createElement(EllipsisVertical, {
        style: {
            color: 'var(--color-gray-text-secondary)'
        }
    }), /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(props.prefixCls, "-more-custom"), props.hashId)
    }, props.renderMoreAction(file))))) : null));
};
