"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const NodeMailer = require("nodemailer");
const utils_1 = require("./utils");
const settings_1 = require("./settings");
class Mailer {
    constructor(tag = "tag") {
        this.mailTransport = null;
        this.status = Mailer.STATUS.READY;
        this.config = settings_1.default.Instance.getMailer(tag);
    }
    connect() {
        this.mailTransport = NodeMailer.createTransport(this.config.mailTransportConfig);
        this.status = Mailer.STATUS.CONNECTED;
    }
    sendMail(mailOptions, callback) {
        if (this.mailTransport == null) {
            this.connect();
        }
        ;
        let self = this;
        let interceptedCallback = function (err, info) {
            //mark that we have an error when we attempted to send an email and failed!
            if (err != null) {
                self.status = Mailer.STATUS.ERROR;
            }
            callback(err, info);
        };
        if (this.mailTransport != null) {
            let options = utils_1.default.deepExtend({}, this.config.mailConfig, mailOptions);
            this.mailTransport.sendMail(options, interceptedCallback);
        }
        this.destroy();
    }
    destroy() {
        // clearInterval(this.interval);
        if (this.mailTransport != null) {
            this.mailTransport.close();
            this.mailTransport = null;
        }
        this.status = Mailer.STATUS.DISCONNECTED;
    }
    getStatus() {
        return this.status.toString();
    }
    isFunctional() {
        return Mailer.STATUS.ERROR != this.status;
    }
}
Mailer.STATUS = {
    READY: "ready",
    CONNECTED: "connected",
    DISCONNECTED: "disconnected",
    ERROR: "error"
};
exports.default = Mailer;
//# sourceMappingURL=mailer.js.map