"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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ETagCacheInterceptor = exports.CACHE_KEY_KEY = exports.CACHE_TTL_KEY = void 0;
exports.CacheTTL = CacheTTL;
exports.CacheKey = CacheKey;
const common_1 = require("@nestjs/common");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const etag_cache_service_1 = require("../cache/etag-cache.service");
const core_1 = require("@nestjs/core");
exports.CACHE_TTL_KEY = 'cache_ttl';
exports.CACHE_KEY_KEY = 'cache_key';
let ETagCacheInterceptor = class ETagCacheInterceptor {
    constructor(cacheService, reflector) {
        this.cacheService = cacheService;
        this.reflector = reflector;
    }
    intercept(context, next) {
        const req = context.switchToHttp().getRequest();
        const res = context.switchToHttp().getResponse();
        if (req.method !== 'GET') {
            return next.handle();
        }
        if (res.headersSent) {
            return rxjs_1.NEVER;
        }
        const cacheKey = res.locals.cacheKey || req.url;
        const _ttl = this.reflector.getAllAndOverride(exports.CACHE_TTL_KEY, [
            context.getHandler(),
            context.getClass(),
        ]) ?? 30 * 1000;
        return next.handle().pipe((0, operators_1.tap)((data) => {
            if (res.headersSent || data === undefined || data === null) {
                return;
            }
            const etag = this.cacheService.set(cacheKey, data);
            if (etag && !res.headersSent) {
                res.set('ETag', etag);
            }
        }));
    }
};
exports.ETagCacheInterceptor = ETagCacheInterceptor;
exports.ETagCacheInterceptor = ETagCacheInterceptor = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, common_1.Inject)(etag_cache_service_1.ETagCacheService)),
    __metadata("design:paramtypes", [etag_cache_service_1.ETagCacheService,
        core_1.Reflector])
], ETagCacheInterceptor);
const common_2 = require("@nestjs/common");
function CacheTTL(ttlMs) {
    return (0, common_2.SetMetadata)(exports.CACHE_TTL_KEY, ttlMs);
}
function CacheKey(key) {
    return (0, common_2.SetMetadata)(exports.CACHE_KEY_KEY, key);
}
//# sourceMappingURL=etag-cache.interceptor.js.map