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 ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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 { Eye, FileFailed, FileUploadingSpin, Play } from "@sofa-design/icons";
import { Image, Tooltip } from "antd";
import React, { useEffect, useState } from "react";
import { useAdaptiveTooltipProps } from "../../../Hooks/useAdaptiveTooltipProps";
import { getFileTypeIcon } from "../../../Workspace/File/utils";
import { isImageFile, isVideoFile, kbToSize } from "../utils";
var VideoThumbnail = function VideoThumbnail(param) {
    var src = param.src, className = param.className, style = param.style;
    return /*#__PURE__*/ React.createElement("div", {
        className: className,
        style: _object_spread_props(_object_spread({}, style), {
            position: 'relative',
            overflow: 'hidden',
            borderRadius: 'var(--radius-base, 4px)',
            flexShrink: 0
        })
    }, /*#__PURE__*/ React.createElement("video", {
        src: src,
        preload: "metadata",
        muted: true,
        playsInline: true,
        style: {
            width: '100%',
            height: '100%',
            objectFit: 'cover',
            display: 'block'
        }
    }), /*#__PURE__*/ React.createElement("div", {
        style: {
            position: 'absolute',
            inset: 0,
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            background: 'rgba(0, 0, 0, 0.35)',
            pointerEvents: 'none'
        }
    }, /*#__PURE__*/ React.createElement(Play, {
        style: {
            width: 24,
            height: 24,
            color: '#fff'
        }
    })));
};
/**
 * 从 File 创建并使用 object URL 的视频缩略图，避免每次渲染都创建新 URL
 */ var VideoThumbnailFromBlob = function VideoThumbnailFromBlob(param) {
    var file = param.file, className = param.className, style = param.style;
    var _useState = _sliced_to_array(useState(function() {
        return null;
    }), 2), objectUrl = _useState[0], setObjectUrl = _useState[1];
    useEffect(function() {
        var url = URL.createObjectURL(file);
        setObjectUrl(url);
        return function() {
            URL.revokeObjectURL(url);
        };
    }, [
        file
    ]);
    if (!objectUrl) return null;
    return /*#__PURE__*/ React.createElement(VideoThumbnail, {
        src: objectUrl,
        className: className,
        style: style
    });
};
/**
 * AttachmentFileIcon 组件 - 附件文件图标组件
 *
 * 该组件根据文件类型显示不同的图标或预览。如果是图片文件，则显示图片预览；
 * 如果是其他类型文件，则显示对应的文件图标。
 *
 * @component
 * @description 附件文件图标组件，根据文件类型显示图标或预览
 * @param {Object} props - 组件属性
 * @param {AttachmentFile} props.file - 附件文件对象
 * @param {string} [props.className] - 自定义CSS类名
 * @param {React.CSSProperties} [props.style] - 自定义样式
 *
 * @example
 * ```tsx
 * <AttachmentFileIcon
 *   file={fileData}
 *   className="custom-icon"
 *   style={{ fontSize: '24px' }}
 * />
 * ```
 *
 * @returns {React.ReactElement} 渲染的文件图标或预览组件
 *
 * @remarks
 * - 支持图片文件预览显示
 * - 支持多种文件类型图标
 * - 自动识别文件类型
 * - 提供自定义样式支持
 * - 响应式布局
 * - 图片自适应显示
 * - 图标居中显示
 */ var IMAGE_STYLE = {
    width: '40px',
    height: '40px',
    overflow: 'hidden'
};
/** 无 url/previewUrl 时展示文件大小与格式的占位块（内容未拿到） */ export var FileMetaPlaceholder = function FileMetaPlaceholder(param) {
    var file = param.file, className = param.className, style = param.style;
    var _ref, _file_size, _ref1, _ref2, _rawFormat_split_pop;
    var _file_uploadResponse, _file_uploadResponse1;
    var sizeBytes = (_ref = (_file_size = file.size) !== null && _file_size !== void 0 ? _file_size : (_file_uploadResponse = file.uploadResponse) === null || _file_uploadResponse === void 0 ? void 0 : _file_uploadResponse.fileSize) !== null && _ref !== void 0 ? _ref : 0;
    var sizeText = sizeBytes > 0 ? kbToSize(sizeBytes / 1024) : '';
    var extFromName = file.name ? file.name.split('.').pop() || '' : '';
    var rawFormat = (_ref1 = (_ref2 = (_file_uploadResponse1 = file.uploadResponse) === null || _file_uploadResponse1 === void 0 ? void 0 : _file_uploadResponse1.fileType) !== null && _ref2 !== void 0 ? _ref2 : file.type) !== null && _ref1 !== void 0 ? _ref1 : extFromName;
    var formatSuffix = rawFormat.includes('/') ? ((_rawFormat_split_pop = rawFormat.split('/').pop()) !== null && _rawFormat_split_pop !== void 0 ? _rawFormat_split_pop : '').toUpperCase() : rawFormat.toUpperCase();
    var formatText = formatSuffix || '-';
    var adaptiveTooltip = useAdaptiveTooltipProps('informational');
    return /*#__PURE__*/ React.createElement(Tooltip, _object_spread({
        title: file.name
    }, adaptiveTooltip), /*#__PURE__*/ React.createElement("div", {
        "data-testid": "file-meta-placeholder",
        className: className,
        style: _object_spread({
            height: 48,
            boxSizing: 'border-box',
            display: 'flex',
            justifyContent: 'center',
            borderRadius: 'var(--radius-base, 4px)',
            background: 'var(--color-fill-quaternary, rgba(0,0,0,0.04))',
            fontSize: 10,
            color: 'var(--color-text-tertiary, rgba(0,0,0,0.45))',
            lineHeight: 1.2,
            overflow: 'hidden',
            // 占位块不应在 flex 容器中自动拉伸为整行宽度
            flex: '0 0 auto',
            minWidth: 80,
            padding: 'var(--padding-1x) var(--padding-2x)',
            flexDirection: 'column'
        }, style)
    }, formatText && /*#__PURE__*/ React.createElement("span", {
        style: {
            lineClamp: 1,
            lineHeight: '20px',
            height: '20px',
            boxSizing: 'border-box',
            overflow: 'hidden',
            textOverflow: 'ellipsis',
            minWidth: 0,
            maxWidth: '100%'
        }
    }, formatText), sizeText && /*#__PURE__*/ React.createElement("span", {
        style: {
            lineClamp: 1,
            overflow: 'hidden',
            textOverflow: 'ellipsis',
            minWidth: 0,
            boxSizing: 'border-box',
            lineHeight: '20px',
            height: '20px',
            maxWidth: '100%'
        }
    }, sizeText)));
};
var IMAGE_PREVIEW_CONFIG = {
    mask: /*#__PURE__*/ React.createElement("div", null, /*#__PURE__*/ React.createElement(Eye, null)),
    visible: false
};
export var AttachmentFileIcon = function AttachmentFileIcon(props) {
    var _file_type;
    var file = props.file, className = props.className;
    // 上传中状态（兼容 pending）
    if (file.status === 'uploading' || file.status === 'pending') {
        return /*#__PURE__*/ React.createElement(FileUploadingSpin, null);
    }
    // 错误状态
    if (file.status === 'error') {
        return /*#__PURE__*/ React.createElement(FileFailed, null);
    }
    // 图片文件预览
    if (isImageFile(file)) {
        return /*#__PURE__*/ React.createElement(Image, {
            src: file.url,
            style: IMAGE_STYLE,
            rootClassName: className,
            preview: IMAGE_PREVIEW_CONFIG,
            alt: file.name
        });
    }
    // 视频文件缩略图预览（与图片类似，带播放按钮）
    if (isVideoFile(file)) {
        var videoUrl = file.previewUrl || file.url;
        if (videoUrl) {
            return /*#__PURE__*/ React.createElement(VideoThumbnail, {
                src: videoUrl,
                className: className,
                style: IMAGE_STYLE
            });
        }
        if (file.size) {
            return /*#__PURE__*/ React.createElement(VideoThumbnailFromBlob, {
                key: "".concat(file.name, "-").concat(file.size, "-").concat(file.lastModified || 0),
                file: file,
                className: className,
                style: IMAGE_STYLE
            });
        }
    }
    // 其他类型文件图标
    var fileType = (_file_type = file.type) === null || _file_type === void 0 ? void 0 : _file_type.split('/').at(-1);
    return getFileTypeIcon(fileType, '', file.name);
};
