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;
}
import { FileFailed, FileUploadingSpin, X } from "@sofa-design/icons";
import { Tooltip } from "antd";
import classNames from "clsx";
import React, { useContext } from "react";
import { I18nContext } from "../../../I18n";
import { isAttachmentFileLoading, isFileMetaPlaceholderState, kbToSize } from "../utils";
import { AttachmentFileIcon, FileMetaPlaceholder } from "./AttachmentFileIcon";
var getFileNameWithoutExtension = function getFileNameWithoutExtension(fileName) {
    return fileName.split('.').slice(0, -1).join('.');
};
var getFileExtension = function getFileExtension(fileName) {
    return fileName.split('.').slice(-1)[0];
};
var FileIcon = function FileIcon(param) {
    var file = param.file, prefixCls = param.prefixCls, hashId = param.hashId;
    var status = (file.status || 'done') === 'pending' ? 'uploading' : file.status || 'done';
    var iconMap = {
        uploading: /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-uploading-icon"), hashId)
        }, /*#__PURE__*/ React.createElement(FileUploadingSpin, null)),
        error: /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-error-icon"), hashId)
        }, /*#__PURE__*/ React.createElement(FileFailed, null)),
        done: /*#__PURE__*/ React.createElement(AttachmentFileIcon, {
            file: file,
            className: classNames("".concat(prefixCls, "-file-icon-img"), hashId)
        })
    };
    return /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefixCls, "-file-icon"), hashId)
    }, iconMap[status]);
};
var DoneFileSizeItems = function DoneFileSizeItems(param) {
    var file = param.file, prefixCls = param.prefixCls, hashId = param.hashId;
    var fileExtension = getFileExtension(file.name);
    var fileSize = file.size ? kbToSize(file.size / 1024) : '';
    var sizeItems = [
        fileExtension,
        fileSize
    ].filter(Boolean);
    return /*#__PURE__*/ React.createElement(React.Fragment, null, sizeItems.map(function(item) {
        return /*#__PURE__*/ React.createElement("span", {
            key: item,
            className: classNames("".concat(prefixCls, "-file-size-item"), hashId)
        }, item);
    }));
};
var FileSizeInfo = function FileSizeInfo(param) {
    var file = param.file, prefixCls = param.prefixCls, hashId = param.hashId, locale = param.locale;
    var status = file.status || 'done';
    var baseClassName = classNames("".concat(prefixCls, "-file-size"), hashId);
    if (status === 'uploading' || status === 'pending') {
        return /*#__PURE__*/ React.createElement("div", {
            className: baseClassName
        }, (locale === null || locale === void 0 ? void 0 : locale.uploading) || 'Uploading...');
    }
    if (status === 'error') {
        return /*#__PURE__*/ React.createElement("div", {
            className: classNames(baseClassName, "".concat(prefixCls, "-file-size-error"))
        }, file.errorMessage || (locale === null || locale === void 0 ? void 0 : locale.uploadFailed) || 'Upload failed');
    }
    return /*#__PURE__*/ React.createElement("div", {
        className: baseClassName
    }, /*#__PURE__*/ React.createElement(DoneFileSizeItems, {
        file: file,
        prefixCls: prefixCls,
        hashId: hashId
    }));
};
var DeleteButton = function DeleteButton(param) {
    var onClick = param.onClick, prefixCls = param.prefixCls, hashId = param.hashId;
    return /*#__PURE__*/ React.createElement("div", {
        onClick: onClick,
        className: classNames("".concat(prefixCls, "-close-icon"), hashId)
    }, /*#__PURE__*/ React.createElement(X, {
        role: "img",
        "aria-label": "X"
    }));
};
export var AttachmentFileListItem = function AttachmentFileListItem(param) {
    var file = param.file, prefixCls = param.prefixCls, hashId = param.hashId, onPreview = param.onPreview, onRetry = param.onRetry, onDelete = param.onDelete, className = param.className, _param_motionState = param.motionState, motionState = _param_motionState === void 0 ? 'enter' : _param_motionState, _param_motionDelaySec = param.motionDelaySec, motionDelaySec = _param_motionDelaySec === void 0 ? 0 : _param_motionDelaySec;
    var locale = useContext(I18nContext).locale;
    var isErrorStatus = file.status === 'error';
    var isNonRetryableError = isErrorStatus && (file.errorCode === 'FILE_SIZE_EXCEEDED' || file.errorCode === 'FILE_COUNT_EXCEEDED');
    var canRetry = isErrorStatus && !isNonRetryableError;
    var isDoneStatus = file.status === 'done';
    var canDelete = !isAttachmentFileLoading(file.status);
    var handleFileClick = function handleFileClick() {
        if (canRetry) {
            onRetry === null || onRetry === void 0 ? void 0 : onRetry(file);
            return;
        }
        if (!isDoneStatus) return;
        onPreview === null || onPreview === void 0 ? void 0 : onPreview(file);
    };
    var handleDeleteClick = function handleDeleteClick(e) {
        e.stopPropagation();
        onDelete(file);
    };
    // 有 status 但无 url/previewUrl：文件内容未拿到，展示大小与格式占位块
    // 注意：error 状态不走占位符，直接在下面的 file-item 中展示失败 UI
    if (!isErrorStatus && isFileMetaPlaceholderState(file)) {
        return /*#__PURE__*/ React.createElement(FileMetaPlaceholder, {
            file: file,
            className: className
        });
    }
    return /*#__PURE__*/ React.createElement(Tooltip, {
        title: canRetry ? (locale === null || locale === void 0 ? void 0 : locale.clickToRetry) || '点击重试' : undefined,
        open: canRetry ? undefined : false
    }, /*#__PURE__*/ React.createElement("div", {
        onClick: handleFileClick,
        className: classNames(className, "".concat(prefixCls, "-motion"), _define_property({}, "".concat(prefixCls, "-meta-placeholder"), isFileMetaPlaceholderState(file))),
        "data-testid": "file-item",
        "data-state": motionState,
        style: {
            '--attachment-item-delay': "".concat(motionDelaySec, "s")
        }
    }, /*#__PURE__*/ React.createElement(FileIcon, {
        file: file,
        prefixCls: prefixCls,
        hashId: hashId
    }), /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefixCls, "-file-info"), hashId)
    }, /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefixCls, "-file-name"), hashId)
    }, /*#__PURE__*/ React.createElement("span", {
        className: classNames("".concat(prefixCls, "-file-name-text"), hashId)
    }, getFileNameWithoutExtension(file.name))), /*#__PURE__*/ React.createElement(FileSizeInfo, {
        file: file,
        prefixCls: prefixCls,
        hashId: hashId,
        locale: locale
    })), canDelete && /*#__PURE__*/ React.createElement(DeleteButton, {
        onClick: handleDeleteClick,
        prefixCls: prefixCls,
        hashId: hashId
    })));
};
