"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 Queue_1 = __importDefault(require("../../models/Queue"));
const User_1 = __importDefault(require("../../models/User"));
const UserQueue_1 = __importDefault(require("../../models/UserQueue"));
const ListUser = ({ profile, userId, raw, userIds, profiles, userQueues: userQueuesToNumber }) => __awaiter(void 0, void 0, void 0, function* () {
    let where_clause = {};
    let userIdInQueues = [];
    if (userQueuesToNumber !== undefined) {
        let userQueues = userQueuesToNumber.map(id => parseInt(id));
        const userQueuesFiltered = yield UserQueue_1.default.findAll({
            where: { queueId: { [sequelize_1.Op.or]: [userQueues, null] } },
            order: [
                ['userId', 'ASC']
            ],
            raw: true
        });
        if (userQueuesFiltered)
            for (let queueId of userQueues) {
                for (let userQueue of userQueuesFiltered) {
                    if (queueId == userQueue.queueId) {
                        const isAlready = userIdInQueues.indexOf(userQueue.userId);
                        if (isAlready === -1)
                            userIdInQueues.push(userQueue.userId);
                    }
                }
            }
    }
    if (userId && profile) {
        where_clause = {
            [sequelize_1.Op.and]: [{ userId: userId }, { profile: profile }]
        };
    }
    else if (userId) {
        where_clause = {
            [sequelize_1.Op.and]: [{ userId: userId }]
        };
    }
    else if (profile) {
        where_clause = {
            profile: profile
        };
    }
    else if (userIds) {
        where_clause = {
            id: { [sequelize_1.Op.in]: userIds }
        };
    }
    else if (profiles && userIdInQueues.length > 0) {
        where_clause = {
            profile: { [sequelize_1.Op.in]: profiles },
            id: { [sequelize_1.Op.in]: userIdInQueues }
        };
    }
    else if (profiles) {
        where_clause = {
            profile: { [sequelize_1.Op.in]: profiles },
        };
    }
    const users = yield User_1.default.findAll({
        where: where_clause,
        raw,
        attributes: ["id", "name", "email", "transferToOtherQueues"],
        include: [
            { model: Queue_1.default, as: "queues", attributes: ["id", "name", "color"] }
        ],
        order: [["id", "ASC"]],
        group: userIds ? undefined : ["User.id"]
    });
    return users;
});
exports.default = ListUser;
