"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._restore = exports.getRestoreControll = exports.delRestoreControllFile = exports.shifRestoreControll = exports.setRestoreControll = void 0;
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
// import { insertOrUpeateWhatsCache } from './WhatsCache';
const AutoRestore_1 = __importDefault(require("./AutoRestore"));
const socket_1 = require("../libs/socket");
const setRestoreControll = (obj) => {
    const restoreInfoFile = path_1.default.join(os_1.default.tmpdir(), `restoreInfo.json`);
    try {
        if (fs_1.default.existsSync(restoreInfoFile)) {
            if (Array.isArray(obj)) {
                fs_1.default.writeFileSync(restoreInfoFile, JSON.stringify(obj), "utf8");
            }
            else {
                const restoreInfo = fs_1.default.readFileSync(restoreInfoFile, { encoding: 'utf8', flag: 'r' });
                let lstRestore = JSON.parse(restoreInfo);
                lstRestore.push(obj);
                fs_1.default.writeFileSync(restoreInfoFile, JSON.stringify(lstRestore), "utf8");
            }
        }
        else {
            console.log('restoreInfo.json file not found! It will be created.');
            if (Array.isArray(obj)) {
                fs_1.default.writeFileSync(restoreInfoFile, JSON.stringify(obj), "utf8");
            }
            else {
                fs_1.default.writeFileSync(restoreInfoFile, JSON.stringify([obj]), "utf8");
            }
        }
    }
    catch (error) {
        console.log('There was an error on try to read the restoreInfo.json file: ', error);
    }
};
exports.setRestoreControll = setRestoreControll;
const shifRestoreControll = () => {
    const restoreInfoFile = path_1.default.join(os_1.default.tmpdir(), `restoreInfo.json`);
    try {
        if (fs_1.default.existsSync(restoreInfoFile)) {
            const restoreInfo = fs_1.default.readFileSync(restoreInfoFile, { encoding: 'utf8', flag: 'r' });
            let lstRestore = JSON.parse(restoreInfo);
            let whatsapp = lstRestore.shift();
            fs_1.default.writeFileSync(restoreInfoFile, JSON.stringify(lstRestore), "utf8");
            return whatsapp;
        }
    }
    catch (error) {
        console.log('There was an error on try to read the restoreInfo.json file: ', error);
    }
    return {};
};
exports.shifRestoreControll = shifRestoreControll;
const delRestoreControllFile = () => {
    const restoreInfoFile = path_1.default.join(os_1.default.tmpdir(), `restoreInfo.json`);
    try {
        if (fs_1.default.existsSync(restoreInfoFile)) {
            fs_1.default.unlinkSync(restoreInfoFile);
        }
        else {
            console.log('restoreInfo.json file not found!');
        }
    }
    catch (error) {
        console.log('There was an error on try delete the restoreInfo.json file: ', error);
    }
};
exports.delRestoreControllFile = delRestoreControllFile;
const getRestoreControll = () => {
    const restoreInfoFile = path_1.default.join(os_1.default.tmpdir(), `restoreInfo.json`);
    try {
        if (fs_1.default.existsSync(restoreInfoFile)) {
            const restoreInfo = fs_1.default.readFileSync(restoreInfoFile, { encoding: 'utf8', flag: 'r' });
            let lstRestore = JSON.parse(restoreInfo);
            return lstRestore;
        }
        else {
            console.log('restoreInfo.json file not found!');
        }
    }
    catch (error) {
        console.log('There was an error on try to read the restoreInfo.json file: ', error);
    }
    return [];
};
exports.getRestoreControll = getRestoreControll;
const _restore = (whatsapp, msg_file_title) => __awaiter(void 0, void 0, void 0, function* () {
    return;
    if (whatsapp.status != 'RESTORING' && whatsapp.status != 'qrcode') {
        console.log('THE WHATSAAP ID: ', whatsapp.id, ' WILL BE RESTORED SOON!');
        yield whatsapp.update({ status: "RESTORING", });
        const io = socket_1.getIO();
        io.emit("whatsappSession", {
            action: "update",
            session: whatsapp
        });
        // await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { status: "RESTORING", })
        setTimeout(() => __awaiter(void 0, void 0, void 0, function* () { return yield AutoRestore_1.default(whatsapp.id, msg_file_title); }), 95000);
    }
});
exports._restore = _restore;
