import { ok } from "../is.js";
import { on, dispatchEvent } from "../events/index.js";
import { MAIL_FROM, MAIL_SENDER, MAIL_PASSWORD, MAIL_USERNAME, MAIL_MAILER, MAIL_URL, MAIL_HOST, MAIL_PORT, MAIL_SENDMAIL_PATH, MAIL_MAILGUN_API_KEY, MAIL_MAILGUN_DOMAIN, MAIL_ENCRYPTION, MAIL_REPLY_TO } from "../config/index.js";
export async function email(options) {
    const { schedule, ...emailOptions } = options;
    if (schedule) {
        const event = {
            type: EMAIL,
            email: emailOptions,
            schedule
        };
        return dispatchEvent(event);
    }
    const transport = await getNodemailerTransport();
    return transport.sendMail({
        from: MAIL_FROM,
        sender: MAIL_SENDER,
        replyTo: MAIL_REPLY_TO,
        ...emailOptions
    });
}
let transport;
export async function getNodemailerTransport() {
    if (transport)
        return transport;
    return transport = await getTransport();
    async function getTransport() {
        const nodemailer = await import("nodemailer");
        if (MAIL_MAILER === "mailgun") {
            const { default: mailgun } = await import("nodemailer-mailgun-transport");
            return nodemailer.createTransport(mailgun({
                auth: {
                    domain: MAIL_MAILGUN_DOMAIN || MAIL_USERNAME,
                    api_key: MAIL_MAILGUN_API_KEY || MAIL_PASSWORD
                }
            }));
        }
        if (MAIL_MAILER === "sendmail") {
            return nodemailer.createTransport({
                sendmail: true,
                path: MAIL_SENDMAIL_PATH
            });
        }
        ok(!MAIL_MAILER || MAIL_MAILER === "smtp", `Unsupported MAIL_MAILER "${MAIL_MAILER}"`);
        return nodemailer.createTransport({
            url: MAIL_URL,
            host: MAIL_HOST,
            port: MAIL_PORT ? +MAIL_PORT : undefined,
            auth: (MAIL_USERNAME || MAIL_PASSWORD) ? {
                user: MAIL_USERNAME,
                pass: MAIL_PASSWORD
            } : undefined,
            tls: {
                rejectUnauthorized: MAIL_ENCRYPTION === "tls"
            }
        });
    }
}
const EMAIL = "email";
export const removeEmailScheduledFunction = on(EMAIL, async (event) => {
    if (event.email) {
        await email(event.email);
    }
});
//# sourceMappingURL=email.js.map