"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const pdfdataextract_1 = require("pdfdataextract");
const config_1 = require("./config");
const lib_1 = require("./lib");
const timeUtils_1 = require("./lib/timeUtils");
const errors_1 = require("./lib/errors");
const generalUtils_1 = require("./lib/generalUtils");
const utils_1 = require("./utils");
class PDFparser {
    constructor(document) {
        this.getPDFMeta = () => __awaiter(this, void 0, void 0, function* () {
            if (!(this.document instanceof Buffer)) {
                throw new errors_1.AppError("Document is not Buffer");
            }
            if (!(0, generalUtils_1.isPDF)(this.document)) {
                throw new errors_1.AppError("Only pdf file type is supported");
            }
            try {
                const signaturesMeta = yield (0, lib_1.verifyPDF)(this.document);
                const pdfMeta = yield pdfdataextract_1.PdfData.extract(this.document, config_1.config);
                const result = {
                    pages: pdfMeta.pages,
                    title: pdfMeta.info.Title || "Unknown",
                    author: pdfMeta.info.Author || "Unknown",
                    creation_date: (0, timeUtils_1.formatPdfTime)(pdfMeta.info.CreationDate),
                    mod_date: (0, timeUtils_1.formatPdfTime)(pdfMeta.info.ModDate),
                };
                if (signaturesMeta) {
                    result["signatures"] = signaturesMeta.signatures;
                    result["expired"] = signaturesMeta.expired;
                }
                return result;
            }
            catch (error) {
                throw new errors_1.GeneralError(error);
            }
        });
        this.insertQrCode = (imgBytes, url, coords, scaleFactor, licenseKey, certPath, certTSAUrl) => __awaiter(this, void 0, void 0, function* () {
            let lib, buf;
            if (typeof window !== `undefined`) {
                lib = window.PDFNet;
                const CoreControls = window.CoreControls;
                CoreControls.setWorkerPath("webviewer/core");
            }
            else {
                const pdflib = yield Promise.resolve().then(() => __importStar(require("@pdftron/pdfnet-node")));
                lib = pdflib.PDFNet;
            }
            //will this work ??
            console.log(lib);
            try {
                yield lib.initialize(licenseKey);
            }
            catch (e) {
                console.log(e);
            }
            try {
                buf = yield (0, utils_1.TimestampAndEnableLTV)(this.document, certPath, certTSAUrl, imgBytes, coords, lib);
            }
            catch (error) {
                console.log(error);
                throw new errors_1.GeneralError("Could Not sign pdf");
            }
            yield lib.shutdown();
            return buf;
        });
        this.document = document;
    }
}
exports.default = PDFparser;