"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.ModelMetadataRepository = void 0;
const cache_manager_1 = require("@nestjs/cache-manager");
const common_1 = require("@nestjs/common");
const crypto_1 = require("crypto");
const model_metadata_entity_1 = require("../entities/model-metadata.entity");
const cache_helper_1 = require("../helpers/cache.helper");
const typeorm_1 = require("typeorm");
const solid_base_repository_1 = require("./solid-base.repository");
let ModelMetadataRepository = class ModelMetadataRepository extends solid_base_repository_1.SolidBaseRepository {
    constructor(dataSource, cacheManager) {
        super(model_metadata_entity_1.ModelMetadata, dataSource, null, null);
        this.dataSource = dataSource;
        this.cacheManager = cacheManager;
    }
    async find(options) {
        if (!(0, cache_helper_1.shouldUseCache)()) {
            return super.find(options);
        }
        const cacheKey = this.buildCacheKey('find', options);
        const cached = await this.cacheManager.get(cacheKey);
        if (cached !== undefined) {
            return cached;
        }
        const result = await super.find(options);
        await this.cacheManager.set(cacheKey, result);
        return result;
    }
    async findOne(options) {
        if (!(0, cache_helper_1.shouldUseCache)()) {
            return super.findOne(options);
        }
        const cacheKey = this.buildCacheKey('findOne', options);
        const cached = await this.cacheManager.get(cacheKey);
        if (cached !== undefined) {
            return cached;
        }
        const result = await super.findOne(options);
        await this.cacheManager.set(cacheKey, result);
        return result;
    }
    buildCacheKey(method, options) {
        const serialized = JSON.stringify(options ?? {});
        const hash = (0, crypto_1.createHash)('sha256').update(serialized).digest('hex');
        return `modelMetadataRepo:${method}:${hash}`;
    }
};
exports.ModelMetadataRepository = ModelMetadataRepository;
exports.ModelMetadataRepository = ModelMetadataRepository = __decorate([
    (0, common_1.Injectable)(),
    __param(1, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)),
    __metadata("design:paramtypes", [typeorm_1.DataSource, Object])
], ModelMetadataRepository);
//# sourceMappingURL=model-metadata.repository.js.map