"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 JISMetadataRepository_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JISMetadataRepository = void 0;
const const_1 = require("../../../schema-definitions/jis/redis/const");
const JISMetadataDtoSchema_1 = require("../../../schema-definitions/jis/redis/schemas/JISMetadataDtoSchema");
const RedisModel_1 = require("@golemio/core/dist/integration-engine/models/RedisModel");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
let JISMetadataRepository = JISMetadataRepository_1 = class JISMetadataRepository extends RedisModel_1.RedisModel {
    constructor() {
        super("JISMetadataRepository", {
            decodeDataAfterGet: JSON.parse,
            encodeDataBeforeSave: JSON.stringify,
            isKeyConstructedFromData: false,
            prefix: JISMetadataRepository_1.NAMESPACE_PREFIX,
        }, new golemio_validator_1.JSONSchemaValidator("JISMetadataRepositoryValidator", JISMetadataDtoSchema_1.JISMetadataDtoSchema));
        this.infotextsKey = "infotexts";
        this.eventsKey = "events";
    }
    /** Get the Etag of the last JIS Infotexts response */
    async getLastInfotextsEtag() {
        try {
            const metadata = await this.get(this.infotextsKey);
            return metadata?.lastResponse.etag;
        }
        catch (err) {
            if (err instanceof golemio_errors_1.GeneralError)
                throw err;
            throw new golemio_errors_1.GeneralError("Error while getting last infotexts etag", this.constructor.name, err);
        }
    }
    /**
     * Set the Etag of the last JIS Infotexts response to a given new value
     *
     * @param etag The new Etag of the last JIS Infotexts response
     */
    async setLastInfotextsEtag(etag) {
        try {
            let metadata = await this.get(this.infotextsKey);
            if (!metadata)
                metadata = this.initializeMetadata();
            metadata.lastResponse.etag = etag;
            metadata.lastResponse.updatedAt = new Date().toISOString();
            await this.set(this.infotextsKey, metadata);
        }
        catch (err) {
            if (err instanceof golemio_errors_1.GeneralError)
                throw err;
            throw new golemio_errors_1.GeneralError("Error while updating last infotexts etag", this.constructor.name, err);
        }
    }
    async getLastEventsEtag() {
        try {
            const metadata = await this.get(this.eventsKey);
            return metadata?.lastResponse.etag;
        }
        catch (err) {
            if (err instanceof golemio_errors_1.GeneralError)
                throw err;
            throw new golemio_errors_1.GeneralError("Error while getting last events etag", this.constructor.name, err);
        }
    }
    async setLastEventsEtag(etag) {
        try {
            let metadata = await this.get(this.eventsKey);
            if (!metadata)
                metadata = this.initializeMetadata();
            metadata.lastResponse.etag = etag;
            metadata.lastResponse.updatedAt = new Date().toISOString();
            await this.set(this.eventsKey, metadata);
        }
        catch (err) {
            if (err instanceof golemio_errors_1.GeneralError)
                throw err;
            throw new golemio_errors_1.GeneralError("Error while updating last events etag", this.constructor.name, err);
        }
    }
    initializeMetadata() {
        return {
            lastResponse: {
                etag: "",
                updatedAt: new Date().toISOString(),
            },
        };
    }
};
exports.JISMetadataRepository = JISMetadataRepository;
JISMetadataRepository.NAMESPACE_PREFIX = const_1.JIS_METADATA_NAMESPACE_PREFIX;
exports.JISMetadataRepository = JISMetadataRepository = JISMetadataRepository_1 = __decorate([
    (0, tsyringe_1.injectable)(),
    __metadata("design:paramtypes", [])
], JISMetadataRepository);
//# sourceMappingURL=JISMetadataRepository.js.map