/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __read } from 'tslib';
import React__default from 'react';
import { getGlobalOptions, themeable } from 'amis-core';
import { pdfjs, Document, Page } from 'react-pdf';
import { Icon } from './icons.js';
import Input from './Input.js';
import Spinner from './Spinner.js';

/**
 * @file PdfViewer.tsx PDF 预览
 *
 * @created: 2024/02/26
 */
pdfjs.GlobalWorkerOptions.workerSrc = getGlobalOptions().pdfjsWorkerSrc;
var PdfViewer = function (props) {
    var cx = props.classnames, className = props.className, loading = props.loading, _a = props.width, width = _a === void 0 ? 300 : _a;
    var _b = __read(React__default.useState(props.file), 2), file = _b[0], setFile = _b[1];
    var _c = __read(React__default.useState(false), 2), loaded = _c[0], setLoaded = _c[1];
    var _d = __read(React__default.useState(1), 2), page = _d[0], setPage = _d[1];
    var _e = __read(React__default.useState(1), 2), scale = _e[0], setScale = _e[1];
    var _f = __read(React__default.useState(1), 2), total = _f[0], setTotal = _f[1];
    var inputRef = React__default.useRef();
    React__default.useEffect(function () {
        if (props.file instanceof ArrayBuffer && props.file.byteLength > 0) {
            setFile(props.file);
        }
        else {
            setFile(undefined);
        }
    }, [props.file]);
    function handleLoadSuccess(_a) {
        var numPages = _a.numPages;
        setLoaded(true);
        setTotal(numPages);
    }
    function handleChangePage(idx) {
        var newPage = page + idx;
        if (newPage <= 0 || newPage > total) {
            return;
        }
        setPage(newPage);
    }
    function handlePageBlur(event) {
        var newPage = +event.target.value;
        if (isNaN(newPage) || newPage <= 0 || newPage > total) {
            if (inputRef.current) {
                inputRef.current.value = page + '';
            }
            return;
        }
        setPage(newPage);
    }
    function handleChangeScale(t) {
        setScale(scale * t);
    }
    function renderLoading() {
        return (React__default.createElement("div", { className: cx('PdfViewer-Loading') },
            React__default.createElement(Spinner, null)));
    }
    function renderTool() {
        return (React__default.createElement("div", { className: cx('PdfViewer-Tool') },
            React__default.createElement(Icon, { className: "icon", icon: "prev", onClick: function () { return handleChangePage(-1); } }),
            React__default.createElement(Input, { className: "page-input", value: page, onBlur: handlePageBlur, ref: inputRef }),
            React__default.createElement("span", { className: "gap" }, "/"),
            React__default.createElement("span", null, total),
            React__default.createElement(Icon, { className: "icon", icon: "next", onClick: function () { return handleChangePage(1); } }),
            React__default.createElement(Icon, { className: "icon", icon: "zoom-in", onClick: function () { return handleChangeScale(1.2); } }),
            React__default.createElement(Icon, { className: "icon", icon: "zoom-out", onClick: function () { return handleChangeScale(0.8); } })));
    }
    return (React__default.createElement("div", { className: cx(className, 'PdfViewer') }, !file || loading ? (renderLoading()) : (React__default.createElement(React__default.Fragment, null,
        React__default.createElement("div", { className: cx('PdfViewer-Content', { 'is-loaded': loaded }) },
            React__default.createElement(Document, { file: file, onLoadSuccess: handleLoadSuccess, onLoadError: function (err) { return console.log(err); }, loading: renderLoading() },
                React__default.createElement(Page, { className: cx('PdfViewer-Content-Page'), pageNumber: page, width: width, height: props.height, loading: renderLoading(), noData: React__default.createElement("div", null, "No PDF data"), scale: scale, renderTextLayer: false, renderAnnotationLayer: false }))),
        loaded ? renderTool() : null))));
};
var PdfViewer$1 = themeable(PdfViewer);

export { PdfViewer$1 as default };
