"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    default: ()=>Preview_rslib_entry_,
    Preview: ()=>Preview,
    FileSelector: ()=>FileSelector
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const components_namespaceObject = require("@tachybase/components");
const schema_namespaceObject = require("@tachybase/schema");
const icons_namespaceObject = require("@ant-design/icons");
const external_antd_namespaceObject = require("antd");
const external_classnames_namespaceObject = require("classnames");
var external_classnames_default = /*#__PURE__*/ __webpack_require__.n(external_classnames_namespaceObject);
const external_file_saver_namespaceObject = require("file-saver");
const external_react_i18next_namespaceObject = require("react-i18next");
const ReadPretty_js_namespaceObject = require("../upload/ReadPretty.js");
const shared_js_namespaceObject = require("../upload/shared.js");
const style_js_namespaceObject = require("../upload/style.js");
const Preview = (0, schema_namespaceObject.connect)((props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ReadPretty_js_namespaceObject.ReadPretty.File, {
        ...props
    }), (0, schema_namespaceObject.mapReadPretty)(ReadPretty_js_namespaceObject.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, shared_js_namespaceObject.useUploadProps)({
        ...props
    });
    const [fileList, setFileList] = (0, external_react_namespaceObject.useState)([]);
    const [photoIndex, setPhotoIndex] = (0, external_react_namespaceObject.useState)(0);
    const [visible, setVisible] = (0, external_react_namespaceObject.useState)(false);
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const internalFileList = (0, external_react_namespaceObject.useRef)([]);
    const { wrapSSR, hashId, componentCls: prefixCls } = (0, style_js_namespaceObject.useStyles)();
    const showSelectButton = void 0 === selectFile && void 0 === quickUpload;
    (0, external_react_namespaceObject.useEffect)(()=>{
        const fileList = (0, shared_js_namespaceObject.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, jsx_runtime_namespaceObject.jsxs)("div", {
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                className: external_classnames_default()("".concat(prefixCls, "-wrapper"), "".concat(prefixCls, "-picture-card-wrapper"), 'tb-upload', hashId),
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                    className: external_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, shared_js_namespaceObject.isImage)(file.extname)) {
                                    setVisible(true);
                                    setPhotoIndex(index);
                                } else (0, external_file_saver_namespaceObject.saveAs)(file.url, "".concat(file.title).concat(file.extname));
                            };
                            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                                className: "".concat(prefixCls, "-list-picture-card-container ").concat(prefixCls, "-list-item-container"),
                                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                                    className: external_classnames_default()("".concat(prefixCls, "-list-item"), "".concat(prefixCls, "-list-item-done"), "".concat(prefixCls, "-list-item-list-type-picture-card")),
                                    children: [
                                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                                            className: "".concat(prefixCls, "-list-item-info"),
                                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("span", {
                                                className: "".concat(prefixCls, "-span"),
                                                children: [
                                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("a", {
                                                        className: "".concat(prefixCls, "-list-item-thumbnail"),
                                                        href: file.url,
                                                        target: "_blank",
                                                        rel: "noopener noreferrer",
                                                        onClick: handleClick,
                                                        children: file.imageUrl && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("img", {
                                                            src: file.imageUrl,
                                                            alt: file.title,
                                                            className: "".concat(prefixCls, "-list-item-image")
                                                        })
                                                    }),
                                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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, jsx_runtime_namespaceObject.jsx)("span", {
                                            className: "".concat(prefixCls, "-list-item-actions"),
                                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Space, {
                                                size: 3,
                                                children: [
                                                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
                                                        size: 'small',
                                                        type: 'text',
                                                        icon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.DownloadOutlined, {}),
                                                        onClick: ()=>{
                                                            (0, external_file_saver_namespaceObject.saveAs)(file.url, "".concat(file.title).concat(file.extname));
                                                        }
                                                    }),
                                                    !disabled && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
                                                        size: 'small',
                                                        type: 'text',
                                                        icon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.DeleteOutlined, {}),
                                                        onClick: ()=>{
                                                            handleRemove(file);
                                                            internalFileList.current = internalFileList.current.filter((item)=>item.uid !== file.uid);
                                                        }
                                                    })
                                                ]
                                            })
                                        }),
                                        'uploading' === file.status && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                                            className: "".concat(prefixCls, "-list-item-progress"),
                                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Progress, {
                                                strokeWidth: 2,
                                                type: 'line',
                                                showInfo: false,
                                                percent: file.percent
                                            })
                                        })
                                    ]
                                })
                            }, file.uid || file.id);
                        }),
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
                            children: [
                                showSelectButton ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                                    className: external_classnames_default()("".concat(prefixCls, "-list-picture-card-container"), "".concat(prefixCls, "-list-item-container")),
                                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Upload, {
                                        disabled: disabled,
                                        multiple: multiple,
                                        listType: 'picture-card',
                                        showUploadList: false,
                                        onRemove: handleRemove,
                                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                                            style: {
                                                width: '100%',
                                                height: '100%',
                                                display: 'flex',
                                                flexDirection: 'column',
                                                alignItems: 'center',
                                                justifyContent: 'center'
                                            },
                                            onClick: handleSelect,
                                            children: [
                                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {}),
                                                t('Select')
                                            ]
                                        })
                                    })
                                }) : null,
                                quickUpload ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                                    className: external_classnames_default()("".concat(prefixCls, "-list-picture-card-container"), "".concat(prefixCls, "-list-item-container")),
                                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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, shared_js_namespaceObject.toFileList)(list));
                                            null === (_uploadProps_onChange = uploadProps.onChange) || void 0 === _uploadProps_onChange || _uploadProps_onChange.call(uploadProps, {
                                                fileList: list
                                            });
                                        },
                                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                                            style: {
                                                width: '100%',
                                                height: '100%',
                                                display: 'flex',
                                                flexDirection: 'column',
                                                alignItems: 'center',
                                                justifyContent: 'center'
                                            },
                                            children: [
                                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {}),
                                                t('Upload')
                                            ]
                                        })
                                    })
                                }) : null,
                                selectFile ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                                    className: external_classnames_default()("".concat(prefixCls, "-list-picture-card-container"), "".concat(prefixCls, "-list-item-container")),
                                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Upload, {
                                        disabled: disabled,
                                        multiple: multiple,
                                        listType: 'picture-card',
                                        showUploadList: false,
                                        onRemove: handleRemove,
                                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                                            style: {
                                                width: '100%',
                                                height: '100%',
                                                display: 'flex',
                                                flexDirection: 'column',
                                                alignItems: 'center',
                                                justifyContent: 'center'
                                            },
                                            onClick: handleSelect,
                                            children: [
                                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {}),
                                                t('Select')
                                            ]
                                        })
                                    })
                                }) : null
                            ]
                        })
                    ]
                })
            }),
            visible && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(components_namespaceObject.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, jsx_runtime_namespaceObject.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, external_file_saver_namespaceObject.saveAs)(file.url, "".concat(file.title).concat(file.extname));
                        },
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.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;
}
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
