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