import React, { useState } from "react";
import { Document, Page, pdfjs } from "react-pdf";
import { repeat } from "../../utils";
// TODO check this https://github.com/wojtekmaj/react-pdf#browserify-and-others
pdfjs.GlobalWorkerOptions.workerSrc = "//cdnjs.cloudflare.com/ajax/libs/pdf.js/" + pdfjs.version + "/pdf.worker.js";
/**
 * Component rendering pdf attachments. Uses [react-pdf](http://projects.wojtekmaj.pl/react-pdf/) under the hood.
 */
export var PdfRenderer = function (_a) {
    var attachment = _a.attachment;
    var _b = useState(0), numberOfPages = _b[0], setNumberOfPages = _b[1];
    var pdfData = "";
    var data = attachment.data;
    var splittedArr = data.split("data:application/pdf;base64,");
    pdfData = splittedArr.length > 1 ? splittedArr[1] : splittedArr[0];
    return (React.createElement(Document, { file: "data:application/pdf;base64," + pdfData, onLoadSuccess: function (_a) {
            var numPages = _a.numPages;
            setNumberOfPages(numPages);
        } },
        React.createElement("style", { scoped: true, dangerouslySetInnerHTML: {
                __html: "\n      canvas {\n        margin: auto;\n      }\n    ",
            } }),
        repeat(numberOfPages)(function (index) { return (
        // TODO: Dynamically resize width to fit container
        // https://github.com/wojtekmaj/react-pdf/issues/129
        React.createElement(Page, { key: "page_" + (index + 1), pageNumber: index + 1 })); })));
};
//# sourceMappingURL=PdfRenderer.js.map