import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useEffect, useRef, useState } from "react";
import { Lightbox } from "@tachybase/components";
import { connect, mapReadPretty } from "@tachybase/schema";
import { DeleteOutlined, DownloadOutlined, PlusOutlined } from "@ant-design/icons";
import { Button, Progress, Space, Upload } from "antd";
import classnames from "classnames";
import { saveAs } from "file-saver";
import { useTranslation } from "react-i18next";
import { ReadPretty } from "../upload/ReadPretty.mjs";
import { isImage, toFileList, useUploadProps } from "../upload/shared.mjs";
import { useStyles } from "../upload/style.mjs";
const Preview_Preview = connect((props)=>/*#__PURE__*/ jsx(ReadPretty.File, {
        ...props
    }), mapReadPretty(ReadPretty.File));
const FileSelector = (props)=>{
    var _fileList_photoIndex, _fileList_, _fileList_1, _fileList_photoIndex1;
    const { disabled, multiple, value, quickUpload, selectFile, onRemove, onSelect } = props;
    const uploadProps = useUploadProps({
        ...props
    });
    const [fileList, setFileList] = useState([]);
    const [photoIndex, setPhotoIndex] = useState(0);
    const [visible, setVisible] = useState(false);
    const { t } = useTranslation();
    const internalFileList = useRef([]);
    const { wrapSSR, hashId, componentCls: prefixCls } = useStyles();
    const showSelectButton = void 0 === selectFile && void 0 === quickUpload;
    useEffect(()=>{
        const fileList = 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__*/ jsxs("div", {
        children: [
            /*#__PURE__*/ jsx("div", {
                className: classnames("".concat(prefixCls, "-wrapper"), "".concat(prefixCls, "-picture-card-wrapper"), 'tb-upload', hashId),
                children: /*#__PURE__*/ jsxs("div", {
                    className: classnames("".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 (isImage(file.extname)) {
                                    setVisible(true);
                                    setPhotoIndex(index);
                                } else saveAs(file.url, "".concat(file.title).concat(file.extname));
                            };
                            return /*#__PURE__*/ jsx("div", {
                                className: "".concat(prefixCls, "-list-picture-card-container ").concat(prefixCls, "-list-item-container"),
                                children: /*#__PURE__*/ jsxs("div", {
                                    className: classnames("".concat(prefixCls, "-list-item"), "".concat(prefixCls, "-list-item-done"), "".concat(prefixCls, "-list-item-list-type-picture-card")),
                                    children: [
                                        /*#__PURE__*/ jsx("div", {
                                            className: "".concat(prefixCls, "-list-item-info"),
                                            children: /*#__PURE__*/ jsxs("span", {
                                                className: "".concat(prefixCls, "-span"),
                                                children: [
                                                    /*#__PURE__*/ jsx("a", {
                                                        className: "".concat(prefixCls, "-list-item-thumbnail"),
                                                        href: file.url,
                                                        target: "_blank",
                                                        rel: "noopener noreferrer",
                                                        onClick: handleClick,
                                                        children: file.imageUrl && /*#__PURE__*/ jsx("img", {
                                                            src: file.imageUrl,
                                                            alt: file.title,
                                                            className: "".concat(prefixCls, "-list-item-image")
                                                        })
                                                    }),
                                                    /*#__PURE__*/ 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__*/ jsx("span", {
                                            className: "".concat(prefixCls, "-list-item-actions"),
                                            children: /*#__PURE__*/ jsxs(Space, {
                                                size: 3,
                                                children: [
                                                    /*#__PURE__*/ jsx(Button, {
                                                        size: 'small',
                                                        type: 'text',
                                                        icon: /*#__PURE__*/ jsx(DownloadOutlined, {}),
                                                        onClick: ()=>{
                                                            saveAs(file.url, "".concat(file.title).concat(file.extname));
                                                        }
                                                    }),
                                                    !disabled && /*#__PURE__*/ jsx(Button, {
                                                        size: 'small',
                                                        type: 'text',
                                                        icon: /*#__PURE__*/ jsx(DeleteOutlined, {}),
                                                        onClick: ()=>{
                                                            handleRemove(file);
                                                            internalFileList.current = internalFileList.current.filter((item)=>item.uid !== file.uid);
                                                        }
                                                    })
                                                ]
                                            })
                                        }),
                                        'uploading' === file.status && /*#__PURE__*/ jsx("div", {
                                            className: "".concat(prefixCls, "-list-item-progress"),
                                            children: /*#__PURE__*/ jsx(Progress, {
                                                strokeWidth: 2,
                                                type: 'line',
                                                showInfo: false,
                                                percent: file.percent
                                            })
                                        })
                                    ]
                                })
                            }, file.uid || file.id);
                        }),
                        /*#__PURE__*/ jsxs(Fragment, {
                            children: [
                                showSelectButton ? /*#__PURE__*/ jsx("div", {
                                    className: classnames("".concat(prefixCls, "-list-picture-card-container"), "".concat(prefixCls, "-list-item-container")),
                                    children: /*#__PURE__*/ jsx(Upload, {
                                        disabled: disabled,
                                        multiple: multiple,
                                        listType: 'picture-card',
                                        showUploadList: false,
                                        onRemove: handleRemove,
                                        children: /*#__PURE__*/ jsxs("div", {
                                            style: {
                                                width: '100%',
                                                height: '100%',
                                                display: 'flex',
                                                flexDirection: 'column',
                                                alignItems: 'center',
                                                justifyContent: 'center'
                                            },
                                            onClick: handleSelect,
                                            children: [
                                                /*#__PURE__*/ jsx(PlusOutlined, {}),
                                                t('Select')
                                            ]
                                        })
                                    })
                                }) : null,
                                quickUpload ? /*#__PURE__*/ jsx("div", {
                                    className: classnames("".concat(prefixCls, "-list-picture-card-container"), "".concat(prefixCls, "-list-item-container")),
                                    children: /*#__PURE__*/ jsx(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(toFileList(list));
                                            null == (_uploadProps_onChange = uploadProps.onChange) || _uploadProps_onChange.call(uploadProps, {
                                                fileList: list
                                            });
                                        },
                                        children: /*#__PURE__*/ jsxs("div", {
                                            style: {
                                                width: '100%',
                                                height: '100%',
                                                display: 'flex',
                                                flexDirection: 'column',
                                                alignItems: 'center',
                                                justifyContent: 'center'
                                            },
                                            children: [
                                                /*#__PURE__*/ jsx(PlusOutlined, {}),
                                                t('Upload')
                                            ]
                                        })
                                    })
                                }) : null,
                                selectFile ? /*#__PURE__*/ jsx("div", {
                                    className: classnames("".concat(prefixCls, "-list-picture-card-container"), "".concat(prefixCls, "-list-item-container")),
                                    children: /*#__PURE__*/ jsx(Upload, {
                                        disabled: disabled,
                                        multiple: multiple,
                                        listType: 'picture-card',
                                        showUploadList: false,
                                        onRemove: handleRemove,
                                        children: /*#__PURE__*/ jsxs("div", {
                                            style: {
                                                width: '100%',
                                                height: '100%',
                                                display: 'flex',
                                                flexDirection: 'column',
                                                alignItems: 'center',
                                                justifyContent: 'center'
                                            },
                                            onClick: handleSelect,
                                            children: [
                                                /*#__PURE__*/ jsx(PlusOutlined, {}),
                                                t('Select')
                                            ]
                                        })
                                    })
                                }) : null
                            ]
                        })
                    ]
                })
            }),
            visible && /*#__PURE__*/ jsx(Lightbox, {
                mainSrc: null == (_fileList_photoIndex = fileList[photoIndex]) ? void 0 : _fileList_photoIndex.imageUrl,
                nextSrc: null == (_fileList_ = fileList[(photoIndex + 1) % fileList.length]) ? void 0 : _fileList_.imageUrl,
                prevSrc: null == (_fileList_1 = fileList[(photoIndex + fileList.length - 1) % fileList.length]) ? 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.title,
                toolbarButtons: [
                    /*#__PURE__*/ 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];
                            saveAs(file.url, "".concat(file.title).concat(file.extname));
                        },
                        children: /*#__PURE__*/ jsx(DownloadOutlined, {})
                    }, 'preview-img')
                ]
            })
        ]
    }));
};
const Preview = Preview_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 as Preview, Preview as default };
