import { jsx, jsxs } from "react/jsx-runtime";
import { useRef, useState } from "react";
import { Tag } from "antd";
import { useTranslation } from "react-i18next";
import { Document, Page, pdfjs } from "react-pdf";
import { TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";
import { useStyles } from "./file-pdf.style.mjs";
import "react-pdf/dist/Page/AnnotationLayer.css";
import "react-pdf/dist/Page/TextLayer.css";
import { useIsMobile } from "../../../block-provider/index.mjs";
const options = {
    cMapUrl: "https://assets.tachybase.com/pdfjs-dist@".concat(pdfjs.version, "/cmaps/"),
    cMapPacked: true,
    standardFontDataUrl: "https://assets.tachybase.com/pdfjs-dist@".concat(pdfjs.version, "/standard_fonts/")
};
pdfjs.GlobalWorkerOptions.workerSrc = "https://assets.tachybase.com/pdfjs-dist@".concat(pdfjs.version, "/build/pdf.worker.min.js");
const TransformInternal = (param)=>{
    let { noTransformWrapper = false, children } = param;
    return noTransformWrapper ? children : /*#__PURE__*/ jsx(TransformWrapper, {
        children: /*#__PURE__*/ jsx(TransformComponent, {
            children: children
        })
    });
};
const CheckedComponent = (props)=>{
    const { file, fileInfo, width, noTransformWrapper } = props;
    const pdfInfo = (null == file ? void 0 : file.url) || fileInfo;
    const { t } = useTranslation();
    const { styles } = useStyles();
    const containerRef = useRef(null);
    const [pdfWidth, setPdfWidth] = useState(600);
    const [numPages, setNumPages] = useState(1);
    const isMobile = useIsMobile();
    const onDocumentLoadSuccess = async (params)=>{
        var _containerRef_current;
        const { numPages, _transport } = params;
        const containerWidth = null == (_containerRef_current = containerRef.current) ? void 0 : _containerRef_current.offsetWidth;
        const page = await _transport.getPage(1);
        const originalWidth = page.getViewport({
            scale: 1
        }).width;
        const scale = containerWidth / originalWidth;
        const viewport = page.getViewport({
            scale
        });
        setNumPages(numPages);
        setPdfWidth(viewport.width);
    };
    return /*#__PURE__*/ jsx(Document, {
        className: styles.container,
        inputRef: containerRef,
        file: pdfInfo,
        options: options,
        onLoadError: (error)=>{
            var _window_Sentry, _window;
            null == (_window = window) || null == (_window_Sentry = _window.Sentry) || _window_Sentry.captureException(error);
        },
        noData: /*#__PURE__*/ jsx("div", {
            style: {
                height: '100vh'
            }
        }),
        error: /*#__PURE__*/ jsx("div", {
            children: t('Load PDF file error')
        }),
        loading: "",
        onLoadSuccess: onDocumentLoadSuccess,
        children: /*#__PURE__*/ jsx(TransformInternal, {
            noTransformWrapper: noTransformWrapper || !isMobile,
            children: Array.from(new Array(numPages), (el, index)=>/*#__PURE__*/ jsx(Page, {
                    pageNumber: index + 1,
                    width: width || pdfWidth,
                    children: /*#__PURE__*/ jsx("div", {
                        className: styles.footer,
                        children: /*#__PURE__*/ jsxs(Tag, {
                            className: styles.footerText,
                            bordered: false,
                            children: [
                                index + 1,
                                "/",
                                numPages
                            ]
                        })
                    })
                }, "page_".concat(index + 1)))
        })
    });
};
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 filePdf = {
    key: 'application/pdf',
    type: 'application/pdf',
    viewComponet: (props)=>/*#__PURE__*/ jsx(ViewComponent, {
            ...props
        }),
    checkedComponent: (props)=>/*#__PURE__*/ jsx(CheckedComponent, {
            ...props
        })
};
export { filePdf };
