"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PDF = void 0;
const pdfmake_js_1 = require("./external/pdfmake.js");
class PDF {
    constructor() {
        this.buffers = [];
        this.printer = new pdfmake_js_1.default({
            Courier: {
                normal: "Courier",
                bold: "Courier-Bold",
                italics: "Courier-Oblique",
                bolditalics: "Courier-BoldOblique"
            },
            Helvetica: {
                normal: "Helvetica",
                bold: "Helvetica-Bold",
                italics: "Helvetica-Oblique",
                bolditalics: "Helvetica-BoldOblique"
            },
            Times: {
                normal: "Times-Roman",
                bold: "Times-Bold",
                italics: "Times-Italic",
                bolditalics: "Times-BoldItalic"
            },
            Symbol: {
                normal: "Symbol"
            },
            ZapfDingbats: {
                normal: "ZapfDingbats"
            }
        });
    }
    async write(pdfMake) {
        const supportedFonts = [
            "Courier",
            "Helvetica",
            "Times",
            "Symbol",
            "ZapfDingbats"
        ];
        if (!pdfMake.defaultStyle ||
            !pdfMake.defaultStyle.font ||
            supportedFonts.indexOf(pdfMake.defaultStyle.font) !== -1) {
            pdfMake.defaultStyle = {
                font: "Helvetica"
            };
        }
        this.document = await this.printer.createPdfKitDocument(pdfMake);
        this.document.on("data", ui8a => {
            if (ui8a) {
                this.buffers.push(Buffer.from(ui8a));
            }
        });
        this.document.on("end", () => {
            this.data = Buffer.concat(this.buffers);
        });
        return true;
    }
    getData(encoding = "base64") {
        return new Promise((resolve, reject) => {
            this.getDataBuffer()
                .then(buffer => {
                resolve(buffer.toString(encoding));
            })
                .catch(reject);
        });
    }
    getDataURI() {
        return new Promise((resolve, reject) => {
            this.getData()
                .then(data => {
                resolve("data:application/pdf;base64," + data);
            })
                .catch(reject);
        });
    }
    getDataBuffer() {
        return new Promise((resolve, reject) => {
            if (!this.data) {
                this.document.end();
                setTimeout(() => {
                    resolve(this.data);
                }, 100);
            }
            else {
                resolve(this.data);
            }
        });
    }
}
exports.PDF = PDF;
//# sourceMappingURL=index.js.map