"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 ListTicketTimeLife_1 = __importDefault(require("../services/TicketServices/ListTicketTimeLife"));
const UpdateTicketService_1 = __importDefault(require("../services/TicketServices/UpdateTicketService"));
const BotIsOnQueue_1 = __importDefault(require("./BotIsOnQueue"));
const fsPromises = require("fs/promises");
const fs = require('fs');
let timer;
const CloseBotTickets = () => __awaiter(void 0, void 0, void 0, function* () {
    try {
        const botInfo = yield BotIsOnQueue_1.default('botqueue');
        if (!botInfo.userIdBot)
            return;
        let tickets = yield ListTicketTimeLife_1.default({ timeseconds: 60, status: 'open', userId: botInfo.userIdBot });
        console.log('tickets: ', tickets);
        for (let i = 0; i < tickets.length; i++) {
            yield UpdateTicketService_1.default({
                ticketData: { 'status': 'closed', 'userId': botInfo.userIdBot, 'statusChatEnd': 'FINALIZADO' },
                ticketId: tickets[i].ticket_id
            });
        }
    }
    catch (error) {
        console.log('There was an error on try close the bot tickets: ', error);
    }
});
const schedule = () => __awaiter(void 0, void 0, void 0, function* () {
    try {
        clearInterval(timer);
        yield CloseBotTickets();
    }
    catch (error) {
        console.log('error on schedule: ', error);
    }
    finally {
        timer = setInterval(schedule, 60000);
    }
});
timer = setInterval(schedule, 60000);
exports.default = schedule;
