"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());
    });
};
const { sendSmtpEmail } = require("../../utilities/workerRequests");
const automationUtils = require("../automationUtils");
exports.definition = {
    description: "Send an email using SMTP",
    tagline: "Send SMTP email to {{inputs.to}}",
    icon: "Email",
    name: "Send Email (SMTP)",
    type: "ACTION",
    internal: true,
    stepId: "SEND_EMAIL_SMTP",
    inputs: {},
    schema: {
        inputs: {
            properties: {
                to: {
                    type: "string",
                    title: "Send To",
                },
                from: {
                    type: "string",
                    title: "Send From",
                },
                subject: {
                    type: "string",
                    title: "Email Subject",
                },
                contents: {
                    type: "string",
                    title: "HTML Contents",
                },
            },
            required: ["to", "from", "subject", "contents"],
        },
        outputs: {
            properties: {
                success: {
                    type: "boolean",
                    description: "Whether the email was sent",
                },
                response: {
                    type: "object",
                    description: "A response from the email client, this may be an error",
                },
            },
            required: ["success"],
        },
    },
};
exports.run = function ({ inputs }) {
    return __awaiter(this, void 0, void 0, function* () {
        let { to, from, subject, contents } = inputs;
        if (!contents) {
            contents = "<h1>No content</h1>";
        }
        to = to || undefined;
        try {
            let response = yield sendSmtpEmail(to, from, subject, contents, true);
            return {
                success: true,
                response,
            };
        }
        catch (err) {
            return {
                success: false,
                response: automationUtils.getError(err),
            };
        }
    });
};
