var _doc, _options, _getFileName, _getDoc, _getOption, _addFont;
import { __awaiter, __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
import { Injectable, NgZone } from '@angular/core';
import { ZuiD, ZuiO } from '@material-zui/core';
import * as $ from 'jquery';
import { jsPDF } from 'jspdf';
import { ZuiLoadingService } from '../loading';
import { getDefaultOptions } from './data';
import { TimesBold, TimesBoldItalic, TimesItalic, TimesNormal } from './font';
export class ZuiPrintService {
    constructor(zuiLoadingService, ngZone) {
        this.zuiLoadingService = zuiLoadingService;
        this.ngZone = ngZone;
        _doc.set(this, void 0);
        _options.set(this, void 0);
        _getFileName.set(this, (id, options) => {
            const { fileName = id, timeFormat, showTime } = options;
            const time = showTime ? `-${ZuiD.now(timeFormat)}` : '';
            return `${fileName}${time}`;
        });
        _getDoc.set(this, (inputOptions) => {
            const options = __classPrivateFieldGet(this, _getOption).call(this, inputOptions);
            const { jsPdfOption } = options;
            if (jsPdfOption) {
                __classPrivateFieldSet(this, _doc, new jsPDF(jsPdfOption));
                __classPrivateFieldGet(this, _addFont).call(this);
            }
            return __classPrivateFieldGet(this, _doc);
        });
        _getOption.set(this, (options) => ZuiO.mergeDeep(getDefaultOptions(), __classPrivateFieldGet(this, _options), options));
        _addFont.set(this, () => {
            __classPrivateFieldGet(this, _doc).addFileToVFS('times-normal.ttf', TimesNormal);
            __classPrivateFieldGet(this, _doc).addFont('times-normal.ttf', 'times', 'normal');
            __classPrivateFieldGet(this, _doc).addFileToVFS('times-bold.ttf', TimesBold);
            __classPrivateFieldGet(this, _doc).addFont('times-bold.ttf', 'times', 'bold');
            __classPrivateFieldGet(this, _doc).addFileToVFS('times-italic.ttf', TimesItalic);
            __classPrivateFieldGet(this, _doc).addFont('times-italic.ttf', 'times', 'italic');
            __classPrivateFieldGet(this, _doc).addFileToVFS('times-bolditalic.ttf', TimesBoldItalic);
            __classPrivateFieldGet(this, _doc).addFont('times-bolditalic.ttf', 'times', 'bolditalic');
        });
        this.setOption = (options) => {
            __classPrivateFieldSet(this, _options, options);
        };
        /**
         * With `addFont` access url: https://peckconsulting.s3.amazonaws.com/fontconverter/fontconverter.html
         * then upload `.ttf` file to generate `.js`/`.ts` file to import
         * - ex:
         * ```ts
         * this.#doc.addFileToVFS('times-normal.ttf', TimesNormal)
         * this.#doc.addFont('times-normal.ttf', 'times', 'normal')
         * ```
         */
        this.config = (callback) => callback(__classPrivateFieldGet(this, _doc));
        this.print = (id, inputOptions) => {
            const options = __classPrivateFieldGet(this, _getOption).call(this, inputOptions);
            const { getAppElement } = options;
            const printElement = $(`#${id}`).clone();
            const body = $('body');
            const appElement = getAppElement ? getAppElement(body) : body.children().first();
            appElement.addClass('d-print-none');
            const currentTitle = document.title;
            const fileName = __classPrivateFieldGet(this, _getFileName).call(this, id, options);
            document.title = fileName;
            body.append(printElement);
            window.print();
            document.title = currentTitle;
            appElement.removeClass('d-print-none');
            printElement.remove();
        };
        this.getJsPdf = (id, inputOptions) => new Promise(resolve => {
            __classPrivateFieldGet(this, _addFont).call(this);
            const options = __classPrivateFieldGet(this, _getOption).call(this, inputOptions);
            const doc = __classPrivateFieldGet(this, _getDoc).call(this, options);
            const { htmlOption, fontFamily } = options;
            const printEl = $(`#${id}`).clone();
            printEl.css({ display: 'block', fontFamily });
            doc.html(printEl.get(0), Object.assign(Object.assign({}, htmlOption), { callback: jsPdf => {
                    if (htmlOption === null || htmlOption === void 0 ? void 0 : htmlOption.callback)
                        htmlOption.callback(jsPdf);
                    resolve(jsPdf);
                } }));
        });
        this.download = (id, inputOptions) => __awaiter(this, void 0, void 0, function* () {
            const options = __classPrivateFieldGet(this, _getOption).call(this, inputOptions);
            const { showLoading } = options;
            if (showLoading)
                this.zuiLoadingService.start();
            this.ngZone.run(() => __awaiter(this, void 0, void 0, function* () {
                const doc = yield this.getJsPdf(id);
                const fileName = __classPrivateFieldGet(this, _getFileName).call(this, id, options);
                doc.save(fileName);
                this.zuiLoadingService.close(showLoading);
            }));
        });
        this.toBlob = (id, inputOptions) => __awaiter(this, void 0, void 0, function* () {
            const options = __classPrivateFieldGet(this, _getOption).call(this, inputOptions);
            const doc = yield this.getJsPdf(id, options);
            return doc.output('blob');
        });
        this.toBuffer = (id, inputOptions) => __awaiter(this, void 0, void 0, function* () {
            const options = __classPrivateFieldGet(this, _getOption).call(this, inputOptions);
            const doc = yield this.getJsPdf(id, options);
            return doc.output('arraybuffer');
        });
        __classPrivateFieldSet(this, _doc, new jsPDF('p', 'pt', 'a4'));
        __classPrivateFieldGet(this, _addFont).call(this);
    }
}
_doc = new WeakMap(), _options = new WeakMap(), _getFileName = new WeakMap(), _getDoc = new WeakMap(), _getOption = new WeakMap(), _addFont = new WeakMap();
ZuiPrintService.decorators = [
    { type: Injectable }
];
ZuiPrintService.ctorParameters = () => [
    { type: ZuiLoadingService },
    { type: NgZone }
];
//# sourceMappingURL=index.service.js.map