"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// services/rabbitmqService.ts
const amqplib_1 = __importDefault(require("amqplib"));
class RabbitmqService {
    /**
     * 将消息发布到指定的 RabbitMQ 队列
     * @param {string} queueName - 消息将要发送到的队列名称
     * @param {object} messagePayload - 要发送的 JSON 消息体
     */
    async publishActivityMessage(queueName, messagePayload) {
        let connection = null;
        try {
            const rabbitMqUrl = process.env.RABBITMQ_URL;
            if (!rabbitMqUrl) {
                throw new Error("RABBITMQ_URL not defined in environment variables.");
            }
            connection = await amqplib_1.default.connect(rabbitMqUrl);
            const channel = await connection.createChannel();
            // 确保队列存在
            await channel.assertQueue(queueName, {
                durable: true,
            });
            // 将消息转换为 Buffer 并发送
            const msg = JSON.stringify(messagePayload);
            channel.sendToQueue(queueName, Buffer.from(msg), {
                persistent: true,
            });
            console.log(`[x] Sent message to queue '${queueName}': '${msg}'`);
        }
        catch (error) {
            if (error instanceof Error) {
                console.error(`Failed to publish message: ${error.message}`);
            }
            else {
                console.error("An unknown error occurred while publishing the message.");
            }
        }
        finally {
            if (connection) {
                // 确保连接在消息发送后关闭
                await connection.close();
            }
        }
    }
}
exports.default = new RabbitmqService();
