"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 });
exports.wbotMonitorQrcodeNumberRead = exports.wbotMonitorQrcodeRemote = exports.wbotMonitorRemote = void 0;
const socket_1 = require("../libs/socket");
const logger_1 = require("../utils/logger");
const Whatsapp_1 = __importDefault(require("../models/Whatsapp"));
const OmnhitDashboardSession_1 = __importDefault(require("../helpers/OmnhitDashboardSession"));
// type IndexQuery = {
//   centro_custo: string;
// };
const wbotMonitorRemote = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    const { action, whatsappId, reason } = req.body;
    console.log("action: ", action, " | whatsappId: ", whatsappId, " | reason: ", reason);
    console.log("-----------> ACTION: ", req.body["action"]);
    const whatsapp = yield Whatsapp_1.default.findByPk(whatsappId, { raw: true });
    if (whatsapp) {
        if (action === "disconnected") {
            logger_1.logger.info(`Disconnected session: ${whatsapp.name}, reason: ${reason}`);
        }
        let data = {};
        data.whatsapp = Object.assign(Object.assign({}, whatsapp), { DB: process.env.DB_NAME });
        data.action = "update";
        yield OmnhitDashboardSession_1.default(data);
        const io = socket_1.getIO();
        io.emit("whatsappSession", {
            action: "update",
            session: whatsapp
        });
    }
    return res.status(200).json({ message: "Ok" });
});
exports.wbotMonitorRemote = wbotMonitorRemote;
const wbotMonitorQrcodeRemote = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    const { whatsappId } = req.body;
    console.log("-----------> QRCODE MONITOR whatsappId: ", req.body["whatsappId"]);
    const whatsapp = yield Whatsapp_1.default.findByPk(whatsappId, { raw: true });
    // let whatsapp = await ShowWhatsAppService(whatsappId)
    if (whatsapp) {
        let data = {};
        data.whatsapp = Object.assign(Object.assign({}, whatsapp), { DB: process.env.DB_NAME });
        data.action = "update";
        yield OmnhitDashboardSession_1.default(data);
    }
    if (whatsapp) {
        const io = socket_1.getIO();
        io.emit("whatsappSession", {
            action: "update",
            session: whatsapp
        });
    }
    return res.status(200).json({ message: "Ok" });
});
exports.wbotMonitorQrcodeRemote = wbotMonitorQrcodeRemote;
const wbotMonitorQrcodeNumberRead = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    const { number } = req.body;
    console.log("-----------> number read: ", number);
    const msg = `Numero lido: ${number} \nEssa sessão de whatsapp foi desconectada porque o numero que esta descrito no nome dessa sessão não corresponde ao numero lido!`;
    let data = {};
    data.msg = msg;
    data.action = "error";
    yield OmnhitDashboardSession_1.default(data);
    const io = socket_1.getIO();
    io.emit("whatsappSession", {
        action: "error",
        msg: `Numero lido: ${number} \nEssa sessão de whatsapp foi desconectada porque o numero que esta descrito no nome dessa sessão não corresponde ao numero lido!`
    });
    return res.status(200).json({ message: "Ok" });
});
exports.wbotMonitorQrcodeNumberRead = wbotMonitorQrcodeNumberRead;
