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__upload_ReadPretty_mjs__ from "../upload/ReadPretty.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__upload_shared_mjs__ from "../upload/shared.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__upload_style_mjs__ from "../upload/style.mjs";
const Preview = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__upload_ReadPretty_mjs__.ReadPretty.File, {
        ...props
    }), (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.mapReadPretty)(__WEBPACK_EXTERNAL_MODULE__upload_ReadPretty_mjs__.ReadPretty.File));
const FileSelector = (props)=>{
    var _fileList_photoIndex, _fileList_, _fileList_1, _fileList_photoIndex1;
    const { disabled, multiple, value, quickUpload, selectFile, onRemove, onSelect } = props;
    const uploadProps = (0, __WEBPACK_EXTERNAL_MODULE__upload_shared_mjs__.useUploadProps)({
        ...props
    });
    const [fileList, setFileList] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)([]);
    const [photoIndex, setPhotoIndex] = (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 internalFileList = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)([]);
    const { wrapSSR, hashId, componentCls: prefixCls } = (0, __WEBPACK_EXTERNAL_MODULE__upload_style_mjs__.useStyles)();
    const showSelectButton = void 0 === selectFile && void 0 === quickUpload;
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        const fileList = (0, __WEBPACK_EXTERNAL_MODULE__upload_shared_mjs__.toFileList)(value);
        setFileList(fileList);
        internalFileList.current = fileList;
    }, [
        value
    ]);
    const handleRemove = (file)=>{
        null == onRemove || onRemove(file);
        return true;
    };
    const handleSelect = (e)=>{
        e.preventDefault();
        e.stopPropagation();
        null == onSelect || onSelect();
    };
    const list = fileList.length ? multiple ? fileList : [
        fileList[fileList.length - 1]
    ] : [];
    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: [
                        list.map((file)=>{
                            const handleClick = (e)=>{
                                e.preventDefault();
                                e.stopPropagation();
                                const index = fileList.indexOf(file);
                                if ((0, __WEBPACK_EXTERNAL_MODULE__upload_shared_mjs__.isImage)(file.extname)) {
                                    setVisible(true);
                                    setPhotoIndex(index);
                                } else (0, __WEBPACK_EXTERNAL_MODULE_file_saver__.saveAs)(file.url, "".concat(file.title).concat(file.extname));
                            };
                            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,
                                                        children: file.imageUrl && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("img", {
                                                            src: file.imageUrl,
                                                            alt: file.title,
                                                            className: "".concat(prefixCls, "-list-item-image")
                                                        })
                                                    }),
                                                    /*#__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: ()=>{
                                                            handleRemove(file);
                                                            internalFileList.current = internalFileList.current.filter((item)=>item.uid !== file.uid);
                                                        }
                                                    })
                                                ]
                                            })
                                        }),
                                        '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);
                        }),
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
                            children: [
                                showSelectButton ? /*#__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, {
                                        disabled: disabled,
                                        multiple: multiple,
                                        listType: 'picture-card',
                                        showUploadList: false,
                                        onRemove: handleRemove,
                                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                                            style: {
                                                width: '100%',
                                                height: '100%',
                                                display: 'flex',
                                                flexDirection: 'column',
                                                alignItems: 'center',
                                                justifyContent: 'center'
                                            },
                                            onClick: handleSelect,
                                            children: [
                                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.PlusOutlined, {}),
                                                t('Select')
                                            ]
                                        })
                                    })
                                }) : null,
                                quickUpload ? /*#__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,
                                        showUploadList: false,
                                        onRemove: handleRemove,
                                        onChange: (info)=>{
                                            var _uploadProps_onChange;
                                            const list = updateFileList(info.file, internalFileList.current);
                                            internalFileList.current = list;
                                            setFileList((0, __WEBPACK_EXTERNAL_MODULE__upload_shared_mjs__.toFileList)(list));
                                            null === (_uploadProps_onChange = uploadProps.onChange) || void 0 === _uploadProps_onChange || _uploadProps_onChange.call(uploadProps, {
                                                fileList: list
                                            });
                                        },
                                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                                            style: {
                                                width: '100%',
                                                height: '100%',
                                                display: 'flex',
                                                flexDirection: 'column',
                                                alignItems: 'center',
                                                justifyContent: 'center'
                                            },
                                            children: [
                                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.PlusOutlined, {}),
                                                t('Upload')
                                            ]
                                        })
                                    })
                                }) : null,
                                selectFile ? /*#__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, {
                                        disabled: disabled,
                                        multiple: multiple,
                                        listType: 'picture-card',
                                        showUploadList: false,
                                        onRemove: handleRemove,
                                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                                            style: {
                                                width: '100%',
                                                height: '100%',
                                                display: 'flex',
                                                flexDirection: 'column',
                                                alignItems: 'center',
                                                justifyContent: 'center'
                                            },
                                            onClick: handleSelect,
                                            children: [
                                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.PlusOutlined, {}),
                                                t('Select')
                                            ]
                                        })
                                    })
                                }) : null
                            ]
                        })
                    ]
                })
            }),
            visible && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.Lightbox, {
                mainSrc: null === (_fileList_photoIndex = fileList[photoIndex]) || void 0 === _fileList_photoIndex ? void 0 : _fileList_photoIndex.imageUrl,
                nextSrc: null === (_fileList_ = fileList[(photoIndex + 1) % fileList.length]) || void 0 === _fileList_ ? void 0 : _fileList_.imageUrl,
                prevSrc: null === (_fileList_1 = fileList[(photoIndex + fileList.length - 1) % fileList.length]) || void 0 === _fileList_1 ? void 0 : _fileList_1.imageUrl,
                onCloseRequest: ()=>setVisible(false),
                onMovePrevRequest: ()=>setPhotoIndex((photoIndex + fileList.length - 1) % fileList.length),
                onMoveNextRequest: ()=>setPhotoIndex((photoIndex + 1) % fileList.length),
                imageTitle: null === (_fileList_photoIndex1 = fileList[photoIndex]) || void 0 === _fileList_photoIndex1 ? void 0 : _fileList_photoIndex1.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 = fileList[photoIndex];
                            (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')
                ]
            })
        ]
    }));
};
const Preview_rslib_entry_ = Preview;
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;
}
export { FileSelector, Preview, Preview_rslib_entry_ as default };
