"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.render = exports.configureTransporter = void 0;
const lodash_1 = __importDefault(require("lodash"));
const deep_extend_1 = __importDefault(require("deep-extend"));
const graceful_fs_1 = require("graceful-fs");
const moment_1 = __importDefault(require("moment"));
const nodemailer_html_to_text_1 = require("nodemailer-html-to-text");
const config_1 = require("../config");
const logger_1 = __importDefault(require("../logger"));
const urlBuilder_1 = require("../utils/urlBuilder");
const templateBasePath = __dirname + '/../mailTemplates';
const snippetsBasePath = templateBasePath + '/snippets';
const templateFunctions = {};
function renderSnippet(name, data) {
    const snippetFile = snippetsBasePath + '/' + name + '.html';
    return lodash_1.default.template(graceful_fs_1.readFileSync(snippetFile).toString())(deep_extend_1.default({}, this, // parent data
    data, templateFunctions));
}
function snippet(name) {
    return function (data) {
        return renderSnippet.bind(this)(name, data);
    };
}
function renderLink(href, text) {
    // noinspection HtmlUnknownTarget
    return lodash_1.default.template('<a href="<%- href %>#" style="color: #E5287A;"><%- text %></a>')({
        href: href,
        text: text || href
    });
}
function renderHR() {
    return '<hr style="border-top: 1px solid #333333; border-left: 0; border-right: 0; border-bottom: 0;" />';
}
function formatDateTime(unix) {
    return moment_1.default.unix(unix).locale('de').local().format('DD.MM.YYYY HH:mm');
}
function formatFromNow(unix) {
    return moment_1.default.unix(unix).locale('de').fromNow();
}
templateFunctions.header = snippet('header');
templateFunctions.footer = snippet('footer');
templateFunctions.monitoringFooter = snippet('monitoring-footer');
templateFunctions.snippet = renderSnippet;
templateFunctions.link = renderLink;
templateFunctions.hr = renderHR;
templateFunctions.formatDateTime = formatDateTime;
templateFunctions.formatFromNow = formatFromNow;
function configureTransporter(transporter) {
    transporter.use('compile', nodemailer_html_to_text_1.htmlToText({
        tables: ['.table']
    }));
}
exports.configureTransporter = configureTransporter;
async function render(mailOptions) {
    const templatePathPrefix = templateBasePath + '/' + mailOptions.email;
    const subject = await graceful_fs_1.promises.readFile(templatePathPrefix + '.subject.txt');
    const body = await graceful_fs_1.promises.readFile(templatePathPrefix + '.body.html');
    const data = deep_extend_1.default({}, mailOptions.data, {
        community: config_1.config.client.community,
        editNodeUrl: urlBuilder_1.editNodeUrl()
    }, templateFunctions);
    try {
        return {
            subject: lodash_1.default.trim(lodash_1.default.template(subject.toString())(data)),
            body: lodash_1.default.template(body.toString())(data)
        };
    }
    catch (error) {
        logger_1.default
            .tag('mail', 'template')
            .error('Error rendering template for mail[' + mailOptions.id + ']:', error);
        throw error;
    }
}
exports.render = render;
//# sourceMappingURL=mailTemplateService.js.map