"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseSessionRepository = void 0;
const ms_1 = __importDefault(require("ms"));
class BaseSessionRepository {
    isExpired(session) {
        if (!session.expiresAt)
            return false;
        return new Date() > new Date(session.expiresAt);
    }
    calculateExpiresAt(duration) {
        const milliseconds = typeof duration === 'string' ? (0, ms_1.default)(duration) : duration * 1000;
        return new Date(Date.now() + milliseconds);
    }
    serializeSession(session) {
        return {
            ...session,
            data: session.data ? JSON.stringify(session.data) : null,
            expiresAt: session.expiresAt instanceof Date
                ? session.expiresAt.toISOString()
                : session.expiresAt,
            lastActive: session.lastActive instanceof Date
                ? session.lastActive.toISOString()
                : session.lastActive,
        };
    }
    deserializeSession(data) {
        return {
            ...data,
            data: data['data'] && data['data'] !== 'null' && data['data'] !== ''
                ? JSON.parse(data['data'])
                : null,
            expiresAt: data['expiresAt'] ? new Date(data['expiresAt']) : null,
            lastActive: data['lastActive'] ? new Date(data['lastActive']) : null,
        };
    }
    filterActive(sessions) {
        return sessions.filter(session => !this.isExpired(session));
    }
}
exports.BaseSessionRepository = BaseSessionRepository;
//# sourceMappingURL=base-session.repository.js.map