"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 AppError_1 = __importDefault(require("../../errors/AppError"));
const RedisClient_1 = require("../../helpers/RedisClient");
const TicketCache_1 = require("../../helpers/TicketCache");
const socket_1 = require("../../libs/socket");
const Message_1 = __importDefault(require("../../models/Message"));
const Ticket_1 = __importDefault(require("../../models/Ticket"));
const CreateMessageService = ({ messageData }) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        yield Message_1.default.upsert(messageData);
        const message = yield Message_1.default.findByPk(messageData.id, {
            include: [
                "contact",
                {
                    model: Ticket_1.default,
                    as: "ticket",
                    include: ["contact", "queue"]
                },
                {
                    model: Message_1.default,
                    as: "quotedMsg",
                    include: ["contact"]
                }
            ]
        });
        if (!message) {
            throw new Error("ERR_CREATING_MESSAGE");
        }
        //////////////////// SETTINGS ///////////////////////////
        let ticketRemote = yield RedisClient_1.get({
            key: `remote:ticketId:${message.ticket.id}`
        });
        if (ticketRemote && !ticketRemote.includes("messageDateTime")) {
            ticketRemote = JSON.parse(ticketRemote);
            ticketRemote = Object.assign(Object.assign({}, ticketRemote), { messageDateTime: message.createdAt });
            const ticket = yield Ticket_1.default.findByPk(message.ticket.id);
            ticket === null || ticket === void 0 ? void 0 : ticket.update({ remoteDone: true });
            console.log('MESSAGE SERVICE: XXXXXXXXXXXXXXXXXXXXXXX');
            RedisClient_1.set(`remote:ticketId:${message.ticket.id}`, JSON.stringify(ticketRemote));
        }
        //////////////////////////////////////////////////////////
        if (message.ticket.status != "queueChoice") {
            yield TicketCache_1.updateTicketCacheByTicketId(message.ticket.id, {
                lastMessage: message.body,
                updatedAt: new Date(message.ticket.updatedAt).toISOString(),
                "contact.profilePicUrl": message.ticket.contact.profilePicUrl,
                unreadMessages: message.ticket.unreadMessages
            });
            const io = socket_1.getIO();
            io.to(message.ticketId.toString())
                .to(message.ticket.status)
                .to("notification")
                .emit("appMessage", {
                action: "create",
                message,
                ticket: message.ticket,
                contact: message.ticket.contact
            });
        }
        return message;
    }
    catch (error) {
        console.error("===> Error on CreateMessageService.ts file: \n", error);
        throw new AppError_1.default(error.message);
    }
});
exports.default = CreateMessageService;
