"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 SettingTicket_1 = __importDefault(require("../models/SettingTicket"));
const ListTicketTimeLife_1 = __importDefault(require("../services/TicketServices/ListTicketTimeLife"));
const UpdateTicketService_1 = __importDefault(require("../services/TicketServices/UpdateTicketService"));
const date_fns_1 = require("date-fns");
const pt_BR_1 = __importDefault(require("date-fns/locale/pt-BR"));
const SplitDateTime_1 = require("./SplitDateTime");
const Whatsapp_1 = __importDefault(require("../models/Whatsapp"));
const fsPromises = require("fs/promises");
const fs = require("fs");
let timer;
const AutoCloseTickets = () => __awaiter(void 0, void 0, void 0, function* () {
    try {
        // const botInfo = await BotIsOnQueue('botqueue')
        // if (!botInfo.userIdBot) return
        const whatsapps = yield Whatsapp_1.default.findAll({ group: ["number"] });
        for (const whatsapp of whatsapps) {
            // console.log("-------> whatsapp: ", JSON.stringify(whatsapps, null, 6));
            const ticketExpiration = yield SettingTicket_1.default.findOne({
                where: { key: "ticketExpiration", number: whatsapp.number }
            });
            if (ticketExpiration && ticketExpiration.value == "enabled") {
                const startTime = SplitDateTime_1.splitDateTime(new Date(date_fns_1.format(new Date(ticketExpiration.startTime), "yyyy-MM-dd HH:mm:ss", {
                    locale: pt_BR_1.default
                })));
                const seconds = timeStringToSeconds(startTime.fullTime);
                let tickets = yield ListTicketTimeLife_1.default({
                    timeseconds: seconds,
                    status: "open",
                    number: whatsapp.number
                });
                for (let i = 0; i < tickets.length; i++) {
                    yield UpdateTicketService_1.default({
                        ticketData: { status: "closed", statusChatEnd: "FINALIZADO" },
                        ticketId: tickets[i].ticket_id,
                        msg: ticketExpiration.message
                    });
                }
            }
        }
    }
    catch (error) {
        console.log("There was an error on try close the bot tickets: ", error);
    }
});
function timeStringToSeconds(timeString) {
    const [hours, minutes, seconds] = timeString.split(":").map(Number);
    return hours * 3600 + minutes * 60 + seconds;
}
const schedule = () => __awaiter(void 0, void 0, void 0, function* () {
    try {
        clearInterval(timer);
        yield AutoCloseTickets();
    }
    catch (error) {
        console.log("error on schedule: ", error);
    }
    finally {
        timer = setInterval(schedule, 60000);
    }
});
timer = setInterval(schedule, 60000);
exports.default = schedule;
