"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.dragonfly = void 0;
const ioredis_1 = __importDefault(require("ioredis"));
const env_1 = require("../../config/env");
const logger_1 = require("../../config/logger");
class DragonflyClient {
    constructor() {
        this.ttl = parseInt(env_1.env.DRAGONFLY_TTL_SECONDS, 10);
        this.client = new ioredis_1.default(env_1.env.DRAGONFLY_URL, {
            maxRetriesPerRequest: 3,
            enableReadyCheck: true,
            lazyConnect: true,
        });
        this.client.on('connect', () => logger_1.logger.info('DragonflyDB conectado'));
        this.client.on('error', (err) => logger_1.logger.error(err, 'DragonflyDB erro'));
    }
    async connect() {
        await this.client.connect();
    }
    async get(key) {
        return this.client.get(key);
    }
    async getJson(key) {
        const raw = await this.client.get(key);
        if (!raw)
            return null;
        return JSON.parse(raw);
    }
    async set(key, value, ttlSeconds) {
        await this.client.set(key, value, 'EX', ttlSeconds ?? this.ttl);
    }
    async setJson(key, value, ttlSeconds) {
        await this.client.set(key, JSON.stringify(value), 'EX', ttlSeconds ?? this.ttl);
    }
    async del(key) {
        await this.client.del(key);
    }
    async exists(key) {
        const count = await this.client.exists(key);
        return count > 0;
    }
    /** Publica em um canal pub/sub (usado para broadcasting de QR/status) */
    async publish(channel, message) {
        await this.client.publish(channel, message);
    }
    raw() {
        return this.client;
    }
}
exports.dragonfly = new DragonflyClient();
