import mail from '@adonisjs/mail/services/main';
export class AKEmailService {
    config;
    static eventTypeHeaderKey = 'ak_event_type';
    static edgeDriveKey = 'akago_emails';
    constructor(config) {
        this.config = config;
    }
    static templatePath(templateName) {
        return `${AKEmailService.edgeDriveKey}::${templateName ?? 'default'}`;
    }
    async send(templateBasePath, recipientEmail, subject, data, eventType) {
        if (this.isMailRecipientAllowed(recipientEmail))
            try {
                await mail.send(async (message) => {
                    message
                        .to(recipientEmail)
                        .subject(subject)
                        .header(AKEmailService.eventTypeHeaderKey, eventType)
                        .htmlView(`${templateBasePath}_html.edge`, data)
                        .textView(`${templateBasePath}_text.edge`, data);
                });
            }
            catch (e) {
                if (e.code === 'EENVELOPE')
                    console.error(e.response);
                else
                    throw e;
            }
    }
    isMailRecipientAllowed(email) {
        if (this.isInList(email, this.config.whitelist ?? []))
            return true;
        if (this.isInList(email, this.config.blacklist ?? []))
            return false;
        return true;
    }
    isInList(email, list) {
        const [_name, domain] = email.split('@');
        return list.some((item) => (item.includes('@') && email === item) || item === domain);
    }
}
//# sourceMappingURL=email_service.js.map