"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.isOutBusinessTimeSaturday = exports.isOutBusinessTime = exports.isHoliday = exports.isWeekend = void 0;
const SettingTicket_1 = __importDefault(require("../models/SettingTicket"));
const SplitDateTime_1 = require("./SplitDateTime");
const date_fns_tz_1 = require("date-fns-tz");
const date_fns_1 = require("date-fns");
const pt_BR_1 = __importDefault(require("date-fns/locale/pt-BR"));
const isHoliday = (number) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b;
    let obj = { set: false, msg: "" };
    const holiday = yield SettingTicket_1.default.findOne({
        where: { key: "holiday", number }
    });
    if (holiday &&
        holiday.value == "enabled" &&
        ((_b = (_a = holiday.message) === null || _a === void 0 ? void 0 : _a.trim()) === null || _b === void 0 ? void 0 : _b.length) > 0) {
        const startTime = SplitDateTime_1.splitDateTime(new Date(date_fns_1.format(new Date(holiday.startTime), "yyyy-MM-dd HH:mm:ss", {
            locale: pt_BR_1.default
        })));
        const currentDate = SplitDateTime_1.splitDateTime(new Date(date_fns_1.format(new Date(), "yyyy-MM-dd HH:mm:ss", {
            locale: pt_BR_1.default
        })));
        if (currentDate.fullDate == startTime.fullDate) {
            obj.set = true;
            obj.msg = holiday.message.trim();
        }
    }
    return obj;
});
exports.isHoliday = isHoliday;
const isWeekend = (number) => __awaiter(void 0, void 0, void 0, function* () {
    var _c, _d;
    let obj = { set: false, msg: "" };
    const weekend = yield SettingTicket_1.default.findOne({
        where: { key: "weekend", number }
    });
    if (weekend &&
        weekend.value == "enabled" &&
        ((_d = (_c = weekend.message) === null || _c === void 0 ? void 0 : _c.trim()) === null || _d === void 0 ? void 0 : _d.length) > 0) {
        // Convert parsed date to Brazil time zone
        const localDate = localDateConvert();
        // Check if it's Saturday or Sunday
        if (date_fns_1.isSaturday(localDate)) {
            const saturday = yield SettingTicket_1.default.findOne({
                where: { key: "saturday" }
            });
            if (saturday && saturday.value == "enabled") {
                //  botSendMessage(ticket, weekend.message);
                obj.set = true;
                obj.msg = weekend.message;
            }
        }
        else if (date_fns_1.isSunday(localDate)) {
            const sunday = yield SettingTicket_1.default.findOne({
                where: { key: "sunday" }
            });
            if (sunday && sunday.value == "enabled") {
                //  botSendMessage(ticket, weekend.message);
                obj.set = true;
                obj.msg = weekend.message;
            }
        }
        else {
            //  obj.set = true;
            //  obj.msg = weekend.message;
        }
        return obj;
    }
});
exports.isWeekend = isWeekend;
function isOutBusinessTime(number) {
    var _a, _b;
    return __awaiter(this, void 0, void 0, function* () {
        let obj = { set: false, msg: "" };
        const outBusinessHours = yield SettingTicket_1.default.findOne({
            where: { key: "outBusinessHours", number }
        });
        let isWithinRange = false;
        if (outBusinessHours &&
            outBusinessHours.value == "enabled" &&
            ((_b = (_a = outBusinessHours === null || outBusinessHours === void 0 ? void 0 : outBusinessHours.message) === null || _a === void 0 ? void 0 : _a.trim()) === null || _b === void 0 ? void 0 : _b.length) > 0) {
            const ticketDateTimeUpdate = SplitDateTime_1.splitDateTime(new Date(date_fns_1.format(new Date(), "yyyy-MM-dd HH:mm:ss", {
                locale: pt_BR_1.default
            })));
            const startTime = SplitDateTime_1.splitDateTime(new Date(date_fns_1.format(new Date(outBusinessHours.startTime), "yyyy-MM-dd HH:mm:ss", {
                locale: pt_BR_1.default
            })));
            const endTime = SplitDateTime_1.splitDateTime(new Date(date_fns_1.format(new Date(outBusinessHours.endTime), "yyyy-MM-dd HH:mm:ss", {
                locale: pt_BR_1.default
            })));
            const format = "HH:mm:ss";
            const parsedStartTime = date_fns_1.parse(ticketDateTimeUpdate.fullTime, format, new Date());
            const parsedEndTime = date_fns_1.parse(startTime.fullTime, format, new Date());
            const parsedTimeToCheck = date_fns_1.parse(endTime.fullTime, format, new Date());
            const timeInterval = { start: parsedStartTime, end: parsedEndTime };
            // If the time range spans across different days, handle the date part
            if (parsedEndTime < parsedStartTime) {
                const nextDay = new Date(parsedStartTime);
                nextDay.setDate(nextDay.getDate() + 1);
                timeInterval.end = nextDay;
            }
            isWithinRange = date_fns_1.isWithinInterval(parsedTimeToCheck, timeInterval);
            if (!isWithinRange) {
                obj.set = true;
                obj.msg = outBusinessHours.message;
            }
        }
        return obj;
    });
}
exports.isOutBusinessTime = isOutBusinessTime;
function isOutBusinessTimeSaturday(number) {
    var _a, _b;
    return __awaiter(this, void 0, void 0, function* () {
        let obj = { set: false, msg: "" };
        // Convert parsed date to Brazil time zone
        const localDate = localDateConvert();
        // Check if it's Saturday or Sunday
        if (!date_fns_1.isSaturday(localDate)) {
            return obj;
        }
        const outBusinessHoursSaturday = yield SettingTicket_1.default.findOne({
            where: { key: "saturdayBusinessTime", number }
        });
        let isWithinRange = false;
        if (outBusinessHoursSaturday &&
            outBusinessHoursSaturday.value == "enabled" &&
            ((_b = (_a = outBusinessHoursSaturday === null || outBusinessHoursSaturday === void 0 ? void 0 : outBusinessHoursSaturday.message) === null || _a === void 0 ? void 0 : _a.trim()) === null || _b === void 0 ? void 0 : _b.length) > 0) {
            const ticketDateTimeUpdate = SplitDateTime_1.splitDateTime(new Date(date_fns_1.format(new Date(), "yyyy-MM-dd HH:mm:ss", {
                locale: pt_BR_1.default
            })));
            const startTime = SplitDateTime_1.splitDateTime(new Date(date_fns_1.format(new Date(outBusinessHoursSaturday.startTime), "yyyy-MM-dd HH:mm:ss", {
                locale: pt_BR_1.default
            })));
            const endTime = SplitDateTime_1.splitDateTime(new Date(date_fns_1.format(new Date(outBusinessHoursSaturday.endTime), "yyyy-MM-dd HH:mm:ss", {
                locale: pt_BR_1.default
            })));
            const format = "HH:mm:ss";
            const parsedStartTime = date_fns_1.parse(ticketDateTimeUpdate.fullTime, format, new Date());
            const parsedEndTime = date_fns_1.parse(startTime.fullTime, format, new Date());
            const parsedTimeToCheck = date_fns_1.parse(endTime.fullTime, format, new Date());
            const timeInterval = { start: parsedStartTime, end: parsedEndTime };
            // If the time range spans across different days, handle the date part
            if (parsedEndTime < parsedStartTime) {
                const nextDay = new Date(parsedStartTime);
                nextDay.setDate(nextDay.getDate() + 1);
                timeInterval.end = nextDay;
            }
            isWithinRange = date_fns_1.isWithinInterval(parsedTimeToCheck, timeInterval);
            if (!isWithinRange) {
                obj.set = true;
                obj.msg = outBusinessHoursSaturday.message;
            }
        }
        return obj;
    });
}
exports.isOutBusinessTimeSaturday = isOutBusinessTimeSaturday;
function localDateConvert() {
    const brazilTimeZone = "America/Sao_Paulo";
    const currentDateUtc = new Date();
    // Convert UTC date to Brazil time zone
    const currentDate = date_fns_tz_1.utcToZonedTime(currentDateUtc, brazilTimeZone);
    // Format the date using the desired format
    const formattedDate = date_fns_1.format(currentDate, "yyyy-MM-dd HH:mm:ssXXX");
    const parsedDate = date_fns_1.parseISO(formattedDate);
    // Convert parsed date to Brazil time zone
    const localDate = date_fns_tz_1.utcToZonedTime(parsedDate, brazilTimeZone);
    return localDate;
}
