"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionUtil = void 0;
const ms_1 = __importDefault(require("ms"));
class SessionUtil {
    static extractMetadata(session) {
        return {
            device: session.deviceName || 'Unknown Device',
            ip: session.ipAddress || 'Unknown IP',
        };
    }
    static getRemainingTime(session) {
        if (!session.expiresAt) {
            return {
                milliseconds: 0,
                seconds: 0,
                minutes: 0,
                hours: 0,
                formatted: 'Never',
            };
        }
        const now = Date.now();
        const expiresAt = new Date(session.expiresAt).getTime();
        const remaining = Math.max(0, expiresAt - now);
        const seconds = Math.floor(remaining / 1000);
        const minutes = Math.floor(seconds / 60);
        const hours = Math.floor(minutes / 60);
        return {
            milliseconds: remaining,
            seconds,
            minutes,
            hours,
            formatted: (0, ms_1.default)(remaining, { long: true }),
        };
    }
    static isExpiringSoon(session, thresholdMs = 5 * 60 * 1000) {
        const remaining = this.getRemainingTime(session);
        return remaining.milliseconds > 0 && remaining.milliseconds < thresholdMs;
    }
    static isExpired(session) {
        if (!session.expiresAt)
            return false;
        return new Date() > new Date(session.expiresAt);
    }
    static isActive(session) {
        return !this.isExpired(session);
    }
    static formatForDisplay(session) {
        return {
            id: session.id,
            device: session.deviceName || 'Unknown',
            ipAddress: session.ipAddress || 'Unknown',
            lastActive: session.lastActive
                ? new Date(session.lastActive).toLocaleString()
                : 'Unknown',
            expiresAt: session.expiresAt
                ? new Date(session.expiresAt).toLocaleString()
                : 'Never',
            isActive: this.isActive(session),
        };
    }
    static sanitize(session) {
        const { refreshToken, data, ...sanitized } = session;
        return sanitized;
    }
    static isSameDevice(session1, session2) {
        return (session1.deviceName === session2.deviceName &&
            session1.userAgent === session2.userAgent &&
            session1.ipAddress === session2.ipAddress);
    }
    static getAge(session) {
        if (!session.createdAt) {
            return { milliseconds: 0, formatted: 'Unknown' };
        }
        const age = Date.now() - new Date(session.createdAt).getTime();
        return {
            milliseconds: age,
            formatted: (0, ms_1.default)(age, { long: true }),
        };
    }
    static getIdleTime(session) {
        if (!session.lastActive) {
            return { milliseconds: 0, formatted: 'Unknown' };
        }
        const idle = Date.now() - new Date(session.lastActive).getTime();
        return {
            milliseconds: idle,
            formatted: (0, ms_1.default)(idle, { long: true }),
        };
    }
}
exports.SessionUtil = SessionUtil;
//# sourceMappingURL=session.util.js.map