"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWhatsappIds = exports.setWhatsappId = void 0;
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const setWhatsappId = (whatsappId, split) => {
    console.log('os.tmpdir(): ', os_1.default.tmpdir());
    const whatsappIdFile = path_1.default.join(os_1.default.tmpdir(), `whatsappIdInfo.txt`);
    try {
        if (split) {
            fs_1.default.unlink(whatsappIdFile, (err) => {
                if (err) {
                    throw err;
                }
                console.log(`Delete File ${whatsappIdFile} successfully.`);
            });
        }
        fs_1.default.appendFile(whatsappIdFile, `${whatsappId},`, err => {
            if (err) {
                console.error(err);
            }
            // done!
        });
    }
    catch (error) {
        console.log('There was an error on try to read/delete the whatsappIdInfo.json file: ', error);
    }
};
exports.setWhatsappId = setWhatsappId;
const getWhatsappIds = () => {
    const whatsappIdFile = path_1.default.join(os_1.default.tmpdir(), `whatsappIdInfo.txt`);
    try {
        if (fs_1.default.existsSync(whatsappIdFile)) {
            let whatsappInfo = fs_1.default.readFileSync(whatsappIdFile, { encoding: 'utf8', flag: 'r' });
            if (whatsappInfo && whatsappInfo.endsWith(',')) {
                whatsappInfo = whatsappInfo.substring(0, whatsappInfo.length - 1);
            }
            whatsappInfo = whatsappInfo.split(',');
            // console.log('xxxxxxxxxx whatsappInfo: ',whatsappInfo)
            return whatsappInfo;
        }
        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.getWhatsappIds = getWhatsappIds;
