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_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
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 { FileFailed, FileUploadingSpin, X } from "@sofa-design/icons";
import { Tooltip } from "antd";
import classNames from "classnames";
import { motion } from "framer-motion";
import React from "react";
import { kbToSize } from "../utils";
import { AttachmentFileIcon } from "./AttachmentFileIcon";
var getFileNameWithoutExtension = function(fileName) {
    return fileName.split('.').slice(0, -1).join('.');
};
var getFileExtension = function(fileName) {
    return fileName.split('.').slice(-1)[0];
};
var buildClassName = function() {
    for(var _len = arguments.length, classes = new Array(_len), _key = 0; _key < _len; _key++){
        classes[_key] = arguments[_key];
    }
    return classNames.apply(void 0, _to_consumable_array(classes));
};
var FileIcon = function(param) {
    var file = param.file, prefixCls = param.prefixCls, hashId = param.hashId;
    var status = file.status || 'done';
    var iconMap = {
        uploading: /*#__PURE__*/ React.createElement("div", {
            className: buildClassName("".concat(prefixCls, "-uploading-icon"), hashId)
        }, /*#__PURE__*/ React.createElement(FileUploadingSpin, null)),
        error: /*#__PURE__*/ React.createElement("div", {
            className: buildClassName("".concat(prefixCls, "-error-icon"), hashId)
        }, /*#__PURE__*/ React.createElement(FileFailed, null)),
        done: /*#__PURE__*/ React.createElement(AttachmentFileIcon, {
            file: file,
            className: buildClassName("".concat(prefixCls, "-file-icon-img"), hashId)
        })
    };
    return /*#__PURE__*/ React.createElement("div", {
        className: buildClassName("".concat(prefixCls, "-file-icon"), hashId)
    }, iconMap[status]);
};
var FileSizeInfo = function(param) {
    var file = param.file, prefixCls = param.prefixCls, hashId = param.hashId;
    var status = file.status || 'done';
    var baseClassName = buildClassName("".concat(prefixCls, "-file-size"), hashId);
    var statusContentMap = {
        uploading: '上传中...',
        error: /*#__PURE__*/ React.createElement("div", {
            className: buildClassName(baseClassName, "".concat(prefixCls, "-file-size-error"))
        }, "上传失败"),
        done: function() {
            var fileExtension = getFileExtension(file.name);
            var fileSize = file.size ? kbToSize(file.size / 1024) : '';
            var sizeItems = [
                fileExtension,
                fileSize
            ].filter(Boolean);
            return sizeItems.map(function(item) {
                return /*#__PURE__*/ React.createElement("span", {
                    key: item,
                    className: buildClassName("".concat(prefixCls, "-file-size-item"), hashId)
                }, item);
            });
        }()
    };
    var content = statusContentMap[status];
    return typeof content === 'string' ? /*#__PURE__*/ React.createElement("div", {
        className: baseClassName
    }, content) : /*#__PURE__*/ React.createElement("div", {
        className: baseClassName
    }, content);
};
var DeleteButton = function(param) {
    var isVisible = param.isVisible, onClick = param.onClick, prefixCls = param.prefixCls, hashId = param.hashId;
    if (!isVisible) return null;
    return /*#__PURE__*/ React.createElement("div", {
        onClick: onClick,
        className: buildClassName("".concat(prefixCls, "-close-icon"), hashId)
    }, /*#__PURE__*/ React.createElement(X, {
        role: "img",
        "aria-label": "X"
    }));
};
var ANIMATION_VARIANTS = {
    hidden: {
        y: 20,
        opacity: 0
    },
    visible: {
        y: 0,
        opacity: 1
    },
    exit: {
        opacity: 0,
        y: -20
    }
};
export var AttachmentFileListItem = function(param) {
    var file = param.file, prefixCls = param.prefixCls, hashId = param.hashId, onPreview = param.onPreview, onRetry = param.onRetry, onDelete = param.onDelete, className = param.className;
    var isErrorStatus = file.status === 'error';
    var isDoneStatus = file.status === 'done';
    var canDelete = file.status !== 'uploading';
    var handleFileClick = function() {
        if (!isDoneStatus) return;
        onPreview === null || onPreview === void 0 ? void 0 : onPreview();
    };
    var handleRetryClick = function() {
        if (!isErrorStatus) return;
        onRetry === null || onRetry === void 0 ? void 0 : onRetry();
    };
    var handleDeleteClick = function(e) {
        e.stopPropagation();
        onDelete === null || onDelete === void 0 ? void 0 : onDelete();
    };
    return /*#__PURE__*/ React.createElement(Tooltip, {
        title: "点击重试",
        open: isErrorStatus ? undefined : false
    }, /*#__PURE__*/ React.createElement(motion.div, {
        variants: ANIMATION_VARIANTS,
        onClick: handleFileClick,
        className: className,
        exit: ANIMATION_VARIANTS.exit
    }, /*#__PURE__*/ React.createElement(FileIcon, {
        file: file,
        prefixCls: prefixCls,
        hashId: hashId
    }), /*#__PURE__*/ React.createElement("div", {
        className: buildClassName("".concat(prefixCls, "-file-info"), hashId)
    }, /*#__PURE__*/ React.createElement("div", {
        onClick: handleRetryClick,
        className: buildClassName("".concat(prefixCls, "-file-name"), hashId)
    }, /*#__PURE__*/ React.createElement("span", {
        className: buildClassName("".concat(prefixCls, "-file-name-text"), hashId)
    }, getFileNameWithoutExtension(file.name))), /*#__PURE__*/ React.createElement(FileSizeInfo, {
        file: file,
        prefixCls: prefixCls,
        hashId: hashId
    })), /*#__PURE__*/ React.createElement(DeleteButton, {
        isVisible: canDelete,
        onClick: handleDeleteClick,
        prefixCls: prefixCls,
        hashId: hashId
    })));
};
