"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var NestAuthModule_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NestAuthModule = void 0;
const common_1 = require("@nestjs/common");
const event_emitter_1 = require("@nestjs/event-emitter");
const request_context_middleware_1 = require("./request-context/request-context.middleware");
const auth_module_1 = require("./auth/auth.module");
const user_module_1 = require("./user/user.module");
const role_module_1 = require("./role/role.module");
const session_module_1 = require("./session/session.module");
const tenant_module_1 = require("./tenant/tenant.module");
const core_module_1 = require("./core/core.module");
const auth_config_service_1 = require("./core/services/auth-config.service");
const auth_constants_1 = require("./auth.constants");
const admin_console_module_1 = require("./admin-console/admin-console.module");
const audit_service_1 = require("./audit/services/audit.service");
const deepmerge_1 = __importDefault(require("deepmerge"));
let NestAuthModule = NestAuthModule_1 = class NestAuthModule {
    static forRoot(options) {
        const mergedOptions = this.getOptions(options);
        auth_config_service_1.AuthConfigService.setOptions(mergedOptions);
        const providers = [audit_service_1.AuditService];
        return {
            module: NestAuthModule_1,
            global: mergedOptions.isGlobal,
            imports: [
                event_emitter_1.EventEmitterModule,
                core_module_1.CoreModule,
                auth_module_1.AuthModule,
                tenant_module_1.TenantModule,
                user_module_1.UserModule,
                role_module_1.RoleModule,
                session_module_1.SessionModule,
                admin_console_module_1.AdminConsoleModule,
            ],
            providers,
            exports: [
                core_module_1.CoreModule,
                auth_module_1.AuthModule,
                tenant_module_1.TenantModule,
                user_module_1.UserModule,
                role_module_1.RoleModule,
                session_module_1.SessionModule,
                admin_console_module_1.AdminConsoleModule,
                audit_service_1.AuditService,
            ],
        };
    }
    static forRootAsync(options) {
        const asyncProviders = this.createAsyncProviders(options);
        const providers = [...asyncProviders, audit_service_1.AuditService];
        return {
            module: NestAuthModule_1,
            global: options.isGlobal,
            imports: [
                event_emitter_1.EventEmitterModule,
                core_module_1.CoreModule,
                auth_module_1.AuthModule,
                tenant_module_1.TenantModule,
                user_module_1.UserModule,
                role_module_1.RoleModule,
                session_module_1.SessionModule,
                admin_console_module_1.AdminConsoleModule,
                ...(options.imports || []),
            ],
            providers,
            exports: [
                core_module_1.CoreModule,
                auth_module_1.AuthModule,
                tenant_module_1.TenantModule,
                user_module_1.UserModule,
                role_module_1.RoleModule,
                session_module_1.SessionModule,
                admin_console_module_1.AdminConsoleModule,
                audit_service_1.AuditService,
            ],
        };
    }
    static createAsyncProviders(options) {
        if (options.useExisting || options.useFactory) {
            return [this.createAsyncOptionsProvider(options)];
        }
        if (options.useClass) {
            return [
                this.createAsyncOptionsProvider(options),
                {
                    provide: options.useClass,
                    useClass: options.useClass,
                },
            ];
        }
        return [];
    }
    static createAsyncOptionsProvider(options) {
        if (options.useFactory) {
            return {
                provide: auth_constants_1.NEST_AUTH_ASYNC_OPTIONS_PROVIDER,
                useFactory: async (...args) => {
                    const userOptions = await options.useFactory(...args);
                    const mergedOptions = this.getOptions(userOptions);
                    auth_config_service_1.AuthConfigService.setOptions(mergedOptions);
                    return mergedOptions;
                },
                inject: options.inject || [],
            };
        }
        return {
            provide: auth_constants_1.NEST_AUTH_ASYNC_OPTIONS_PROVIDER,
            useFactory: async (optionsFactory) => {
                const userOptions = await optionsFactory.createAuthModuleOptions();
                const mergedOptions = this.getOptions(userOptions);
                auth_config_service_1.AuthConfigService.setOptions(mergedOptions);
                return mergedOptions;
            },
            inject: [options.useExisting || options.useClass],
        };
    }
    static getOptions(options) {
        const defaultOptions = auth_config_service_1.AuthConfigService.getDefaultOptions();
        return (0, deepmerge_1.default)(defaultOptions, options);
    }
    configure(consumer) {
        consumer.apply(request_context_middleware_1.RequestContextMiddleware).forRoutes('auth/*');
    }
};
exports.NestAuthModule = NestAuthModule;
exports.NestAuthModule = NestAuthModule = NestAuthModule_1 = __decorate([
    (0, common_1.Module)({})
], NestAuthModule);
//# sourceMappingURL=nest-auth.module.js.map