"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__, {
    Upload: ()=>Upload,
    UploadReadFile: ()=>UploadReadFile,
    default: ()=>Upload_rslib_entry_
});
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 index_js_namespaceObject = require("../../../application/index.js");
const withDynamicSchemaProps_js_namespaceObject = require("../../../application/hoc/withDynamicSchemaProps.js");
const useProps_js_namespaceObject = require("../../hooks/useProps.js");
const external_ReadPretty_js_namespaceObject = require("./ReadPretty.js");
const external_shared_js_namespaceObject = require("./shared.js");
const external_style_js_namespaceObject = require("./style.js");
const Upload = (0, schema_namespaceObject.connect)((props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Upload, {
        ...(0, external_shared_js_namespaceObject.useUploadProps)(props)
    }), (0, schema_namespaceObject.mapProps)({
    value: 'fileList'
}), (0, schema_namespaceObject.mapReadPretty)(external_ReadPretty_js_namespaceObject.ReadPretty.Upload));
Upload.Attachment = (0, schema_namespaceObject.connect)((props)=>{
    var _images_fileIndex, _images_, _images_1, _images_fileIndex1;
    const { disabled, multiple, value, onChange } = props;
    const [fileList, setFileList] = (0, external_react_namespaceObject.useState)([]);
    const [sync, setSync] = (0, external_react_namespaceObject.useState)(true);
    const app = (0, index_js_namespaceObject.useApp)();
    const previewList = app.AttachmentPreviewManager.get();
    const images = fileList;
    const [fileIndex, setFileIndex] = (0, external_react_namespaceObject.useState)(0);
    const [visible, setVisible] = (0, external_react_namespaceObject.useState)(false);
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const uploadProps = (0, external_shared_js_namespaceObject.useUploadProps)({
        ...props
    });
    const { wrapSSR, hashId, componentCls: prefixCls } = (0, external_style_js_namespaceObject.useStyles)();
    const internalFileList = (0, external_react_namespaceObject.useRef)([]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (sync) {
            const fileList = (0, external_shared_js_namespaceObject.toFileList)(value);
            setFileList(fileList);
            internalFileList.current = fileList;
        }
    }, [
        value,
        sync
    ]);
    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: [
                        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, external_react_namespaceObject.createElement)(UploadReadFile, {
                                ...fileProps,
                                key: index
                            });
                        }),
                        !disabled && (multiple || (0, external_shared_js_namespaceObject.toArr)(value).length < 1) && /*#__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,
                                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, external_shared_js_namespaceObject.toValue)(list));
                                        null == onChange || onChange((0, external_shared_js_namespaceObject.toValue)(list));
                                        setFileList(list.map(external_shared_js_namespaceObject.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, external_shared_js_namespaceObject.toItem)(info.file)
                                        ]);
                                        setSync(true);
                                    }
                                },
                                showUploadList: false,
                                children: !disabled && (multiple || (0, external_shared_js_namespaceObject.toArr)(value).length < 1) && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("span", {
                                    children: [
                                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {}),
                                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("br", {}),
                                        " ",
                                        t('Upload')
                                    ]
                                })
                            })
                        })
                    ]
                })
            }),
            visible && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(components_namespaceObject.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, 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 = images[fileIndex];
                            (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')
                ]
            })
        ]
    }));
}, (0, schema_namespaceObject.mapReadPretty)(external_ReadPretty_js_namespaceObject.ReadPretty.File));
Upload.Dragger = (0, schema_namespaceObject.connect)((props)=>{
    const { tipContent } = props;
    const { wrapSSR, hashId, componentCls: prefixCls } = (0, external_style_js_namespaceObject.useStyles)();
    return wrapSSR(/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: external_classnames_default()("".concat(prefixCls, "-dragger"), hashId),
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Upload.Dragger, {
            ...(0, external_shared_js_namespaceObject.useUploadProps)(props),
            children: [
                tipContent,
                props.children
            ]
        })
    }));
}, (0, schema_namespaceObject.mapProps)({
    value: 'fileList'
}));
Upload.DraggerV2 = (0, withDynamicSchemaProps_js_namespaceObject.withDynamicSchemaProps)((0, schema_namespaceObject.connect)((props)=>{
    const { t } = (0, external_react_i18next_namespaceObject.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, useProps_js_namespaceObject.useProps)(props);
    const [loading, setLoading] = (0, external_react_namespaceObject.useState)(false);
    const { wrapSSR, hashId, componentCls: prefixCls } = (0, external_style_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)("div", {
        className: external_classnames_default()("".concat(prefixCls, "-dragger"), hashId),
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Upload.Dragger, {
            ...(0, external_shared_js_namespaceObject.useUploadProps)({
                ...props,
                ...extraProps,
                onChange: handleChange
            }),
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("p", {
                    className: "".concat(prefixCls, "-drag-icon"),
                    children: loading ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.LoadingOutlined, {
                        style: {
                            fontSize: 36
                        },
                        spin: true
                    }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.InboxOutlined, {})
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("p", {
                    className: "".concat(prefixCls, "-text"),
                    children: title
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("p", {
                    className: "".concat(prefixCls, "-hint"),
                    children: subTitle
                })
            ]
        })
    }));
}, (0, schema_namespaceObject.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, 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,
                                style: {
                                    lineHeight: '100%'
                                },
                                children: viewComponet({
                                    prefixCls,
                                    file,
                                    setFileIndex,
                                    setVisible
                                })
                            }),
                            /*#__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: ()=>{
                                    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, external_shared_js_namespaceObject.toValue)([
                                            ...prevFileList
                                        ]));
                                        return [
                                            ...prevFileList
                                        ];
                                    });
                                }
                            })
                        ]
                    })
                }),
                '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);
};
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
});
