"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 Contact_1 = __importDefault(require("../../models/Contact"));
const ContactCustomField_1 = __importDefault(require("../../models/ContactCustomField"));
const TicketCache_1 = require("../../helpers/TicketCache");
const ContactsCache_1 = require("../../helpers/ContactsCache");
const AssociateContatctQueue_1 = __importDefault(require("./AssociateContatctQueue"));
const UpdateContactService = ({ contactData, contactId }) => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const { email, name, number, extraInfo, queueIds } = contactData;
        // console.log('email, name, number, extraInfo: ', email, name, number, extraInfo)
        const contact = yield Contact_1.default.findOne({
            where: { id: contactId },
            attributes: ["id", "name", "number", "email", "profilePicUrl"],
            include: ["extraInfo"]
        });
        if (!contact) {
            throw new AppError_1.default("ERR_NO_CONTACT_FOUND", 404);
        }
        if (extraInfo) {
            yield Promise.all(extraInfo.map((info) => __awaiter(void 0, void 0, void 0, function* () {
                yield ContactCustomField_1.default.upsert(Object.assign(Object.assign({}, info), { contactId: contact.id }));
            })));
            yield Promise.all(contact.extraInfo.map((oldInfo) => __awaiter(void 0, void 0, void 0, function* () {
                const stillExists = extraInfo.findIndex(info => info.id === oldInfo.id);
                if (stillExists === -1) {
                    yield ContactCustomField_1.default.destroy({ where: { id: oldInfo.id } });
                }
            })));
        }
        const oldNumber = contact.number;
        //Solução para o erro tcp_wrap.cc
        console.log("----------> oldNumber: ", oldNumber);
        if (number) {
            const numberExists = yield Contact_1.default.findOne({
                where: { number }
            });
            if (numberExists && numberExists.id != +contactId) {
                throw new AppError_1.default("ERR_DUPLICATED_CONTACT");
            }
        }
        yield contact.update({
            name,
            number,
            email
        });
        if (queueIds)
            yield AssociateContatctQueue_1.default(contact, queueIds);
        //TEST DEL
        yield TicketCache_1.updateTicketsByContactsCache(oldNumber, contact.name, contact.number);
        //
        yield contact.reload({
            attributes: ["id", "name", "number", "email", "profilePicUrl"],
            include: ["extraInfo"]
        });
        // console.log('contactcontactcontactcontact: ',flatten(JSON.parse(JSON.stringify(contact))))
        yield ContactsCache_1.updateContactCacheById(contact.id, JSON.parse(JSON.stringify(contact)));
        return contact;
    }
    catch (error) {
        console.error("===> Error on UpdateContactService.ts file: \n", error);
        throw new AppError_1.default(error.message);
    }
});
exports.default = UpdateContactService;
