"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmtpProvider = void 0;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-server-foundation/type-metadata-runtime"));
const nodemailer_1 = require("nodemailer");
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":22,\"type\":{\"kind\":6}}]]", []);
let _SmtpProviderMetadata_1;
class SmtpProvider {
    config;
    logger;
    transporter;
    constructor(config, logger) {
        this.config = config;
        this.logger = logger;
        if (!this.config.SMTP_HOST) {
            throw new Error('SMTP_HOST is not set');
        }
        this.transporter = (0, nodemailer_1.createTransport)({
            host: this.config.SMTP_HOST,
            port: this.config.SMTP_PORT,
            secure: this.config.SMTP_TLS,
            auth: this.config.SMTP_USER
                ? {
                    user: this.config.SMTP_USER,
                    pass: this.config.SMTP_PASSWORD_SECRET
                }
                : undefined
        });
    }
    async send(message) {
        const response = await this.transporter.sendMail({
            from: message.from,
            to: message.to,
            subject: message.subject,
            html: message.message,
            text: message.plainMessage,
            replyTo: message.replyTo,
            attachments: message.attachments?.map(attachment => ({
                filename: attachment.name,
                content: attachment.content,
                contentType: attachment.contentType,
                cid: attachment.cid
            }))
        });
        return response.messageId;
    }
    static __tsfType = _SmtpProviderMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"SmtpProvider\",\"typeName\":\"SmtpProvider\",\"classType\":{\"$tsf\":0},\"properties\":[],\"methods\":[{\"name\":\"send\",\"parameters\":[{\"name\":\"message\",\"type\":{\"kind\":18,\"typeName\":\"PreparedMessageProperties\",\"types\":[{\"kind\":20,\"name\":\"subject\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"message\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"plainMessage\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"attachments\",\"type\":{\"kind\":12,\"types\":[{\"kind\":14,\"type\":{\"kind\":18,\"types\":[{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"content\",\"type\":{\"kind\":16,\"typeName\":\"Buffer\",\"classType\":{\"$tsf\":1}},\"optional\":false},{\"kind\":20,\"name\":\"contentType\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"cid\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}]}},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"to\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"from\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"replyTo\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}],\"implements\":[{\"kind\":18,\"typeName\":\"Omit\",\"utilityType\":\"Omit\",\"typeArguments\":[{\"kind\":18,\"typeName\":\"MessageProperties\",\"types\":[{\"kind\":20,\"name\":\"from\",\"type\":{\"kind\":12,\"types\":[{\"kind\":18,\"types\":[{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"address\",\"type\":{\"kind\":6},\"optional\":false}]},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"to\",\"type\":{\"kind\":18,\"types\":[{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"address\",\"type\":{\"kind\":6},\"optional\":false}]},\"optional\":false},{\"kind\":20,\"name\":\"replyTo\",\"type\":{\"kind\":12,\"types\":[{\"kind\":18,\"types\":[{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"address\",\"type\":{\"kind\":6},\"optional\":false}]},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"subject\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"message\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"plainMessage\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"attachments\",\"type\":{\"kind\":12,\"types\":[{\"kind\":14,\"type\":{\"kind\":18,\"types\":[{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"content\",\"type\":{\"kind\":16,\"typeName\":\"Buffer\",\"classType\":{\"$tsf\":1}},\"optional\":false},{\"kind\":20,\"name\":\"contentType\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"cid\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}]}},{\"kind\":4}]},\"optional\":true}]}],\"utilityKeys\":[\"to\",\"from\",\"replyTo\"],\"types\":[{\"kind\":20,\"name\":\"subject\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"message\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"plainMessage\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"attachments\",\"type\":{\"kind\":12,\"types\":[{\"kind\":14,\"type\":{\"kind\":18,\"types\":[{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"content\",\"type\":{\"kind\":16,\"typeName\":\"Buffer\",\"classType\":{\"$tsf\":1}},\"optional\":false},{\"kind\":20,\"name\":\"contentType\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"cid\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}]}},{\"kind\":4}]},\"optional\":true}]}]},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":0}}],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"config\",\"type\":{\"kind\":16,\"typeName\":\"BaseAppConfig\",\"classType\":{\"$tsfImport\":[2,\"../../app/config\",\"BaseAppConfig\"]}},\"optional\":false,\"default\":false},{\"name\":\"logger\",\"type\":{\"kind\":16,\"typeName\":\"ScopedLogger\",\"classType\":{\"$tsfImport\":[2,\"../logger\",\"ScopedLogger\"]}},\"optional\":false,\"default\":false}]}]", [() => SmtpProvider, () => (typeof Buffer !== "undefined" ? Buffer : (typeof exports !== "undefined" ? exports.Buffer : undefined)), require], __tsf_metadata_type);
}
exports.SmtpProvider = SmtpProvider;
_SmtpProviderMetadata_1.classType = SmtpProvider;
SmtpProvider.__tsfType = _SmtpProviderMetadata_1;
//# sourceMappingURL=smtp.js.map