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 { Download as DownloadIcon, Eye as EyeIcon, Locate, SquareArrowOutUpRight as ShareIcon } from "@sofa-design/icons";
import { Typography } from "antd";
import classNames from "clsx";
import React from "react";
import { ActionIconBox } from "../../../Components/ActionIconBox";
import { formatFileSize, formatLastModified } from "../../utils";
import { fileTypeProcessor, isImageFile } from "../FileTypeProcessor";
import { ensureNodeWithId, handleDefaultShare, handleFileDownload } from "../handlers";
import { getFileTypeIcon } from "../utils";
import { AccessibleButton } from "./AccessibleButton";
/**
 * 单个文件项
 *
 * 行为约定：
 * - 用户传 `onClick` 时优先触发；否则 fallback 到 `onPreview`
 * - 操作按钮显示：
 *   - 预览：`canPreview` 用户优先；默认对图片要求有 url，其他类型按 fileTypeProcessor 判定
 *   - 下载：`canDownload` 用户优先；默认存在下载源（onDownload/url/content/file）即可
 *   - 分享/定位：默认隐藏，需用户显式 `canShare/canLocate=true`
 * - 禁用态：操作按钮全部隐藏，根容器不响应点击
 */ var FileItemComponent = function FileItemComponent(param) {
    var file = param.file, onClick = param.onClick, onDownload = param.onDownload, onPreview = param.onPreview, onShare = param.onShare, onLocate = param.onLocate, prefixCls = param.prefixCls, hashId = param.hashId, locale = param.locale, _param_bindDomId = param.bindDomId, bindDomId = _param_bindDomId === void 0 ? false : _param_bindDomId;
    var fileWithId = ensureNodeWithId(file);
    var fileTypeInfo = fileTypeProcessor.inferFileType(fileWithId);
    var isDisabled = fileWithId.disabled === true;
    var handleClick = function handleClick() {
        if (isDisabled) return;
        if (onClick) {
            onClick(fileWithId);
            return;
        }
        if (onPreview) {
            onPreview(fileWithId);
        }
    };
    var showDownloadButton = function() {
        if (fileWithId.canDownload !== undefined) return fileWithId.canDownload;
        return Boolean(onDownload || fileWithId.url || fileWithId.content || fileWithId.file);
    }();
    var handleDownload = function handleDownload(e) {
        e.stopPropagation();
        if (onDownload) {
            onDownload(fileWithId);
            return;
        }
        handleFileDownload(fileWithId);
    };
    var handlePreview = function handlePreview(e) {
        e.stopPropagation();
        onPreview === null || onPreview === void 0 ? void 0 : onPreview(fileWithId);
    };
    var handleShare = function handleShare(e) {
        e.stopPropagation();
        if (onShare) {
            onShare(fileWithId, {
                anchorEl: e.currentTarget,
                origin: 'list'
            });
            return;
        }
        handleDefaultShare(fileWithId);
    };
    var showPreviewButton = function() {
        if (fileWithId.canPreview !== undefined) return fileWithId.canPreview;
        return onPreview && (isImageFile(fileWithId) ? !!(fileWithId.url || fileWithId.previewUrl) : fileTypeProcessor.processFile(fileWithId).canPreview);
    }();
    var showShareButton = fileWithId.canShare === true;
    var showLocationButton = fileWithId.canLocate === true;
    var handleLocate = function handleLocate(e) {
        e.stopPropagation();
        onLocate === null || onLocate === void 0 ? void 0 : onLocate(fileWithId);
    };
    var actionBtnClass = classNames("".concat(prefixCls, "-item-action-btn"), hashId);
    var builtinActions = {
        preview: showPreviewButton ? /*#__PURE__*/ React.createElement(ActionIconBox, {
            key: "preview",
            title: (locale === null || locale === void 0 ? void 0 : locale['workspace.file.preview']) || '预览',
            onClick: handlePreview,
            tooltipProps: {
                mouseEnterDelay: 0.3
            },
            className: actionBtnClass
        }, /*#__PURE__*/ React.createElement(EyeIcon, null)) : null,
        locate: showLocationButton ? /*#__PURE__*/ React.createElement(ActionIconBox, {
            key: "locate",
            title: (locale === null || locale === void 0 ? void 0 : locale['workspace.file.location']) || '定位',
            onClick: handleLocate,
            tooltipProps: {
                mouseEnterDelay: 0.3
            },
            className: actionBtnClass
        }, /*#__PURE__*/ React.createElement(Locate, null)) : null,
        share: showShareButton ? /*#__PURE__*/ React.createElement(ActionIconBox, {
            key: "share",
            title: (locale === null || locale === void 0 ? void 0 : locale['workspace.file.share']) || '分享',
            onClick: handleShare,
            tooltipProps: {
                mouseEnterDelay: 0.3
            },
            className: actionBtnClass
        }, /*#__PURE__*/ React.createElement(ShareIcon, null)) : null,
        download: showDownloadButton ? /*#__PURE__*/ React.createElement(ActionIconBox, {
            key: "download",
            title: (locale === null || locale === void 0 ? void 0 : locale['workspace.file.download']) || '下载',
            onClick: handleDownload,
            tooltipProps: {
                mouseEnterDelay: 0.3
            },
            className: actionBtnClass
        }, /*#__PURE__*/ React.createElement(DownloadIcon, null)) : null
    };
    var renderContext = {
        file: fileWithId,
        prefixCls: prefixCls,
        hashId: hashId,
        disabled: isDisabled,
        actions: builtinActions
    };
    var ariaLabel = "".concat((locale === null || locale === void 0 ? void 0 : locale['workspace.file']) || '文件', "：").concat(fileWithId.name);
    return /*#__PURE__*/ React.createElement(AccessibleButton, {
        icon: /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-item-icon"), hashId)
        }, getFileTypeIcon(fileTypeInfo.fileType, fileWithId.icon, fileWithId.name)), /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-item-info"), hashId)
        }, /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-item-name"), hashId)
        }, fileWithId.renderName ? fileWithId.renderName(renderContext) : /*#__PURE__*/ React.createElement(Typography.Text, {
            ellipsis: {
                tooltip: fileWithId.name
            }
        }, fileWithId.name)), (fileWithId.renderDetails || fileTypeInfo.displayType || fileWithId.size || fileWithId.lastModified) && /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-item-details"), hashId)
        }, fileWithId.renderDetails ? fileWithId.renderDetails(renderContext) : /*#__PURE__*/ React.createElement(Typography.Text, {
            type: "secondary",
            ellipsis: true
        }, fileTypeInfo.displayType && /*#__PURE__*/ React.createElement("span", {
            className: classNames("".concat(prefixCls, "-item-type"), hashId)
        }, fileTypeInfo.displayType), fileWithId.size && /*#__PURE__*/ React.createElement(React.Fragment, null, fileTypeInfo.displayType && /*#__PURE__*/ React.createElement("span", {
            className: classNames("".concat(prefixCls, "-item-separator"), hashId)
        }, "|"), /*#__PURE__*/ React.createElement("span", {
            className: classNames("".concat(prefixCls, "-item-size"), hashId)
        }, formatFileSize(fileWithId.size))), fileWithId.lastModified && /*#__PURE__*/ React.createElement(React.Fragment, null, (fileTypeInfo.displayType || fileWithId.size) && /*#__PURE__*/ React.createElement("span", {
            className: classNames("".concat(prefixCls, "-item-separator"), hashId)
        }, "|"), /*#__PURE__*/ React.createElement("span", {
            className: classNames("".concat(prefixCls, "-item-time"), hashId)
        }, formatLastModified(fileWithId.lastModified)))))), /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-item-actions"), hashId),
            onClick: function onClick(e) {
                return e.stopPropagation();
            }
        }, fileWithId.renderActions ? fileWithId.renderActions(renderContext) : !isDisabled ? /*#__PURE__*/ React.createElement(React.Fragment, null, builtinActions.preview, builtinActions.locate, builtinActions.share, builtinActions.download) : null)),
        onClick: handleClick,
        className: classNames("".concat(prefixCls, "-item"), _define_property({}, "".concat(prefixCls, "-item-disabled"), isDisabled), hashId),
        ariaLabel: ariaLabel,
        id: bindDomId ? fileWithId.id : undefined
    });
};
FileItemComponent.displayName = 'FileItem';
export var FileItem = /*#__PURE__*/ React.memo(FileItemComponent);
