"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MailService = exports.__ΩMailProvider = exports.__ΩPreparedMessageProperties = exports.__ΩTemplateMessageProperties = exports.__ΩMessageProperties = exports.MailTemplate = void 0;
const __ΩOmit = ['T', 'K', () => __ΩPick, () => __ΩExclude, 'Omit', 'b!b"e!!e!!ge!"o$#o##w%y'];
const __ΩPick = ['T', 'K', 'Pick', 'l+e#!e"!fRb!b"Pde""N#!w#y'];
const __ΩExclude = ['T', 'U', 'Exclude', 'l6!Re$!RPe#!e$"qk#%QRb!b"Pde"!p)w#y'];
/*@ts-ignore*/
var { __ΩClassType } = require('@deepkit/core');
/*@ts-ignore*/
var { __ΩScopedLogger } = require('@deepkit/logger');
const app_1 = require("../../app");
const helpers_1 = require("../../helpers");
const postmark_1 = require("./postmark");
const smtp_1 = require("./smtp");
const senderSuffix = process.env.APP_ENV !== 'production' ? ` (${process.env.APP_ENV}) ` : '';
const subjectPrefix = process.env.APP_ENV !== 'production' ? `[${process.env.APP_ENV}] ` : '';
class MailTemplate {
    constructor(data) {
        this.data = data;
    }
    generateText() {
        /** */
    }
}
exports.MailTemplate = MailTemplate;
MailTemplate.__type = ['T', 'data', 'constructor', 'subject', 'generateHtml', 'generateText', 'MailTemplate', 'b!Pe"!2"<"0#&3$=P&0%=PP&$J0&5w\''];
const __ΩMessageProperties = ['name', 'address', 'from', 'to', 'replyTo', 'subject', 'message', 'plainMessage', 'content', 'contentType', 'cid', 'attachments', 'MessageProperties', 'PP&4!8&4"M4#8P&4!8&4"M4$P&4!8&4"M4%8&4&&4\'&4(8P&4!!4)&4*&4+8MF4,8Mw-y'];
exports.__ΩMessageProperties = __ΩMessageProperties;
const __ΩTemplateMessageProperties = ['T', () => __ΩOmit, () => __ΩMessageProperties, "subject", "message", "plainMessage", () => __ΩClassType, () => MailTemplate, 'template', 'data', 'TemplateMessageProperties', 'b!Pn#P.$.%.&Jo"#Pe#!7(o\'"4)e"!4*Mw+y'];
exports.__ΩTemplateMessageProperties = __ΩTemplateMessageProperties;
const __ΩPreparedMessageProperties = [() => __ΩOmit, () => __ΩMessageProperties, "to", "from", "replyTo", 'to', 'from', 'replyTo', 'PreparedMessageProperties', 'Pn"P.#.$.%Jo!#&4&&4\'&4(8Mw)y'];
exports.__ΩPreparedMessageProperties = __ΩPreparedMessageProperties;
const __ΩMailProvider = [() => __ΩPreparedMessageProperties, 'message', 'send', 'MailProvider', 'PPn!2"&`1#Mw$y'];
exports.__ΩMailProvider = __ΩMailProvider;
class MailService {
    constructor(config, logger) {
        this.config = config;
        this.logger = logger;
        if (this.config.MAIL_PROVIDER === 'postmark') {
            this.provider = new postmark_1.PostmarkProvider(this.config, logger);
        }
        else if (this.config.MAIL_PROVIDER === 'smtp') {
            this.provider = new smtp_1.SmtpProvider(this.config, logger);
        }
        else {
            throw new Error('Unsupported mail provider');
        }
    }
    async send(params) {
        const preparedParams = this.prepare(params);
        await this.sendPrepared(preparedParams);
    }
    async sendFromTemplate(params) {
        const preparedParams = this.prepareFromTemplate(params);
        await this.sendPrepared(preparedParams);
    }
    prepare(params) {
        const fromName = params.from?.name || this.config.MAIL_FROM_NAME;
        const fromAddressPrefix = fromName ? `"${fromName}${senderSuffix}" ` : '';
        const fromAddress = params.from?.address || this.config.MAIL_FROM;
        const replyToPrefix = params.replyTo?.name ? `"${params.replyTo.name}" ` : '';
        const toAddressPrefix = params.to.name ? `"${params.to.name}" ` : '';
        return {
            ...params,
            from: `${fromAddressPrefix}<${fromAddress}>`,
            to: `${toAddressPrefix}<${params.to.address}>`,
            replyTo: params.replyTo ? `${replyToPrefix}<${params.replyTo.address}>` : undefined,
            subject: `${subjectPrefix}${params.subject}`
        };
    }
    prepareFromTemplate(params) {
        const tpl = new params.template(params.data);
        const message = tpl.generateHtml();
        const plainMessage = tpl.generateText() ?? undefined;
        return this.prepare({ ...params, message, plainMessage, subject: tpl.subject });
    }
    async sendPrepared(message) {
        try {
            const messageId = await this.provider.send(message);
            this.logger.info('Email sent', { to: message.to, subject: message.subject, messageId });
        }
        catch (err) {
            this.logger.error('Failed to send email', err, { to: message.to, subject: message.subject });
            throw (0, helpers_1.toError)('Failed to send email', err);
        }
    }
}
exports.MailService = MailService;
MailService.__type = [() => __ΩMailProvider, 'provider', () => app_1.BaseAppConfig, 'config', () => __ΩScopedLogger, 'logger', 'constructor', () => __ΩMessageProperties, 'params', 'send', () => __ΩTemplateMessageProperties, 'sendFromTemplate', () => __ΩMessageProperties, () => __ΩPreparedMessageProperties, 'prepare', () => __ΩTemplateMessageProperties, () => __ΩPreparedMessageProperties, 'prepareFromTemplate', () => __ΩPreparedMessageProperties, 'message', 'sendPrepared', 'MailService', 'n!3"PP7#2$;n%2&;"0\'Pn(2)"0*P"o+"2)"0,Pn-2)n.0/P"o0"2)n102Pn324"055w6'];
//# sourceMappingURL=index.js.map