"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, _m, _o, _p;
                    try {
                        const profileSettings = await prisma.setting.findMany({
                            where: {
                                slug: {
                                    in: ['mail-integration-profile-id'],
                                },
                            },
                            select: { slug: true, value: true },
                        });
                        const profileSettingsMap = new Map(profileSettings.map((item) => [item.slug, item.value]));
                        const profileId = Number(profileSettingsMap.get('mail-integration-profile-id'));
                        if (!profileId || profileId <= 0) {
                            return { global: true, type: 'SMTP', host: 'localhost', port: 1025, secure: false, username: '', password: '' };
                        }
                        const profile = await prisma.integration_profile.findUnique({
                            where: { id: profileId },
                            include: {
                                integration_provider: { select: { slug: true } },
                                integration_type: { select: { slug: true } },
                            },
                        });
                        if (!profile) {
                            return { global: true, type: 'SMTP', host: 'localhost', port: 1025, secure: false, username: '', password: '' };
                        }
                        if (((_a = profile.integration_type) === null || _a === void 0 ? void 0 : _a.slug) !== 'email') {
                            return { global: true, type: 'SMTP', host: 'localhost', port: 1025, secure: false, username: '', password: '' };
                        }
                        const cfg = (_b = profile.config) !== null && _b !== void 0 ? _b : {};
                        const base = { global: true };
                        switch (profile.integration_provider.slug) {
                            case 'gmail':
                                return Object.assign(base, {
                                    type: 'GMAIL',
                                    clientId: String((_c = cfg.client_id) !== null && _c !== void 0 ? _c : ''),
                                    clientSecret: String((_d = cfg.client_secret) !== null && _d !== void 0 ? _d : ''),
                                    refreshToken: String((_e = cfg.refresh_token) !== null && _e !== void 0 ? _e : ''),
                                    from: String((_f = cfg.from_email) !== null && _f !== void 0 ? _f : ''),
                                });
                            case 'smtp':
                                return Object.assign(base, {
                                    type: 'SMTP',
                                    host: String((_g = cfg.host) !== null && _g !== void 0 ? _g : 'localhost'),
                                    port: Number((_h = cfg.port) !== null && _h !== void 0 ? _h : 1025),
                                    secure: Boolean(cfg.secure),
                                    username: String((_j = cfg.username) !== null && _j !== void 0 ? _j : ''),
                                    password: String((_k = cfg.password) !== null && _k !== void 0 ? _k : ''),
                                });
                            case 'ses':
                                return Object.assign(base, {
                                    type: 'SES',
                                    region: String((_l = cfg.region) !== null && _l !== void 0 ? _l : ''),
                                    accessKeyId: String((_m = cfg.access_key_id) !== null && _m !== void 0 ? _m : ''),
                                    secretAccessKey: String((_o = cfg.secret_access_key) !== null && _o !== void 0 ? _o : ''),
                                    from: String((_p = cfg.from_email) !== null && _p !== void 0 ? _p : ''),
                                });
                            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