"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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogoutService = void 0;
const common_1 = require("@nestjs/common");
const typeorm_1 = require("@nestjs/typeorm");
const typeorm_2 = require("typeorm");
const event_emitter_1 = require("@nestjs/event-emitter");
const user_entity_1 = require("../../user/entities/user.entity");
const session_manager_service_1 = require("../../session/services/session-manager.service");
const request_context_1 = require("../../request-context/request-context");
const auth_constants_1 = require("../../auth.constants");
const logged_out_event_1 = require("../events/logged-out.event");
const logged_out_all_event_1 = require("../events/logged-out-all.event");
let LogoutService = class LogoutService {
    userRepository;
    sessionManager;
    eventEmitter;
    constructor(userRepository, sessionManager, eventEmitter) {
        this.userRepository = userRepository;
        this.sessionManager = sessionManager;
        this.eventEmitter = eventEmitter;
    }
    async logout(logoutType = 'user', reason) {
        const session = request_context_1.RequestContext.currentSession();
        const user = await request_context_1.RequestContext.currentUser();
        if (session) {
            await this.eventEmitter.emitAsync(auth_constants_1.NestAuthEvents.LOGGED_OUT, new logged_out_event_1.LoggedOutEvent({
                user,
                tenantId: session?.data?.tenantId ?? user?.tenantId,
                session,
                logoutType,
                reason,
            }));
            await this.sessionManager.revokeSession(session.id, 'logout');
        }
        return true;
    }
    async logoutAll(userId, logoutType = 'user', reason) {
        const sessions = await this.sessionManager.getUserSessions(userId);
        await this.sessionManager.revokeAllUserSessions(userId);
        const user = await this.userRepository.findOne({ where: { id: userId } });
        if (user) {
            await this.eventEmitter.emitAsync(auth_constants_1.NestAuthEvents.LOGGED_OUT_ALL, new logged_out_all_event_1.LoggedOutAllEvent({
                user,
                tenantId: request_context_1.RequestContext.currentTenantId(),
                logoutType,
                reason,
                sessions,
            }));
        }
        return true;
    }
};
exports.LogoutService = LogoutService;
exports.LogoutService = LogoutService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, typeorm_1.InjectRepository)(user_entity_1.NestAuthUser)),
    __metadata("design:paramtypes", [typeorm_2.Repository,
        session_manager_service_1.SessionManagerService,
        event_emitter_1.EventEmitter2])
], LogoutService);
//# sourceMappingURL=logout.service.js.map