"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) => {
                    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
                    try {
                        const profileIdSetting = await prisma.setting.findUnique({
                            where: { slug: 'mail-profile-id' },
                            select: { value: true },
                        });
                        const profileId = Number(profileIdSetting === null || profileIdSetting === void 0 ? void 0 : profileIdSetting.value);
                        if (!profileId || profileId <= 0) {
                            return { global: true, type: 'SMTP', host: 'localhost', port: 1025, secure: false, username: '', password: '' };
                        }
                        const profile = await prisma.mail_profile.findUnique({
                            where: { id: profileId },
                        });
                        if (!profile) {
                            return { global: true, type: 'SMTP', host: 'localhost', port: 1025, secure: false, username: '', password: '' };
                        }
                        const base = { global: true };
                        switch (profile.provider) {
                            case 'GMAIL':
                                return Object.assign(base, {
                                    type: 'GMAIL',
                                    clientId: (_a = profile.gmail_client_id) !== null && _a !== void 0 ? _a : '',
                                    clientSecret: (_b = profile.gmail_client_secret) !== null && _b !== void 0 ? _b : '',
                                    refreshToken: (_c = profile.gmail_refresh_token) !== null && _c !== void 0 ? _c : '',
                                    from: profile.from_email,
                                });
                            case 'SMTP':
                                return Object.assign(base, {
                                    type: 'SMTP',
                                    host: (_d = profile.smtp_host) !== null && _d !== void 0 ? _d : 'localhost',
                                    port: (_e = profile.smtp_port) !== null && _e !== void 0 ? _e : 1025,
                                    secure: (_f = profile.smtp_secure) !== null && _f !== void 0 ? _f : false,
                                    username: (_g = profile.smtp_username) !== null && _g !== void 0 ? _g : '',
                                    password: (_h = profile.smtp_password) !== null && _h !== void 0 ? _h : '',
                                });
                            case 'SES':
                                return Object.assign(base, {
                                    type: 'SES',
                                    region: (_j = profile.ses_region) !== null && _j !== void 0 ? _j : '',
                                    accessKeyId: (_k = profile.ses_access_key_id) !== null && _k !== void 0 ? _k : '',
                                    secretAccessKey: (_l = profile.ses_secret_access_key) !== null && _l !== void 0 ? _l : '',
                                    from: profile.from_email,
                                });
                            default:
                                return { global: true, type: 'SMTP', host: 'localhost', port: 1025, secure: false, username: '', password: '' };
                        }
                    }
                    catch (error) {
                        console.error('Error loading mail settings:', error);
                        return { global: true, type: 'SMTP', host: 'localhost', port: 1025, secure: false, username: '', password: '' };
                    }
                }
            })),
            (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