"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.restoreMonit = void 0;
const Whatsapp_1 = __importDefault(require("../models/Whatsapp"));
const AutoRestore_1 = __importDefault(require("./AutoRestore"));
const RestoreControll_1 = require("./RestoreControll");
let lstAutoRestore = [];
// Delete a directory and its children 
const restoreMonit = (whatsappId) => {
    return;
    try {
        let lstRestore = RestoreControll_1.getRestoreControll();
        console.log('ENTROU NO AUTO RESTORE..................................');
        if (Object.keys(lstRestore.filter((e) => +e.id == +whatsappId)).length) {
            console.log('Restore executed by human whatasappId: ', whatsappId);
        }
        else {
            lstAutoRestore.push({ 'whatsappId': +whatsappId });
            setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
                lstRestore = RestoreControll_1.getRestoreControll();
                let count = lstAutoRestore.length;
                for (let i = 0; i < count; i++) {
                    let autoR = lstAutoRestore.shift();
                    console.log('----------------> autoR: ', autoR);
                    if (autoR && autoR.whatsappId) {
                        if (Object.keys(lstRestore.filter((e) => +e.id == +autoR.whatsappId)).length) {
                            console.log(' EXECUTING RESTORING autoR: ', autoR);
                            continue;
                        }
                        const _whatsapp = yield Whatsapp_1.default.findOne({ where: { id: autoR.whatsappId } });
                        console.log('----------------> autoR exec after 180 seconds: ', autoR);
                        let whatsappStatus = ["CONFLICT",
                            "DEPRECATED_VERSION",
                            "OPENING",
                            "PAIRING",
                            "PROXYBLOCK",
                            "SMB_TOS_BLOCK",
                            "TIMEOUT",
                            "TOS_BLOCK",
                            "UNLAUNCHED",
                            "UNPAIRED",
                            "UNPAIRED_IDLE"];
                        if (_whatsapp === null || _whatsapp === void 0 ? void 0 : _whatsapp.status) {
                            if (whatsappStatus.includes(_whatsapp.status)) {
                                yield AutoRestore_1.default(autoR.whatsappId, 'auto_monit');
                            }
                        }
                    }
                }
            }), 180000);
        }
    }
    catch (error) {
        console.log('There was an error on try execute AUTO-RESTORE: ', error);
    }
};
exports.restoreMonit = restoreMonit;
