"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestContext = void 0;
const async_hooks_1 = require("async_hooks");
class RequestContext {
    static storage = new async_hooks_1.AsyncLocalStorage();
    id;
    request;
    response;
    constructor(request, response) {
        this.id = Math.random();
        this.request = request;
        this.response = response;
    }
    static create(request, response, next) {
        const context = new RequestContext(request, response);
        RequestContext.storage.run(context, () => next());
    }
    static current() {
        return RequestContext.storage.getStore();
    }
    static currentRequest() {
        const requestContext = RequestContext.current();
        return requestContext ? requestContext.request : null;
    }
    static currentUser() {
        const request = RequestContext.currentRequest();
        if (!request['user']) {
            return null;
        }
        return {
            id: request['user'].sub,
            ...request['user']
        };
    }
    static currentSession() {
        const request = RequestContext.currentRequest();
        return request ? request['session'] : null;
    }
    static getDeviceInfo() {
        return {
            deviceName: this.getDeviceName(),
            ipAddress: this.getIpAddress(),
            browser: this.getBrowser(),
        };
    }
    static getDeviceName() {
        const request = this.currentRequest();
        if (!request) {
            return 'Unknown Device';
        }
        const userAgent = request.headers['user-agent'] || 'unknown';
        if (userAgent.includes('Windows')) {
            return 'Windows';
        }
        else if (userAgent.includes('Macintosh')) {
            return 'Mac';
        }
        else if (userAgent.includes('Linux')) {
            return 'Linux';
        }
        else if (userAgent.includes('Android')) {
            return 'Android';
        }
        else if (userAgent.includes('iPhone') || userAgent.includes('iPad')) {
            return 'iOS';
        }
        return 'Unknown Device';
    }
    static getBrowser() {
        const request = this.currentRequest();
        if (!request) {
            return 'unknown';
        }
        const userAgent = request.headers['user-agent'] || 'unknown';
        if (userAgent.includes('Chrome') && !userAgent.includes('Chromium')) {
            return 'Chrome';
        }
        if (userAgent.includes('Firefox')) {
            return 'Firefox';
        }
        if (userAgent.includes('Safari') && !userAgent.includes('Chrome')) {
            return 'Safari';
        }
        if (userAgent.includes('Edg')) {
            return 'Edge';
        }
        if (userAgent.includes('Opera') || userAgent.includes('OPR')) {
            return 'Opera';
        }
        if (userAgent.includes('MSIE') || userAgent.includes('Trident/')) {
            return 'Internet Explorer';
        }
        if (userAgent.includes('Brave')) {
            return 'Brave';
        }
        return 'Unknown Browser';
    }
    static getIpAddress() {
        const request = this.currentRequest();
        if (request) {
            return request.ip ||
                request.headers['x-forwarded-for']?.toString() ||
                request.connection.remoteAddress ||
                'unknown';
        }
        return 'unknown';
    }
}
exports.RequestContext = RequestContext;
//# sourceMappingURL=request-context.js.map