"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 ShowUserServiceReport_1 = __importDefault(require("../services/UserServices/ShowUserServiceReport"));
const socket_1 = require("../libs/socket");
const CountTicketsByUserQueue_1 = __importDefault(require("../services/UserServices/CountTicketsByUserQueue"));
const ShowQueuesByUser_1 = __importDefault(require("../services/UserServices/ShowQueuesByUser"));
// import CountTicketsByUserQueue from "../services/UserServices/CountTicketsByUserQueue";
const TicketEmiterSumOpenClosedByUser = (userId, startDate, endDate) => __awaiter(void 0, void 0, void 0, function* () {
    const openByUser = yield ShowUserServiceReport_1.default({ startDate: startDate, endDate: endDate, ticketStatus: 'open', userId: userId.toString() });
    const closedByUser = yield ShowUserServiceReport_1.default({ startDate: endDate, endDate: endDate, ticketStatus: 'closed', userId: userId.toString() });
    const openByUserOnQueue = yield CountTicketsByUserQueue_1.default({ startDate: startDate, endDate: endDate, status: 'open', clientChatStart: true, userId: userId });
    const openByUserOutQueue = yield CountTicketsByUserQueue_1.default({ startDate: startDate, endDate: endDate, status: 'open', clientChatStart: false, userId: userId });
    const closedByUserOnQueue = yield CountTicketsByUserQueue_1.default({ startDate: startDate, endDate: endDate, status: 'closed', clientChatStart: true, userId: userId });
    const closedUserOutQueue = yield CountTicketsByUserQueue_1.default({ startDate: startDate, endDate: endDate, status: 'closed', clientChatStart: false, userId: userId });
    const queuesByUser = yield ShowQueuesByUser_1.default({ profile: 'user', userId: userId });
    let openCloseOnQueue = openByUserOnQueue.concat(closedByUserOnQueue);
    let openCloseOutQueue = openByUserOutQueue.concat(closedUserOutQueue);
    // OPEN, CLOSED TICKETS STARTED BY CLIENTS
    for (let i = 0; i < queuesByUser.length; i++) {
        queuesByUser[i].countOpen = 0;
        queuesByUser[i].countClosed = 0;
        for (let x = 0; x < openCloseOnQueue.length; x++) {
            if ((queuesByUser[i].userId == openCloseOnQueue[x].userId) &&
                (queuesByUser[i].queueId == openCloseOnQueue[x].queueId && openCloseOnQueue[x].status == 'open')) {
                queuesByUser[i].countOpen = openCloseOnQueue[x].totAttendance;
            }
            else if ((queuesByUser[i].userId == openCloseOnQueue[x].userId) &&
                (queuesByUser[i].queueId == openCloseOnQueue[x].queueId && openCloseOnQueue[x].status == 'closed')) {
                queuesByUser[i].countClosed = openCloseOnQueue[x].totAttendance;
            }
        }
    }
    // OPEN, CLOSED TICKETS STARTED BY USERS
    let openClosedOutQueue = {};
    let open = openCloseOutQueue.filter((e) => e.status == 'open');
    let closed = openCloseOutQueue.filter((e) => e.status == 'closed');
    openClosedOutQueue = Object.assign(Object.assign({}, openClosedOutQueue), { userId: userId, countOpen: open && open.length > 0 ? open[0].totAttendance : 0, countClosed: closed && closed.length > 0 ? closed[0].totAttendance : 0 });
    const io = socket_1.getIO();
    io.emit("onlineStatus", {
        action: "update",
        userOnlineTime: {
            sumOpen: openByUser.length > 0 ? openByUser[0] : { userId: userId, count: '' },
            sumClosed: closedByUser.length > 0 ? closedByUser[0] : { userId: userId, count: '' },
            openClosedOutQueue: openClosedOutQueue,
            openClosedInQueue: queuesByUser
        }
    });
});
exports.default = TicketEmiterSumOpenClosedByUser;
