"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 Contact_1 = __importDefault(require("../models/Contact"));
const wbotMessageListener_1 = require("../services/WbotServices/wbotMessageListener");
const WhatsappOfficialAPI_1 = __importDefault(require("./WhatsappOfficialAPI"));
function sendWhatsAppMessageOfficialAPI(ticket, body, quotedMsgSerializedId, _template) {
    return __awaiter(this, void 0, void 0, function* () {
        const { contactId, phoneNumberId } = ticket;
        const contact = yield Contact_1.default.findByPk(contactId);
        const { number } = contact;
        let data = {
            messaging_product: "whatsapp",
            recipient_type: "individual",
            to: number
        };
        if (_template) {
            const { template } = _template;
            data = Object.assign(Object.assign({}, data), { type: "template", template });
        }
        else {
            data = Object.assign(Object.assign({}, data), { type: "text", text: {
                    preview_url: true,
                    body
                } });
        }
        if (quotedMsgSerializedId) {
            data = Object.assign(Object.assign({}, data), { context: { message_id: quotedMsgSerializedId.id } });
        }
        if (!wbotMessageListener_1.isValidMsg({ type: data.type })) {
            return;
        }
        console.log("SEND MESSAGE: ", JSON.stringify(data, null, 2));
        WhatsappOfficialAPI_1.default
            .post(`/${process.env.VERSION}/${phoneNumberId}/messages`, data)
            .then(response => {
            console.log("Response:", response.data);
            let msg = {};
            msg = Object.assign(Object.assign({}, msg), { id: { id: response.data.messages[0].id }, fromMe: true, type: "chat", read: false, phoneNumberId,
                body });
            wbotMessageListener_1.verifyMessage(msg, ticket, contact, quotedMsgSerializedId === null || quotedMsgSerializedId === void 0 ? void 0 : quotedMsgSerializedId.id);
        })
            .catch(error => {
            console.log("Error on try request: ", error.response.data.error.message);
        });
    });
}
exports.default = sendWhatsAppMessageOfficialAPI;
