"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.RefreshTokenInterceptor = void 0;
const common_1 = require("@nestjs/common");
const auth_service_1 = require("../services/auth.service");
const cookie_service_1 = require("../services/cookie.service");
const jwt_service_1 = require("../../core/services/jwt.service");
const auth_config_service_1 = require("../../core/services/auth-config.service");
const auth_constants_1 = require("../../auth.constants");
let RefreshTokenInterceptor = class RefreshTokenInterceptor {
    authService;
    cookieService;
    jwtService;
    authConfig;
    constructor(authService, cookieService, jwtService, authConfig) {
        this.authService = authService;
        this.cookieService = cookieService;
        this.jwtService = jwtService;
        this.authConfig = authConfig;
    }
    async intercept(context, next) {
        const request = context.switchToHttp().getRequest();
        const response = context.switchToHttp().getResponse();
        const authHeader = request.headers.authorization;
        if (!authHeader || !authHeader.startsWith('Bearer ')) {
            return next.handle();
        }
        const accessToken = authHeader.split(' ')[1];
        if (!accessToken) {
            return next.handle();
        }
        try {
            await this.jwtService.verifyToken(accessToken);
            return next.handle();
        }
        catch (error) {
            const refreshToken = this.extractRefreshToken(request);
            if (!refreshToken) {
                return next.handle();
            }
            try {
                const newSession = await this.authService.refreshToken(refreshToken);
                const config = this.authConfig.getConfig();
                const useCookies = config.accessTokenType === 'cookie';
                if (useCookies) {
                    this.cookieService.setTokens(response, newSession.accessToken, newSession.refreshToken);
                }
                else {
                    request.headers.authorization = `Bearer ${newSession.accessToken}`;
                }
                return next.handle();
            }
            catch (refreshError) {
                return next.handle();
            }
        }
    }
    extractRefreshToken(request) {
        const tokenFromCookie = request.cookies?.[auth_constants_1.REFRESH_TOKEN_COOKIE_NAME];
        if (tokenFromCookie) {
            return tokenFromCookie;
        }
        const authHeader = request.headers['x-refresh-token'];
        if (authHeader) {
            return authHeader;
        }
        return null;
    }
};
exports.RefreshTokenInterceptor = RefreshTokenInterceptor;
exports.RefreshTokenInterceptor = RefreshTokenInterceptor = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [auth_service_1.AuthService,
        cookie_service_1.CookieService,
        jwt_service_1.JwtService,
        auth_config_service_1.AuthConfigService])
], RefreshTokenInterceptor);
//# sourceMappingURL=refresh-token.interceptor.js.map