"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostmarkProvider = void 0;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-reflection/type-metadata-runtime"));
const postmark_1 = require("postmark");
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":22,\"type\":{\"kind\":6}}]]", []);
let _PostmarkProviderMetadata_1;
class PostmarkProvider {
    config;
    logger;
    postmarkClient;
    constructor(config, logger) {
        this.config = config;
        this.logger = logger;
        if (!this.config.POSTMARK_SECRET) {
            throw new Error('POSTMARK_SECRET is not set');
        }
        this.postmarkClient = new postmark_1.ServerClient(this.config.POSTMARK_SECRET);
    }
    async send(message) {
        const response = await this.postmarkClient.sendEmail({
            From: message.from,
            To: message.to,
            Subject: message.subject,
            HtmlBody: message.message,
            TextBody: message.plainMessage,
            ReplyTo: message.replyTo,
            Headers: this.priorityHeaders(message.priority),
            Attachments: message.attachments?.map(attachment => ({
                Name: attachment.name,
                Content: attachment.content.toString('base64'),
                ContentType: attachment.contentType,
                ContentID: attachment.cid ?? null,
                ContentLength: attachment.content.length
            }))
        });
        return response.MessageID;
    }
    priorityHeaders(priority) {
        if (!priority || priority === 'normal')
            return undefined;
        const high = priority === 'high';
        return [
            { Name: 'Importance', Value: high ? 'High' : 'Low' },
            { Name: 'X-Priority', Value: high ? '1' : '5' },
            { Name: 'X-MSMail-Priority', Value: high ? 'High' : 'Low' }
        ];
    }
    static __tsfType = _PostmarkProviderMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"PostmarkProvider\",\"typeName\":\"PostmarkProvider\",\"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\":\"priority\",\"type\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"high\"},{\"kind\":10,\"literal\":\"normal\"},{\"kind\":10,\"literal\":\"low\"}],\"typeName\":\"MailPriority\"},{\"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\":\"priority\",\"type\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"high\"},{\"kind\":10,\"literal\":\"normal\"},{\"kind\":10,\"literal\":\"low\"}],\"typeName\":\"MailPriority\"},{\"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\":\"priority\",\"type\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"high\"},{\"kind\":10,\"literal\":\"normal\"},{\"kind\":10,\"literal\":\"low\"}],\"typeName\":\"MailPriority\"},{\"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}]}]", [() => PostmarkProvider, () => (typeof Buffer !== "undefined" ? Buffer : (typeof exports !== "undefined" ? exports.Buffer : undefined)), require], __tsf_metadata_type);
}
exports.PostmarkProvider = PostmarkProvider;
_PostmarkProviderMetadata_1.classType = PostmarkProvider;
PostmarkProvider.__tsfType = _PostmarkProviderMetadata_1;
//# sourceMappingURL=postmark.js.map