var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import ComponentCode from "../ComponentCode";
import BadDataException from "../../../../Types/Exception/BadDataException";
import ComponentID from "../../../../Types/Workflow/ComponentID";
import Components from "../../../../Types/Workflow/Components/Email";
import nodemailer from "nodemailer";
import CaptureSpan from "../../../Utils/Telemetry/CaptureSpan";
export default class Email extends ComponentCode {
    constructor() {
        super();
        const Component = Components.find((i) => {
            return i.id === ComponentID.SendEmail;
        });
        if (!Component) {
            throw new BadDataException("Component not found.");
        }
        this.setMetadata(Component);
    }
    async run(args, options) {
        var _a, _b, _c, _d, _e;
        const successPort = this.getMetadata().outPorts.find((p) => {
            return p.id === "success";
        });
        if (!successPort) {
            throw options.onError(new BadDataException("Success port not found"));
        }
        const errorPort = this.getMetadata().outPorts.find((p) => {
            return p.id === "error";
        });
        if (!errorPort) {
            throw options.onError(new BadDataException("Error port not found"));
        }
        if (!args["to"]) {
            throw options.onError(new BadDataException("To Email not found"));
        }
        if (args["to"] && typeof args["to"] !== "string") {
            throw options.onError(new BadDataException("To Email is not type of string"));
        }
        if (!args["from"]) {
            throw options.onError(new BadDataException("From Email not found"));
        }
        if (args["from"] && typeof args["from"] !== "string") {
            throw options.onError(new BadDataException("From Email is not type of string"));
        }
        if (args["smtp-username"] && typeof args["smtp-username"] !== "string") {
            throw options.onError(new BadDataException("SMTP Username is not type of string"));
        }
        if (args["smtp-password"] && typeof args["smtp-password"] !== "string") {
            throw options.onError(new BadDataException("SMTP Password is not type of string"));
        }
        if (!args["smtp-host"]) {
            throw options.onError(new BadDataException("SMTP Host not found"));
        }
        if (args["smtp-host"] && typeof args["smtp-host"] !== "string") {
            throw options.onError(new BadDataException("SMTP Host is not type of string"));
        }
        if (!args["smtp-port"]) {
            throw options.onError(new BadDataException("SMTP Port not found"));
        }
        if (args["smtp-port"] && typeof args["smtp-port"] === "string") {
            args["smtp-port"] = parseInt(args["smtp-port"]);
        }
        if (args["smtp-port"] && typeof args["smtp-port"] !== "number") {
            throw options.onError(new BadDataException("SMTP Port is not type of number"));
        }
        try {
            const username = ((_a = args["smtp-username"]) === null || _a === void 0 ? void 0 : _a.toString()) || undefined;
            const password = ((_b = args["smtp-password"]) === null || _b === void 0 ? void 0 : _b.toString()) || undefined;
            const smtpTransport = {
                host: (_c = args["smtp-host"]) === null || _c === void 0 ? void 0 : _c.toString(),
                port: args["smtp-port"],
            };
            if (args["secure"] === true ||
                args["secure"] === "true" ||
                args["secure"] === 1) {
                smtpTransport.secure = true;
            }
            else {
                smtpTransport.secure = false;
            }
            if (username && password) {
                smtpTransport.auth = {
                    user: username,
                    pass: password,
                };
            }
            const mailer = nodemailer.createTransport(smtpTransport);
            await mailer.sendMail({
                from: args["from"].toString(),
                to: args["to"].toString(),
                subject: ((_d = args["subject"]) === null || _d === void 0 ? void 0 : _d.toString()) || "",
                html: ((_e = args["email-body"]) === null || _e === void 0 ? void 0 : _e.toString()) || "",
            });
            options.log("Email sent.");
            return Promise.resolve({
                returnValues: {},
                executePort: successPort,
            });
        }
        catch (err) {
            options.log(err);
            return Promise.resolve({
                returnValues: {},
                executePort: errorPort,
            });
        }
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", Promise)
], Email.prototype, "run", null);
//# sourceMappingURL=Email.js.map