"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MailModule = void 0;
const api_locale_1 = require("@hed-hog/api-locale");
const api_mail_1 = require("@hed-hog/api-mail");
const api_pagination_1 = require("@hed-hog/api-pagination");
const api_prisma_1 = require("@hed-hog/api-prisma");
const common_1 = require("@nestjs/common");
const mail_sent_module_1 = require("../mail-sent/mail-sent.module");
const setting_module_1 = require("../setting/setting.module");
const mail_controller_1 = require("./mail.controller");
const mail_service_1 = require("./mail.service");
let MailModule = class MailModule {
};
exports.MailModule = MailModule;
exports.MailModule = MailModule = __decorate([
    (0, common_1.Module)({
        imports: [
            (0, common_1.forwardRef)(() => api_mail_1.MailModule.forRootAsync({
                imports: [api_prisma_1.PrismaModule],
                inject: [api_prisma_1.PrismaService],
                useFactory: async (prisma) => {
                    try {
                        const mailSettings = await prisma.setting.findMany({
                            where: {
                                slug: {
                                    in: [
                                        'mail-provider',
                                        'mail-from',
                                        'mail-gmail-client-id',
                                        'mail-gmail-client-secret',
                                        'mail-gmail-refresh-token',
                                        'mail-smtp-host',
                                        'mail-smtp-port',
                                        'mail-smtp-secure',
                                        'mail-client-secret',
                                        'mail-client-secret',
                                        'mail-aws-access-key-id',
                                        'mail-aws-secret-access-key',
                                        'mail-aws-region'
                                    ]
                                }
                            },
                            select: {
                                slug: true,
                                value: true,
                            }
                        });
                        const settings = new Map();
                        for (const setting of mailSettings) {
                            settings.set(setting.slug, setting.value);
                        }
                        const config = {
                            global: true,
                        };
                        switch (settings.get('mail-provider')) {
                            case 'GMAIL':
                                Object.assign(config, {
                                    type: 'GMAIL',
                                    clientId: settings.get('mail-gmail-client-id'),
                                    clientSecret: settings.get('mail-gmail-client-secret'),
                                    refreshToken: settings.get('mail-gmail-refresh-token'),
                                    from: settings.get('mail-from'),
                                });
                                break;
                            case 'SMTP':
                                Object.assign(config, {
                                    type: 'SMTP',
                                    host: settings.get('mail-smtp-host'),
                                    port: Number(settings.get('mail-smtp-port')),
                                    secure: settings.get('mail-smtp-secure') === 'true',
                                    username: settings.get('mail-from'),
                                    password: settings.get('mail-client-secret'),
                                });
                                break;
                            case 'SES':
                                Object.assign(config, {
                                    type: 'SES',
                                    region: settings.get('mail-aws-region'),
                                    accessKeyId: settings.get('mail-aws-access-key-id'),
                                    secretAccessKey: settings.get('mail-aws-secret-access-key'),
                                    from: settings.get('mail-from'),
                                });
                                break;
                            default:
                                throw new common_1.BadRequestException(`Invalid mail provider: ${settings.get('mail-provider')}`);
                        }
                        return config;
                    }
                    catch (error) {
                        console.error('Error loading mail settings:', error);
                        throw error;
                    }
                }
            })),
            (0, common_1.forwardRef)(() => mail_sent_module_1.MailSentModule),
            (0, common_1.forwardRef)(() => api_prisma_1.PrismaModule),
            (0, common_1.forwardRef)(() => api_pagination_1.PaginationModule),
            (0, common_1.forwardRef)(() => api_locale_1.LocaleModule),
            (0, common_1.forwardRef)(() => setting_module_1.SettingModule),
        ],
        controllers: [mail_controller_1.MailController],
        providers: [mail_service_1.MailService],
        exports: [mail_service_1.MailService],
    })
], MailModule);
//# sourceMappingURL=mail.module.js.map