import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_classnames__ from "classnames";
import * as __WEBPACK_EXTERNAL_MODULE_file_saver__ from "file-saver";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__application_index_mjs__ from "../../../application/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__ from "../../../application/hoc/withDynamicSchemaProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_useProps_mjs__ from "../../hooks/useProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__ReadPretty_mjs__ from "./ReadPretty.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__shared_mjs__ from "./shared.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ from "./style.mjs";
const Upload = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Upload, {
        ...(0, __WEBPACK_EXTERNAL_MODULE__shared_mjs__.useUploadProps)(props)
    }), (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapProps)({
    value: 'fileList'
}), (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapReadPretty)(__WEBPACK_EXTERNAL_MODULE__ReadPretty_mjs__.ReadPretty.Upload));
Upload.Attachment = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    var _images_fileIndex, _images_, _images_1, _images_fileIndex1;
    const { disabled, multiple, value, onChange } = props;
    const [fileList, setFileList] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const [sync, setSync] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(true);
    const app = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useApp)();
    const previewList = app.AttachmentPreviewManager.get();
    const images = fileList;
    const [fileIndex, setFileIndex] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(0);
    const [visible, setVisible] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const uploadProps = (0, __WEBPACK_EXTERNAL_MODULE__shared_mjs__.useUploadProps)({
        ...props
    });
    const { wrapSSR, hashId, componentCls: prefixCls } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    const internalFileList = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)([]);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (sync) {
            const fileList = (0, __WEBPACK_EXTERNAL_MODULE__shared_mjs__.toFileList)(value);
            setFileList(fileList);
            internalFileList.current = fileList;
        }
    }, [
        value,
        sync
    ]);
    return wrapSSR(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])("".concat(prefixCls, "-wrapper"), "".concat(prefixCls, "-picture-card-wrapper"), 'tb-upload', hashId),
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                    className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])("".concat(prefixCls, "-list"), "".concat(prefixCls, "-list-picture-card")),
                    children: [
                        fileList.map((file, index)=>{
                            const fileProps = {
                                file,
                                prefixCls,
                                fileList,
                                setFileIndex,
                                setVisible,
                                disabled,
                                t,
                                setSync,
                                setFileList,
                                multiple,
                                onChange,
                                internalFileList,
                                preview: previewList[null == file ? void 0 : file.mimetype] || previewList['default']
                            };
                            return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createElement)(UploadReadFile, {
                                ...fileProps,
                                key: index
                            });
                        }),
                        !disabled && (multiple || (0, __WEBPACK_EXTERNAL_MODULE__shared_mjs__.toArr)(value).length < 1) && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                            className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])("".concat(prefixCls, "-list-picture-card-container"), "".concat(prefixCls, "-list-item-container")),
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Upload, {
                                ...uploadProps,
                                disabled: disabled,
                                multiple: multiple,
                                listType: 'picture-card',
                                fileList: fileList,
                                onChange: (info)=>{
                                    const list = updateFileList(info.file, internalFileList.current);
                                    internalFileList.current = list;
                                    setSync(false);
                                    if (multiple) {
                                        if ('done' === info.file.status) null == onChange || onChange((0, __WEBPACK_EXTERNAL_MODULE__shared_mjs__.toValue)(list));
                                        null == onChange || onChange((0, __WEBPACK_EXTERNAL_MODULE__shared_mjs__.toValue)(list));
                                        setFileList(list.map(__WEBPACK_EXTERNAL_MODULE__shared_mjs__.toItem));
                                        setSync(true);
                                    } else {
                                        if ('done' === info.file.status) {
                                            var _info_file_response, _info_file;
                                            null == onChange || onChange(null);
                                            null == onChange || onChange(null === (_info_file = info.file) || void 0 === _info_file ? void 0 : null === (_info_file_response = _info_file.response) || void 0 === _info_file_response ? void 0 : _info_file_response.data);
                                        }
                                        setFileList([
                                            (0, __WEBPACK_EXTERNAL_MODULE__shared_mjs__.toItem)(info.file)
                                        ]);
                                        setSync(true);
                                    }
                                },
                                showUploadList: false,
                                children: !disabled && (multiple || (0, __WEBPACK_EXTERNAL_MODULE__shared_mjs__.toArr)(value).length < 1) && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("span", {
                                    children: [
                                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.PlusOutlined, {}),
                                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("br", {}),
                                        " ",
                                        t('Upload')
                                    ]
                                })
                            })
                        })
                    ]
                })
            }),
            visible && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.Lightbox, {
                mainSrc: null === (_images_fileIndex = images[fileIndex]) || void 0 === _images_fileIndex ? void 0 : _images_fileIndex.imageUrl,
                nextSrc: null === (_images_ = images[(fileIndex + 1) % images.length]) || void 0 === _images_ ? void 0 : _images_.imageUrl,
                prevSrc: null === (_images_1 = images[(fileIndex + images.length - 1) % images.length]) || void 0 === _images_1 ? void 0 : _images_1.imageUrl,
                mainFile: images[fileIndex],
                nextFile: images[(fileIndex + 1) % images.length],
                prevFile: images[(fileIndex + images.length - 1) % images.length],
                previewList: previewList,
                onCloseRequest: ()=>setVisible(false),
                onMovePrevRequest: ()=>setFileIndex((fileIndex + images.length - 1) % images.length),
                onMoveNextRequest: ()=>setFileIndex((fileIndex + 1) % images.length),
                imageTitle: null === (_images_fileIndex1 = images[fileIndex]) || void 0 === _images_fileIndex1 ? void 0 : _images_fileIndex1.title,
                toolbarButtons: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("button", {
                        style: {
                            fontSize: 22,
                            background: 'none',
                            lineHeight: 1
                        },
                        type: "button",
                        "aria-label": "Zoom in",
                        title: "Zoom in",
                        className: "ril-zoom-in ril__toolbarItemChild ril__builtinButton",
                        onClick: (e)=>{
                            e.preventDefault();
                            const file = images[fileIndex];
                            (0, __WEBPACK_EXTERNAL_MODULE_file_saver__.saveAs)(file.url, "".concat(file.title).concat(file.extname));
                        },
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.DownloadOutlined, {})
                    }, 'preview-img')
                ]
            })
        ]
    }));
}, (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapReadPretty)(__WEBPACK_EXTERNAL_MODULE__ReadPretty_mjs__.ReadPretty.File));
Upload.Dragger = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const { tipContent } = props;
    const { wrapSSR, hashId, componentCls: prefixCls } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    return wrapSSR(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])("".concat(prefixCls, "-dragger"), hashId),
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Upload.Dragger, {
            ...(0, __WEBPACK_EXTERNAL_MODULE__shared_mjs__.useUploadProps)(props),
            children: [
                tipContent,
                props.children
            ]
        })
    }));
}, (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapProps)({
    value: 'fileList'
}));
Upload.DraggerV2 = (0, __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__.withDynamicSchemaProps)((0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const defaultTitle = t('Click or drag file to this area to upload');
    const defaultSubTitle = t('Support for a single or bulk upload, file size should not exceed') + " 10MB";
    const { title = defaultTitle, subTitle = defaultSubTitle, ...extraProps } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_useProps_mjs__.useProps)(props);
    const [loading, setLoading] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const { wrapSSR, hashId, componentCls: prefixCls } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    const handleChange = function() {
        let fileList = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
        const { onChange } = extraProps;
        null == onChange || onChange(fileList);
        fileList.some((file)=>'uploading' === file.status) ? setLoading(true) : setLoading(false);
    };
    return wrapSSR(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])("".concat(prefixCls, "-dragger"), hashId),
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Upload.Dragger, {
            ...(0, __WEBPACK_EXTERNAL_MODULE__shared_mjs__.useUploadProps)({
                ...props,
                ...extraProps,
                onChange: handleChange
            }),
            children: [
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("p", {
                    className: "".concat(prefixCls, "-drag-icon"),
                    children: loading ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.LoadingOutlined, {
                        style: {
                            fontSize: 36
                        },
                        spin: true
                    }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.InboxOutlined, {})
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("p", {
                    className: "".concat(prefixCls, "-text"),
                    children: title
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("p", {
                    className: "".concat(prefixCls, "-hint"),
                    children: subTitle
                })
            ]
        })
    }));
}, (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapProps)({
    value: 'fileList'
})), {
    displayName: 'Upload.DraggerV2'
});
const Upload_rslib_entry_ = Upload;
function updateFileList(file, fileList) {
    const nextFileList = [
        ...fileList
    ];
    const fileIndex = nextFileList.findIndex((param)=>{
        let { uid } = param;
        return uid === file.uid;
    });
    if (-1 === fileIndex) nextFileList.push(file);
    else nextFileList[fileIndex] = file;
    return nextFileList;
}
const UploadReadFile = (props)=>{
    const { file, prefixCls, fileList, setFileIndex, setVisible, disabled, t, setSync, setFileList, multiple, onChange, internalFileList, preview } = props;
    const handleClick = (e)=>{
        e.preventDefault();
        e.stopPropagation();
        const index = fileList.indexOf(file);
        setVisible(true);
        setFileIndex(index);
    };
    const { viewComponet } = preview;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        className: "".concat(prefixCls, "-list-picture-card-container ").concat(prefixCls, "-list-item-container"),
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
            className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])("".concat(prefixCls, "-list-item"), "".concat(prefixCls, "-list-item-done"), "".concat(prefixCls, "-list-item-list-type-picture-card")),
            children: [
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    className: "".concat(prefixCls, "-list-item-info"),
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("span", {
                        className: "".concat(prefixCls, "-span"),
                        children: [
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("a", {
                                className: "".concat(prefixCls, "-list-item-thumbnail"),
                                href: file.url,
                                target: "_blank",
                                rel: "noopener noreferrer",
                                onClick: handleClick,
                                style: {
                                    lineHeight: '100%'
                                },
                                children: viewComponet({
                                    prefixCls,
                                    file,
                                    setFileIndex,
                                    setVisible
                                })
                            }),
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("a", {
                                target: "_blank",
                                rel: "noopener noreferrer",
                                className: "".concat(prefixCls, "-list-item-name"),
                                title: file.title,
                                href: file.url,
                                onClick: handleClick,
                                children: 'uploading' === file.status ? t('Uploading') : file.title
                            })
                        ]
                    })
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                    className: "".concat(prefixCls, "-list-item-actions"),
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Space, {
                        size: 3,
                        children: [
                            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                                size: 'small',
                                type: 'text',
                                icon: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.DownloadOutlined, {}),
                                onClick: ()=>{
                                    (0, __WEBPACK_EXTERNAL_MODULE_file_saver__.saveAs)(file.url, "".concat(file.title).concat(file.extname));
                                }
                            }),
                            !disabled && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                                size: 'small',
                                type: 'text',
                                icon: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.DeleteOutlined, {}),
                                onClick: ()=>{
                                    setSync(false);
                                    setFileList((prevFileList)=>{
                                        if (!multiple) {
                                            null == onChange || onChange(null);
                                            setSync(true);
                                            return [];
                                        }
                                        const index = prevFileList.indexOf(file);
                                        prevFileList.splice(index, 1);
                                        internalFileList.current = internalFileList.current.filter((item)=>item.uid !== file.uid);
                                        null == onChange || onChange((0, __WEBPACK_EXTERNAL_MODULE__shared_mjs__.toValue)([
                                            ...prevFileList
                                        ]));
                                        return [
                                            ...prevFileList
                                        ];
                                    });
                                }
                            })
                        ]
                    })
                }),
                'uploading' === file.status && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    className: "".concat(prefixCls, "-list-item-progress"),
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Progress, {
                        strokeWidth: 2,
                        type: 'line',
                        showInfo: false,
                        percent: file.percent
                    })
                })
            ]
        })
    }, file.uid || file.id);
};
export { Upload, UploadReadFile, Upload_rslib_entry_ as default };
