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 _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 { X } from "@sofa-design/icons";
import { ConfigProvider, Image } from "antd";
import classNames from "classnames";
import { AnimatePresence, motion } from "framer-motion";
import React, { useContext } from "react";
import { ActionIconBox } from "../../../Components/ActionIconBox";
import { isImageFile } from "../utils";
import { AttachmentFileListItem } from "./AttachmentFileListItem";
import { useStyle } from "./style";
var ANIMATION_VARIANTS = {
    visible: {
        opacity: 1,
        transition: {
            when: 'beforeChildren',
            staggerChildren: 0.1
        }
    },
    hidden: {
        opacity: 0,
        transition: {
            when: 'afterChildren'
        }
    }
};
var HIDDEN_STYLE = {
    height: 0,
    overflow: 'hidden',
    padding: 0
};
var IMAGE_PREVIEW_STYLE = {
    display: 'none'
};
var CLEAR_BUTTON_TRANSITION = 'all 0.3s cubic-bezier(0.4, 0, 0.2, 1)';
var getFileKey = function(file, index) {
    return (file === null || file === void 0 ? void 0 : file.uuid) || (file === null || file === void 0 ? void 0 : file.name) || index;
};
var openFileInNewWindow = function(url) {
    if (typeof window === 'undefined' || !url) return;
    window.open(url, '_blank');
};
var ClearButton = function(param) {
    var visible = param.visible, opacity = param.opacity, onClick = param.onClick, className = param.className;
    if (!visible) return null;
    return /*#__PURE__*/ React.createElement(ActionIconBox, {
        style: {
            transition: CLEAR_BUTTON_TRANSITION,
            opacity: opacity
        },
        onClick: onClick,
        className: className
    }, /*#__PURE__*/ React.createElement(X, null));
};
export var AttachmentFileList = function(param) {
    var fileMap = param.fileMap, onDelete = param.onDelete, onPreview = param.onPreview, onDownload = param.onDownload, onRetry = param.onRetry, onClearFileMap = param.onClearFileMap;
    var context = useContext(ConfigProvider.ConfigContext);
    var prefix = context === null || context === void 0 ? void 0 : context.getPrefixCls('agentic-md-editor-attachment-list');
    var _useStyle = useStyle(prefix), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    var _React_useState = _sliced_to_array(React.useState(undefined), 2), imgSrc = _React_useState[0], setImgSrc = _React_useState[1];
    var fileList = Array.from((fileMap === null || fileMap === void 0 ? void 0 : fileMap.values()) || []);
    var fileCount = (fileMap === null || fileMap === void 0 ? void 0 : fileMap.size) || 0;
    var hasFiles = fileList.length > 0;
    var isAnyUploading = fileList.some(function(file) {
        return file.status === 'uploading';
    });
    var canShowClearButton = !isAnyUploading;
    var containerStyle = fileCount ? {} : HIDDEN_STYLE;
    var clearButtonOpacity = fileCount ? 1 : 0;
    var handlePreview = function(file) {
        if (onPreview) {
            onPreview(file);
            return;
        }
        if (isImageFile(file)) {
            setImgSrc(file.previewUrl || file.url);
            return;
        }
        openFileInNewWindow(file.previewUrl || file.url);
    };
    var handlePreviewClose = function(visible) {
        if (!visible) setImgSrc(undefined);
    };
    var handleDelete = function(file) {
        return function() {
            return onDelete(file);
        };
    };
    var handlePreviewFile = function(file) {
        return function() {
            return handlePreview(file);
        };
    };
    var handleDownload = function(file) {
        return function() {
            return onDownload === null || onDownload === void 0 ? void 0 : onDownload(file);
        };
    };
    var handleRetry = function(file) {
        return function() {
            return onRetry === null || onRetry === void 0 ? void 0 : onRetry(file);
        };
    };
    return wrapSSR(/*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(prefix, "-container"), hashId, _define_property({}, "".concat(prefix, "-container-empty"), !hasFiles))
    }, /*#__PURE__*/ React.createElement(motion.div, {
        variants: ANIMATION_VARIANTS,
        whileInView: "visible",
        initial: "hidden",
        animate: "visible",
        style: containerStyle,
        className: classNames(prefix, hashId)
    }, /*#__PURE__*/ React.createElement(AnimatePresence, {
        initial: false
    }, fileList.map(function(file, index) {
        return /*#__PURE__*/ React.createElement(AttachmentFileListItem, {
            prefixCls: "".concat(prefix, "-item"),
            hashId: hashId,
            className: classNames(hashId, "".concat(prefix, "-item")),
            key: getFileKey(file, index),
            file: file,
            onDelete: handleDelete(file),
            onPreview: handlePreviewFile(file),
            onDownload: handleDownload(file),
            onRetry: handleRetry(file)
        });
    })), /*#__PURE__*/ React.createElement(Image, {
        key: "preview",
        src: imgSrc,
        alt: "Preview",
        style: IMAGE_PREVIEW_STYLE,
        preview: {
            visible: !!imgSrc,
            scaleStep: 1,
            src: imgSrc,
            onVisibleChange: handlePreviewClose
        }
    })), /*#__PURE__*/ React.createElement(ClearButton, {
        visible: canShowClearButton,
        opacity: clearButtonOpacity,
        onClick: onClearFileMap,
        className: classNames("".concat(prefix, "-close-icon"), hashId)
    })));
};
