"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 wbot_1 = require("../libs/wbot");
const ShowWhatsAppService_1 = __importDefault(require("../services/WhatsappService/ShowWhatsAppService"));
const StartWhatsAppSession_1 = require("../services/WbotServices/StartWhatsAppSession");
const UpdateWhatsAppService_1 = __importDefault(require("../services/WhatsappService/UpdateWhatsAppService"));
const socket_1 = require("../libs/socket");
const RestoreControll_1 = require("../helpers/RestoreControll");
const AutoRestore_1 = __importDefault(require("../helpers/AutoRestore"));
const axios_1 = __importDefault(require("axios"));
const Whatsapp_1 = __importDefault(require("../models/Whatsapp"));
const EndPointQuery_1 = __importDefault(require("../helpers/EndPointQuery"));
// let lstRestore: any = [] 
const store = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    const { whatsappId } = req.params;
    const whatsapp = yield ShowWhatsAppService_1.default(whatsappId);
    StartWhatsAppSession_1.StartWhatsAppSession(whatsapp);
    return res.status(200).json({ message: "Starting session." });
});
const update = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    const { whatsappId } = req.params;
    const { whatsapp } = yield UpdateWhatsAppService_1.default({
        whatsappId,
        whatsappData: { session: "" }
    });
    StartWhatsAppSession_1.StartWhatsAppSession(whatsapp);
    return res.status(200).json({ message: "Starting session." });
});
const restart = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    const { whatsappId } = req.params;
    const io = socket_1.getIO();
    if (Object.keys(req.body).length > 0) {
        let lstRestore = RestoreControll_1.getRestoreControll();
        for (let i = 0; i < lstRestore.length; i++) {
            io.emit("whatsappSession", {
                action: "update",
                session: { 'id': +lstRestore[i].id, 'disabled': true }
            });
        }
        return res.status(200).json({});
    }
    let whatsapp = yield Whatsapp_1.default.findByPk(whatsappId);
    if (whatsapp) {
        yield whatsapp.update({ status: 'OPENING' });
        const io = socket_1.getIO();
        io.emit("whatsappSession", {
            action: "update",
            session: whatsapp
        });
        whatsapp = JSON.parse(JSON.stringify(whatsapp));
        console.log('whatsapp url: ', whatsapp.url);
        yield EndPointQuery_1.default(`${whatsapp.url}/api/restore`, {});
    }
    AutoRestore_1.default(whatsappId);
    return res.status(200).json({ message: "Starting session." });
});
const remove = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    const { whatsappId } = req.params;
    // const whatsapp = await ShowWhatsAppService(whatsappId);
    // const wbot = getWbot(whatsapp.id);
    // await wbot.logout();
    try {
        const wbot_url = yield wbot_1.getWbot(whatsappId);
        let response = yield axios_1.default.post(`${wbot_url}/api/disconnect`);
    }
    catch (error) {
        console.log('There was an error on try disconnect the whatsapp id: ', whatsappId);
    }
    return res.status(200).json({ message: "Session disconnected." });
});
exports.default = { store, remove, update, restart };
