import { SdCommonModule } from '@sd-angular/core/common';
import { __awaiter, __classPrivateFieldGet } from 'tslib';
import { Injectable, NgModule } from '@angular/core';
import { v4 } from 'uuid';
import { PDFDocument } from 'pdf-lib';
import { CommonModule } from '@angular/common';

var _drawPageFooter;
class SdPdfService {
    constructor() {
        this.merge = (urls, option = { download: true }) => __awaiter(this, void 0, void 0, function* () {
            const mergedPdf = yield PDFDocument.create();
            for (const url of urls) {
                const existingPdfBytes = yield fetch(url).then(res => res.arrayBuffer());
                const file = yield PDFDocument.load(existingPdfBytes);
                const copied = yield mergedPdf.copyPages(file, file.getPageIndices());
                copied.forEach((page) => mergedPdf.addPage(page));
            }
            if ((option === null || option === void 0 ? void 0 : option.isInsertFooter) && (option === null || option === void 0 ? void 0 : option.footerOption)) {
                __classPrivateFieldGet(this, _drawPageFooter).call(this, mergedPdf, option.footerOption);
            }
            const mergedPdfFile = yield mergedPdf.save();
            const blob = new Blob([mergedPdfFile], { type: 'application/pdf' });
            if (option === null || option === void 0 ? void 0 : option.download) {
                SdUtility.downloadBlob(blob, v4());
            }
            return blob;
        });
        _drawPageFooter.set(this, (pdf, option) => {
            const numberOfPages = pdf.getPageCount();
            pdf.getPages().forEach((page, idx) => {
                var _a, _b, _c;
                page.drawText(`${idx + 1}/${numberOfPages}`, {
                    x: (_a = option === null || option === void 0 ? void 0 : option.offsetX) !== null && _a !== void 0 ? _a : 15,
                    y: (_b = option === null || option === void 0 ? void 0 : option.offsetY) !== null && _b !== void 0 ? _b : 15,
                    size: (_c = option === null || option === void 0 ? void 0 : option.size) !== null && _c !== void 0 ? _c : 9,
                });
            });
        });
    }
}
_drawPageFooter = new WeakMap();
SdPdfService.decorators = [
    { type: Injectable }
];
SdPdfService.ctorParameters = () => [];

class SdPdfModule {
}
SdPdfModule.decorators = [
    { type: NgModule, args: [{
                imports: [
                    CommonModule,
                    SdCommonModule
                ],
                declarations: [],
                exports: [],
                providers: [
                    SdPdfService
                ]
            },] }
];

/**
 * Generated bundle index. Do not edit.
 */

export { SdPdfModule, SdPdfService };
//# sourceMappingURL=sd-angular-core-pdf.js.map
