"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") {
        // private tag: string;
        this.mailTransport = null;
        // protected status: string;
        this.waitForStatus = false;
        this.config = settings_1.default.Instance.getMailer(tag);
        // let statusInterval =  this.config.statusCheckInterval;
        // if(statusInterval == null || typeof statusInterval != "number" || statusInterval < 1){
        //     statusInterval = 5000;
        // }
        // this.status = "";        
        // this.tag = tag;
        //this.connect();
        // this.statusCheck();
        // this.interval = setInterval(this.statusCheck.bind(this), statusInterval);
    }
    connect() {
        this.mailTransport = NodeMailer.createTransport(this.config.mailTransportConfig);
    }
    statusCheck() {
        // if (this.waitForStatus) return;
        // if(this.mailTransport == null){
        //     this.status = "disconnected";
        //     return;
        // }
        // this.waitForStatus = true;
        // let self = this;
        // this.mailTransport.verify(function(err: Error | null, success: boolean): void {
        //     self.waitForStatus = false;
        //     if (err) {
        //         self.status = "error";
        //     } else {
        //         self.status = "connected";
        //     }
        //  });
    }
    sendMail(mailOptions, callback) {
        if (this.mailTransport == null) {
            this.connect();
        }
        if (this.mailTransport != null) {
            let options = utils_1.default.deepExtend({}, this.config.mailConfig, mailOptions);
            this.mailTransport.sendMail(options, callback);
        }
        this.destroy();
    }
    destroy() {
        // clearInterval(this.interval);
        if (this.mailTransport != null) {
            this.mailTransport.close();
            this.mailTransport = null;
        }
    }
    getStatus() {
        return (this.mailTransport == null) ? 'connected' : 'discconnected';
    }
    isFunctional() {
        return this.getStatus() == "connected";
    }
}
exports.default = Mailer;
//# sourceMappingURL=mailer.js.map