import dayjs from 'dayjs';
import { cap, range } from '../../../utils';
import { getDayOfWeek, getWeek, getWeekdays, getWeekYear } from '../Input/utils';
export const getLocalizedDays = (calendarType, localeTag) => {
    return getWeekdays(calendarType).map(weekdayDate => {
        return [
            cap(weekdayDate.toLocaleString(localeTag, { weekday: 'long' })),
            cap(weekdayDate.toLocaleString(localeTag, { weekday: 'short' }))
        ];
    });
};
export const isValidDate = (date) => {
    return date.toString() !== 'Invalid Date';
};
export const getDaysOfMonth = (date, calendarType) => {
    const firstDayInMonth = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1));
    const startingWeekDay = getDayOfWeek(firstDayInMonth, calendarType, 'UTC');
    // 6 rows * 7 days = 42
    return range(1 - startingWeekDay, 42 - startingWeekDay).map((day) => {
        return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), day));
    });
};
export const getWeeksOfMonth = (date, calendarType) => {
    const weekdays = [];
    const year = date.getUTCFullYear();
    const month = date.getUTCMonth();
    const dayInLastWeek = new Date(Date.UTC(year, month, 36));
    const lastWeek = getWeek(dayInLastWeek, calendarType, 'UTC');
    let day;
    let currentWeek;
    let iterator = 1;
    do {
        day = new Date(Date.UTC(year, month, iterator));
        currentWeek = {
            weekNo: getWeek(day, calendarType, 'UTC'),
            weekYear: getWeekYear(day, calendarType, 'UTC')
        };
        weekdays.push(currentWeek);
        iterator += 7;
    } while (currentWeek.weekNo !== lastWeek);
    return weekdays;
};
export const getNextMonth = (date) => {
    const next = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + 1, 1));
    return next.getUTCMonth();
};
export const getPreviousMonth = (date) => {
    const prev = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth() - 1, 1));
    return prev.getUTCMonth();
};
const DAY_IN_MILLISECONDS = 86400000;
export function* getIntervalGenerator(interval) {
    const current = new Date(0);
    while (current.getTime() < DAY_IN_MILLISECONDS) {
        yield new Date(current);
        current.setTime(current.getTime() + interval);
    }
}
export const toDateTimeString = (date, mode = 'date') => {
    return date
        ? date.toLocaleString(undefined, mode === 'datetime'
            ? { dateStyle: 'medium', timeStyle: 'medium', timeZone: 'UTC' }
            : { dateStyle: 'medium', timeZone: 'UTC' })
        : undefined;
};
/**
 * Shifts date forward or backward. It sets the day of month
 * to the last day of the resulted month if it does not contain the calculated day,
 * ie. 2022-01-31 -> 2022-02-28.
 * @param currentDate a date to advance by month
 * @param shift determines whether it is the next month or the previous month
 * @returns new Date object with advanced month
 */
export const shiftDateByMonth = (currentDate, shift) => {
    return dayjs.utc(currentDate).add(shift, 'month').toDate();
};
export const getCurrentMonthYearLabel = (value, locale) => {
    return value.toLocaleString(locale, {
        month: 'long',
        year: 'numeric',
        timeZone: 'UTC'
    });
};
//# sourceMappingURL=utils.js.map