"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createReport = void 0;
const utils_1 = require("./utils");
const bluebird_1 = require("bluebird");
const pdf = bluebird_1.Promise.promisifyAll(require("html-pdf"));
const createPDF = (html) => {
    var options = {
        format: "A4",
        base: "file:///" + __dirname + "/",
        localUrlAccess: true, // set this to true to enable local file access
    };
    return new bluebird_1.Promise((resolve, reject) => {
        pdf.create(html, options).toBuffer((err, buffer) => {
            if (err !== null) {
                reject(err);
            }
            else {
                resolve(buffer);
            }
        });
    });
};
const createReport = async (_data, type) => {
    let content;
    //Get the template corresponding to report
    try {
        content = (0, utils_1.getContent)(type);
        content = (0, utils_1.replaceCompetenciesStrWeak)(_data.params.competencies, content);
        content = (0, utils_1.replacePersonality)(_data.params.personality, content);
        let personalitySectionOffset = 0;
        const averageHeight = 1150; //Estimated height for personality card page
        personalitySectionOffset =
            Math.ceil(_data.params.personality.length / 5) * averageHeight;
        let nextPosition = 2000 + personalitySectionOffset;
        if (_data.params.waysToDevelop) {
            content = (0, utils_1.replaceWaysToDevelop)(_data.params.waysToDevelop, nextPosition, content);
        }
        const wtdQty = _data.params.waysToDevelop.length;
        const wtdPagesQty = Math.ceil(wtdQty / 2);
        let practicalTipsOffset = -1;
        if (wtdPagesQty < 5) {
            practicalTipsOffset = 136.66 - wtdPagesQty * 27.66;
        }
        if (wtdPagesQty > 5) {
            practicalTipsOffset = 136 - wtdPagesQty * 27.33;
        }
        nextPosition =
            averageHeight * wtdPagesQty + nextPosition + practicalTipsOffset;
        content = (0, utils_1.replacePracticalTips)(content, nextPosition);
        nextPosition = nextPosition + 1000;
        content = (0, utils_1.replaceYourIndividualDevPlan)(content, nextPosition);
        nextPosition = nextPosition + 1245;
        content = (0, utils_1.replaceFooter)(content, nextPosition);
        const assets = (0, utils_1.getAssets)(type);
        content = (0, utils_1.replaceAssesmentData)([..._data.params.header, ...assets], content);
        const buffer = await createPDF(content);
        return buffer;
    }
    catch (e) {
        console.error(e);
        throw Error();
    }
};
exports.createReport = createReport;
//# sourceMappingURL=index.js.map