import { jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { useField } from "@tachybase/schema";
import { DownloadOutlined } from "@ant-design/icons";
import { Lightbox, isString } from "@tego/client";
import { Button, Space } from "antd";
import style from "antd/es/upload/style";
import classnames from "classnames";
import { saveAs } from "file-saver";
import { useApp } from "../../../application/hooks/index.mjs";
import { useRecord } from "../../../record-provider/index.mjs";
import { toFileList } from "./shared.mjs";
import { useStyles } from "./style.mjs";
const ReadPretty = ()=>null;
ReadPretty.File = function(props) {
    var _images_fileIndex, _images_, _images_1, _images_fileIndex1;
    const { size, showCount = 0 } = props;
    const record = useRecord();
    const field = useField();
    const images = isString(field.value) ? record : toFileList(field.value);
    const app = useApp();
    const [fileIndex, setFileIndex] = useState(0);
    const [visible, setVisible] = useState(false);
    const { wrapSSR, hashId, componentCls: prefixCls } = useStyles();
    const useUploadStyleVal = style["default"] ? style["default"] : style;
    const previewList = app.AttachmentPreviewManager.get();
    useUploadStyleVal(prefixCls);
    return wrapSSR(/*#__PURE__*/ jsxs("div", {
        children: [
            /*#__PURE__*/ jsx("div", {
                className: classnames("".concat(prefixCls, "-wrapper"), "".concat(prefixCls, "-picture-card-wrapper"), "tb-upload", size ? "tb-upload-".concat(size) : null, hashId),
                children: /*#__PURE__*/ jsx("div", {
                    className: classnames("".concat(prefixCls, "-list"), "".concat(prefixCls, "-list-picture-card")),
                    children: images.map((file, index)=>{
                        if ('small' === size) return 0 === index && /*#__PURE__*/ jsx(ReadFile, {
                            file: file,
                            prefixCls: prefixCls,
                            size: size,
                            images: images,
                            setFileIndex: setFileIndex,
                            setVisible: setVisible,
                            preview: previewList[null == file ? void 0 : file.mimetype] || previewList['default']
                        }, index);
                        return /*#__PURE__*/ jsx(ReadFile, {
                            file: file,
                            prefixCls: prefixCls,
                            size: size,
                            images: images,
                            setFileIndex: setFileIndex,
                            setVisible: setVisible,
                            preview: previewList['image/jpeg']
                        }, index);
                    })
                })
            }),
            visible && /*#__PURE__*/ jsx(Lightbox, {
                mainSrc: null == (_images_fileIndex = images[fileIndex]) ? void 0 : _images_fileIndex.imageUrl,
                nextSrc: null == (_images_ = images[(fileIndex + 1) % images.length]) ? void 0 : _images_.imageUrl,
                prevSrc: null == (_images_1 = images[(fileIndex + images.length - 1) % images.length]) ? 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: (e)=>{
                    e.preventDefault();
                    e.stopPropagation();
                    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.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();
                            e.stopPropagation();
                            const file = images[fileIndex];
                            saveAs(file.url, "".concat(file.title).concat(file.extname));
                        },
                        children: /*#__PURE__*/ jsx(DownloadOutlined, {})
                    }, 'download')
                ]
            })
        ]
    }));
};
ReadPretty.Upload = function() {
    const field = useField();
    return (field.value || []).map((item)=>/*#__PURE__*/ jsx("div", {
            children: item.url ? /*#__PURE__*/ jsx("a", {
                target: '_blank',
                href: item.url,
                rel: "noreferrer",
                children: item.name
            }) : /*#__PURE__*/ jsx("span", {
                children: item.name
            })
        }, item.name));
};
const ReadFile = (param)=>{
    let { file, prefixCls, size, images, setFileIndex, setVisible, preview } = param;
    const { viewComponet } = preview;
    const handleClick = (e)=>{
        const index = images.indexOf(file);
        e.preventDefault();
        e.stopPropagation();
        setVisible(true);
        setFileIndex(index);
    };
    return /*#__PURE__*/ jsxs("div", {
        className: classnames("".concat(prefixCls, "-list-picture-card-container"), "".concat(prefixCls, "-list-item-container")),
        style: {
            position: 'relative'
        },
        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,
                                    style: {
                                        lineHeight: '100%'
                                    },
                                    children: viewComponet({
                                        images,
                                        size,
                                        prefixCls,
                                        file,
                                        setFileIndex,
                                        setVisible
                                    })
                                }),
                                /*#__PURE__*/ jsx("a", {
                                    target: "_blank",
                                    rel: "noopener noreferrer",
                                    className: "".concat(prefixCls, "-list-item-name"),
                                    title: file.title,
                                    href: file.url,
                                    onClick: handleClick,
                                    children: file.title
                                })
                            ]
                        })
                    }),
                    'small' !== size && /*#__PURE__*/ jsx("span", {
                        className: "".concat(prefixCls, "-list-item-actions"),
                        children: /*#__PURE__*/ jsx(Space, {
                            size: 3,
                            children: /*#__PURE__*/ jsx(Button, {
                                size: 'small',
                                type: 'text',
                                icon: /*#__PURE__*/ jsx(DownloadOutlined, {}),
                                onClick: (e)=>{
                                    e.preventDefault();
                                    e.stopPropagation();
                                    saveAs(file.url, "".concat(file.title).concat(file.extname));
                                }
                            })
                        })
                    })
                ]
            }),
            images.length > 1 && 'small' === size && /*#__PURE__*/ jsx("div", {
                style: {
                    position: 'absolute',
                    bottom: '0',
                    right: '0',
                    backgroundColor: '#9b999992',
                    width: '50%',
                    height: '30%',
                    lineHeight: '30%',
                    borderRadius: '40%',
                    textAlign: 'center'
                },
                children: "..."
            })
        ]
    }, file.name);
};
export { ReadFile, ReadPretty };
