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);
};
import logger from "../Utils/Logger";
import Redis from "./Redis";
import OneUptimeDate from "../../Types/Date";
import BadDataException from "../../Types/Exception/BadDataException";
import DatabaseNotConnectedException from "../../Types/Exception/DatabaseNotConnectedException";
import JSONFunctions from "../../Types/JSONFunctions";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export default class GlobalCache {
    static async getJSONObject(namespace, key) {
        const json = await this.getJSONArrayOrObject(namespace, key);
        if (!json) {
            return null;
        }
        if (Array.isArray(json)) {
            throw new BadDataException("Expected JSONObject, but got JSONArray");
        }
        return json;
    }
    static async getStringArray(namespace, key) {
        const value = await this.getString(namespace, key);
        if (!value) {
            return null;
        }
        const stringArr = JSON.parse(value);
        if (!Array.isArray(stringArr)) {
            throw new BadDataException("Expected String Array, but got something else");
        }
        return stringArr;
    }
    static async setStringArray(namespace, key, value, options) {
        await this.setString(namespace, key, JSON.stringify(value), options);
    }
    static async getJSONArray(namespace, key) {
        const json = await this.getJSONArrayOrObject(namespace, key);
        if (!json) {
            return null;
        }
        if (!Array.isArray(json)) {
            throw new BadDataException("Expected JSONArray, but got JSONObject");
        }
        return json;
    }
    static async getJSONArrayOrObject(namespace, key) {
        const value = await this.getString(namespace, key);
        if (!value) {
            return null;
        }
        try {
            let jsonObject = JSONFunctions.parse(value);
            if (Array.isArray(jsonObject)) {
                jsonObject = JSONFunctions.deserializeArray(jsonObject);
            }
            else {
                jsonObject = JSONFunctions.deserialize(jsonObject);
            }
            if (!jsonObject) {
                return null;
            }
            return jsonObject;
        }
        catch (err) {
            logger.error(err);
            return null;
        }
    }
    static async getString(namespace, key) {
        const client = Redis.getClient();
        if (!client || !Redis.isConnected()) {
            throw new DatabaseNotConnectedException("Cache is not connected");
        }
        const value = await (client === null || client === void 0 ? void 0 : client.get(`${namespace}-${key}`));
        if (!value) {
            return null;
        }
        return value;
    }
    static async setJSON(namespace, key, value, options) {
        await this.setString(namespace, key, JSON.stringify(JSONFunctions.serialize(value)), options);
    }
    static async setString(namespace, key, value, options) {
        var _a;
        const client = Redis.getClient();
        if (!client || !Redis.isConnected()) {
            throw new DatabaseNotConnectedException("Cache is not connected");
        }
        await client.set(`${namespace}-${key}`, value);
        const expiresInSeconds = (_a = options === null || options === void 0 ? void 0 : options.expiresInSeconds) !== null && _a !== void 0 ? _a : OneUptimeDate.getSecondsInDays(30);
        await client.expire(`${namespace}-${key}`, expiresInSeconds);
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], GlobalCache, "getJSONObject", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], GlobalCache, "getStringArray", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Array, Object]),
    __metadata("design:returntype", Promise)
], GlobalCache, "setStringArray", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], GlobalCache, "getJSONArray", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], GlobalCache, "getString", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object, Object]),
    __metadata("design:returntype", Promise)
], GlobalCache, "setJSON", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, String, Object]),
    __metadata("design:returntype", Promise)
], GlobalCache, "setString", null);
//# sourceMappingURL=GlobalCache.js.map