/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');
var reactPdf = require('react-pdf');
var icons = require('./icons.js');
var Input = require('./Input.js');
var Spinner = require('./Spinner.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
reactPdf.pdfjs.GlobalWorkerOptions.workerSrc = amisCore.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 = tslib.__read(React__default["default"].useState(props.file), 2), file = _b[0], setFile = _b[1];
    var _c = tslib.__read(React__default["default"].useState(false), 2), loaded = _c[0], setLoaded = _c[1];
    var _d = tslib.__read(React__default["default"].useState(1), 2), page = _d[0], setPage = _d[1];
    var _e = tslib.__read(React__default["default"].useState(1), 2), scale = _e[0], setScale = _e[1];
    var _f = tslib.__read(React__default["default"].useState(1), 2), total = _f[0], setTotal = _f[1];
    var inputRef = React__default["default"].useRef();
    React__default["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 (_J$X_("div", { className: cx('PdfViewer-Loading') },
            _J$X_(Spinner["default"], null)));
    }
    function renderTool() {
        return (_J$X_("div", { className: cx('PdfViewer-Tool') },
            _J$X_(icons.Icon, { className: "icon", icon: "prev", onClick: function () { return handleChangePage(-1); } }),
            _J$X_(Input["default"], { className: "page-input", value: page, onBlur: handlePageBlur, ref: inputRef }),
            _J$X_("span", { className: "gap" }, "/"),
            _J$X_("span", null, total),
            _J$X_(icons.Icon, { className: "icon", icon: "next", onClick: function () { return handleChangePage(1); } }),
            _J$X_(icons.Icon, { className: "icon", icon: "zoom-in", onClick: function () { return handleChangeScale(1.2); } }),
            _J$X_(icons.Icon, { className: "icon", icon: "zoom-out", onClick: function () { return handleChangeScale(0.8); } })));
    }
    return (_J$X_("div", { className: cx(className, 'PdfViewer') }, !file || loading ? (renderLoading()) : (_J$X_(React__default["default"].Fragment, null,
        _J$X_("div", { className: cx('PdfViewer-Content', { 'is-loaded': loaded }) },
            _J$X_(reactPdf.Document, { file: file, onLoadSuccess: handleLoadSuccess, onLoadError: function (err) { return console.log(err); }, loading: renderLoading() },
                _J$X_(reactPdf.Page, { className: cx('PdfViewer-Content-Page'), pageNumber: page, width: width, height: props.height, loading: renderLoading(), noData: _J$X_("div", null, "No PDF data"), scale: scale, renderTextLayer: false, renderAnnotationLayer: false }))),
        loaded ? renderTool() : null))));
};
var PdfViewer$1 = amisCore.themeable(PdfViewer);

exports["default"] = PdfViewer$1;
