"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 fsPromises = require("fs/promises");
const fs = require('fs');
const checkInternetConnected = require('check-internet-connected');
const ShowWhatsAppService_1 = __importDefault(require("../services/WhatsappService/ShowWhatsAppService"));
const SplitDateTime_1 = require("../helpers/SplitDateTime");
const date_fns_1 = require("date-fns");
const pt_BR_1 = __importDefault(require("date-fns/locale/pt-BR"));
const path_1 = __importDefault(require("path"));
const socket_1 = require("../libs/socket");
const internetConnection = (whatasappId) => __awaiter(void 0, void 0, void 0, function* () {
    checkInternetConnected().then((result) => {
        console.log('Internet successfully connected to a server: ', result); //successfully connected to a server
    }).catch((ex) => {
        console.log('Intenet connection cannot connect to a server or error occurred: ', ex); // cannot connect to a server or error occurred.
    });
    const config = {
        timeout: 5000,
        retries: 5,
        domain: 'https://apple.com',
    };
    let internet_status = null;
    try {
        internet_status = yield checkInternetConnected(config);
    }
    catch (error) {
        console.log('Erron when trying get internet connection info: ', error);
    }
    console.log('INTERNET STATUS: ', internet_status);
    if (whatasappId && !internet_status) {
        const dateToday = SplitDateTime_1.splitDateTime(new Date(date_fns_1.format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: pt_BR_1.default })));
        let timestamp = Math.floor(Date.now() / 1000);
        const sourcePath = path_1.default.join(process.cwd(), '.wwebjs_auth', 'sessions', 'log');
        fs.writeFileSync(`${sourcePath}/${timestamp}_internet_connection_lost.txt`, `Date: ${dateToday.fullDate} ${dateToday.fullTime}`, (error) => { console.log(error); });
        let whats = yield ShowWhatsAppService_1.default(whatasappId);
        yield whats.update({ status: "OPENING" });
        const io = socket_1.getIO();
        io.emit("whatsappSession", {
            action: "update",
            session: whats
        });
    }
    return internet_status;
});
exports.default = internetConnection;
