import dayjs from "dayjs";

export const dateWithoutTimezone = (date: Date) => {
    const tzoffset = date.getTimezoneOffset() * 60000; //offset in milliseconds
    return new Date(date.valueOf() - tzoffset)
        .toISOString()
        .slice(0, -1);
};

export const getDatesInMonth = (inputDate: Date) => {
    const month = inputDate.getMonth();
    const year = inputDate.getFullYear();

    // Create a Day.js object for the first day of the specified month and year
    let date = dayjs(new Date(year, month, 1));
    const dates = [];

    // Iterate through the days of the month
    while (date.month() === month) {
        dates.push(date);
        date = date.add(1, 'day');
    }

    return dates;
}

export const getDayOfWeek = (day: number): string => {
    switch (day) {
        case 0:
            return "Вс";
        case 1:
            return "Пн";
        case 2:
            return "Вт";
        case 3:
            return "Ср";
        case 4:
            return "Чт";
        case 5:
            return "Пт";
        case 6:
            return "Сб";
    }
    return "";
}

export function getDatesBetween(startDate: Date, endDate: Date): dayjs.Dayjs[] {
    const dates: dayjs.Dayjs[] = [];
    const currentDate = new Date(startDate);

    while (currentDate <= endDate) {
        dates.push(dayjs(new Date(currentDate)));
        currentDate.setDate(currentDate.getDate() + 1);
    }

    return dates;
}

export const isWeekend = (day: number) => (day === 6) || (day === 0);