"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.SessionModule = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const session_entity_1 = require("./entities/session.entity");
const session_manager_service_1 = require("./services/session-manager.service");
const typeorm_session_repository_1 = require("./repositories/typeorm-session.repository");
const memory_session_repository_1 = require("./repositories/memory-session.repository");
const core_module_1 = require("../core/core.module");
const session_options_interface_1 = require("../core/interfaces/session-options.interface");
const auth_config_service_1 = require("../core/services/auth-config.service");
let SessionModule = class SessionModule {
    static createRedisRepository(redisUrl) {
        let RedisSessionRepository;
        let ioredisModule;
        let redisClient;
        try {
            RedisSessionRepository = require('./repositories/redis-session.repository').RedisSessionRepository;
            ioredisModule = require('@nestjs-modules/ioredis');
            const Redis = require('ioredis').default || require('ioredis');
            if (redisUrl) {
                redisClient = new Redis(redisUrl);
            }
            else {
                redisClient = new Redis();
            }
            return new RedisSessionRepository(redisClient);
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            if (errorMessage.includes('Cannot find module')) {
                throw new Error('❌ Redis session storage is configured but packages are not installed.\n' +
                    '   Install them with: npm install ioredis @nestjs-modules/ioredis\n' +
                    '   Or change session.storageType to SessionStorageType.DATABASE');
            }
            throw new Error(`❌ Failed to initialize Redis session storage: ${errorMessage}\n` +
                '   Make sure Redis server is running and accessible.');
        }
    }
};
exports.SessionModule = SessionModule;
exports.SessionModule = SessionModule = __decorate([
    (0, common_1.Module)({
        imports: [
            typeorm_1.TypeOrmModule.forFeature([session_entity_1.NestAuthSession]),
            (0, common_1.forwardRef)(() => core_module_1.CoreModule),
        ],
        providers: [
            typeorm_session_repository_1.TypeORMSessionRepository,
            memory_session_repository_1.MemorySessionRepository,
            {
                provide: session_manager_service_1.SESSION_REPOSITORY,
                useFactory: (typeormRepo) => {
                    const config = auth_config_service_1.AuthConfigService.getOptions();
                    const storageType = config.session?.storageType || session_options_interface_1.SessionStorageType.DATABASE;
                    if (storageType === session_options_interface_1.SessionStorageType.REDIS) {
                        return SessionModule.createRedisRepository(config.session?.redisUrl);
                    }
                    if (storageType === session_options_interface_1.SessionStorageType.MEMORY) {
                        return new memory_session_repository_1.MemorySessionRepository();
                    }
                    return typeormRepo;
                },
                inject: [typeorm_session_repository_1.TypeORMSessionRepository],
            },
            session_manager_service_1.SessionManagerService,
        ],
        exports: [
            session_manager_service_1.SESSION_REPOSITORY,
            session_manager_service_1.SessionManagerService,
            typeorm_session_repository_1.TypeORMSessionRepository,
            memory_session_repository_1.MemorySessionRepository,
        ],
    })
], SessionModule);
//# sourceMappingURL=session.module.js.map