"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessLogInterceptor = void 0;
const common_1 = require("@nestjs/common");
const rxjs_1 = require("rxjs");
const access_log_service_1 = require("./access-log.service");
let AccessLogInterceptor = class AccessLogInterceptor {
    constructor(accessLogService) {
        this.accessLogService = accessLogService;
    }
    intercept(context, next) {
        var _a;
        if (context.getType() !== 'http') {
            return next.handle();
        }
        const request = context.switchToHttp().getRequest();
        const path = (_a = request.url) !== null && _a !== void 0 ? _a : '';
        if (path.startsWith('/mcp')) {
            return next.handle();
        }
        return next.handle().pipe((0, rxjs_1.tap)(() => {
            var _a, _b, _c, _d;
            const userId = (_a = request.auth) === null || _a === void 0 ? void 0 : _a.sub;
            const forwarded = request.headers['x-forwarded-for'];
            const ip = forwarded ? forwarded.split(',')[0].trim() : ((_b = request.ip) !== null && _b !== void 0 ? _b : '');
            const userAgent = (_c = request.headers['user-agent']) !== null && _c !== void 0 ? _c : '';
            const method = (_d = request.method) !== null && _d !== void 0 ? _d : '';
            this.accessLogService.recordHttp({ userId, ip, userAgent, method, path });
        }));
    }
};
exports.AccessLogInterceptor = AccessLogInterceptor;
exports.AccessLogInterceptor = AccessLogInterceptor = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [access_log_service_1.AccessLogService])
], AccessLogInterceptor);
//# sourceMappingURL=access-log.interceptor.js.map