"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const sequelize_1 = require("sequelize");
const UserQueue_1 = __importDefault(require("../../models/UserQueue"));
const dbConfig = require("../../config/database");
const { QueryTypes } = require("sequelize");
const sequelize = new sequelize_1.Sequelize(dbConfig);
const ListWhatsappQueuesByUserQueue = (userId) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        let userQueue = yield UserQueue_1.default.findAll({
            where: { userId },
            attributes: ["queueId"],
            raw: true
        });
        if (userQueue && userQueue.length > 0) {
            userQueue = userQueue.map((u) => u.queueId);
            const result = yield sequelize.query(`select w.id, w.number, wq.whatsappId, wq.queueId, q.id, q.name from WhatsappQueues wq join Queues q on
wq.queueId = q.id join Whatsapps w
on wq.whatsappId = w.id where q.id in (${userQueue.join()})`, { type: QueryTypes.SELECT });
            return result;
        }
    }
    catch (error) {
        console.error("Error fetching joined data:", error);
    }
    return [];
});
exports.default = ListWhatsappQueuesByUserQueue;
