"use strict";
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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendInviteMail = void 0;
const logger_1 = __importDefault(require("../logger"));
const nodemailer_1 = __importDefault(require("nodemailer"));
const liquidjs_1 = require("liquidjs");
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const util_1 = require("util");
const readFile = util_1.promisify(fs_1.default.readFile);
const liquidEngine = new liquidjs_1.Liquid({
    root: path_1.default.resolve(__dirname, 'templates'),
    extname: '.liquid',
});
if (!process.env.EMAIL_TRANSPORT) {
    logger_1.default.warn(`[Email] No email transport is configured. Using default: sendmail.`);
}
const emailTransport = process.env.EMAIL_TRANSPORT || 'sendmail';
let transporter;
if (emailTransport === 'sendmail') {
    transporter = nodemailer_1.default.createTransport({
        sendmail: true,
        newline: process.env.EMAIL_SENDMAIL_NEW_LINE || 'unix',
        path: process.env.EMAIL_SENDMAIL_PATH || '/usr/sbin/sendmail',
    });
}
else if (emailTransport.toLowerCase() === 'smtp') {
    transporter = nodemailer_1.default.createTransport({
        pool: process.env.EMAIL_SMTP_POOL === 'true',
        host: process.env.EMAIL_SMTP_HOST,
        port: Number(process.env.EMAIL_SMTP_PORT),
        secure: process.env.EMAIL_SMTP_SECURE === 'true',
        auth: {
            user: process.env.EMAIL_SMTP_USER,
            pass: process.env.EMAIL_SMTP_PASSWORD,
        },
    });
}
function sendMail(options) {
    return __awaiter(this, void 0, void 0, function* () {
        const templateString = yield readFile(path_1.default.join(__dirname, 'templates/base.liquid'), 'utf8');
        const html = yield liquidEngine.parseAndRender(templateString, { html: options.html });
        options.from = options.from || process.env.EMAIL_FROM;
        try {
            yield transporter.sendMail(Object.assign(Object.assign({}, options), { html: html }));
        }
        catch (error) {
            logger_1.default.warn('[Email] Unexpected error while sending an email:');
            logger_1.default.warn(error);
        }
    });
}
exports.default = sendMail;
function sendInviteMail(email, url) {
    return __awaiter(this, void 0, void 0, function* () {
        /**
         * @TODO pull this from directus_settings
         */
        const projectName = 'directus';
        const html = yield liquidEngine.renderFile('user-invitation', { email, url, projectName });
        yield transporter.sendMail({ from: process.env.EMAIL_FROM, to: email, html: html });
    });
}
exports.sendInviteMail = sendInviteMail;
