import { jsx, jsxs } from "react/jsx-runtime";
import { useEffect, useRef, useState } from "react";
import excel from "@js-preview/excel";
import { Spin } from "antd";
import { useStyles } from "./file-sheet.style.mjs";
import "@js-preview/excel/lib/index.css";
const ExcelView = (props)=>{
    const { file } = props;
    const fileUrl = file.url;
    const { styles } = useStyles();
    const excelContainerRef = useRef(null);
    const excelPreviewerRef = useRef(null);
    const [isLoading, setIsLoading] = useState(true);
    useEffect(()=>{
        const containerElement = excelContainerRef.current;
        if (containerElement && !excelPreviewerRef.current) {
            const myExcelPreviewer = excel.init(containerElement);
            excelPreviewerRef.current = myExcelPreviewer;
            setIsLoading(true);
            const handleShowPreviewer = async ()=>{
                await myExcelPreviewer.preview(fileUrl);
                setIsLoading(false);
            };
            try {
                handleShowPreviewer();
            } catch (e) {
                console.log("%c Line:36 \uD83E\uDD5F e", e);
            }
        }
    }, [
        fileUrl
    ]);
    return /*#__PURE__*/ jsxs("div", {
        className: styles.excelPreview,
        children: [
            /*#__PURE__*/ jsx("div", {
                ref: excelContainerRef,
                className: styles.container
            }),
            isLoading && /*#__PURE__*/ jsx("div", {
                className: styles.loading,
                children: /*#__PURE__*/ jsx(Spin, {
                    size: "large"
                })
            })
        ]
    });
};
const OnlineView = (props)=>{
    const { file } = props;
    var _file_url;
    let fileUrl = null != (_file_url = file.url) ? _file_url : '';
    if (fileUrl.startsWith('/storage') && window.location.origin) fileUrl = window.location.origin + fileUrl;
    return /*#__PURE__*/ jsx("iframe", {
        src: "https://view.officeapps.live.com/op/embed.aspx?src=".concat(fileUrl),
        width: "100%",
        height: "600px",
        frameBorder: "0"
    });
};
const ViewComponent = (props)=>{
    const { file, prefixCls } = props;
    return file.imageUrl && /*#__PURE__*/ jsx("img", {
        src: "".concat(file.imageUrl).concat(file.thumbnailRule || ''),
        style: {
            width: '100%',
            height: '100%'
        },
        alt: file.title,
        className: "".concat(prefixCls, "-list-item-image")
    });
};
const fileXLS = {
    key: 'application/vnd.ms-excel',
    type: 'application/vnd.ms-excel',
    viewComponet: (props)=>/*#__PURE__*/ jsx(ViewComponent, {
            ...props
        }),
    checkedComponent: (props)=>/*#__PURE__*/ jsx(OnlineView, {
            ...props
        })
};
const fileXLSX = {
    key: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    viewComponet: (props)=>/*#__PURE__*/ jsx(ViewComponent, {
            ...props
        }),
    checkedComponent: (props)=>/*#__PURE__*/ jsx(OnlineView, {
            ...props
        })
};
const fileCSV = {
    key: 'text/csv',
    type: 'text/csv',
    viewComponet: (props)=>/*#__PURE__*/ jsx(ViewComponent, {
            ...props
        }),
    checkedComponent: (props)=>/*#__PURE__*/ jsx(ExcelView, {
            ...props
        })
};
export { fileCSV, fileXLS, fileXLSX };
