"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.MemorySessionRepository = void 0;
const common_1 = require("@nestjs/common");
const base_session_repository_1 = require("./base-session.repository");
const uuid_1 = require("uuid");
let MemorySessionRepository = class MemorySessionRepository extends base_session_repository_1.BaseSessionRepository {
    sessions = new Map();
    userSessions = new Map();
    async create(session) {
        const sessionId = session.id || (0, uuid_1.v4)();
        const sessionData = {
            id: sessionId,
            userId: session.userId,
            refreshToken: session.refreshToken,
            data: session.data,
            expiresAt: session.expiresAt,
            userAgent: session.userAgent,
            deviceName: session.deviceName,
            ipAddress: session.ipAddress,
            lastActive: session.lastActive || new Date(),
            createdAt: new Date(),
            updatedAt: new Date(),
        };
        this.sessions.set(sessionId, sessionData);
        if (!this.userSessions.has(session.userId)) {
            this.userSessions.set(session.userId, new Set());
        }
        this.userSessions.get(session.userId).add(sessionId);
        return sessionData;
    }
    async findById(sessionId) {
        const session = this.sessions.get(sessionId);
        if (!session)
            return null;
        if (this.isExpired(session)) {
            await this.delete(sessionId);
            return null;
        }
        return session;
    }
    async findByUserId(userId) {
        const sessionIds = this.userSessions.get(userId) || new Set();
        const sessions = [];
        for (const sessionId of sessionIds) {
            const session = await this.findById(sessionId);
            if (session) {
                sessions.push(session);
            }
        }
        return sessions.sort((a, b) => (b.createdAt?.getTime() || 0) - (a.createdAt?.getTime() || 0));
    }
    async findActiveByUserId(userId) {
        const allSessions = await this.findByUserId(userId);
        return this.filterActive(allSessions);
    }
    async update(sessionId, updates) {
        const session = await this.findById(sessionId);
        if (!session) {
            throw new Error(`Session ${sessionId} not found`);
        }
        const updated = {
            ...session,
            ...updates,
            updatedAt: new Date(),
        };
        this.sessions.set(sessionId, updated);
        return updated;
    }
    async delete(sessionId) {
        const session = this.sessions.get(sessionId);
        if (!session)
            return;
        this.sessions.delete(sessionId);
        const userSessionIds = this.userSessions.get(session.userId);
        if (userSessionIds) {
            userSessionIds.delete(sessionId);
            if (userSessionIds.size === 0) {
                this.userSessions.delete(session.userId);
            }
        }
    }
    async deleteByUserId(userId) {
        const sessionIds = this.userSessions.get(userId) || new Set();
        for (const sessionId of sessionIds) {
            this.sessions.delete(sessionId);
        }
        this.userSessions.delete(userId);
    }
    async deleteExpired() {
        let count = 0;
        const now = new Date();
        for (const [sessionId, session] of this.sessions.entries()) {
            if (this.isExpired(session)) {
                await this.delete(sessionId);
                count++;
            }
        }
        return count;
    }
    async countActiveByUserId(userId) {
        const activeSessions = await this.findActiveByUserId(userId);
        return activeSessions.length;
    }
    async updateLastActive(sessionId) {
        await this.update(sessionId, {
            lastActive: new Date(),
        });
    }
    clear() {
        this.sessions.clear();
        this.userSessions.clear();
    }
};
exports.MemorySessionRepository = MemorySessionRepository;
exports.MemorySessionRepository = MemorySessionRepository = __decorate([
    (0, common_1.Injectable)()
], MemorySessionRepository);
//# sourceMappingURL=memory-session.repository.js.map