import { getPrevMonth, getPrevYear, getNextMonth, getNextYear } from './components/calendar/utils.js';
import { subDays, getFirstDayOfMonth, getLastDayOfMonth, getIsSameDay, formatDate } from './dateUtils.js';

const today = new Date();
const yesterday = subDays(today, 1);
const prevMonth = getPrevMonth(today.getMonth());
const year = prevMonth === 11 ? today.getFullYear() - 1 : today.getFullYear();
const firstDay = getFirstDayOfMonth(year, prevMonth);
const lastDay = getLastDayOfMonth(year, prevMonth);
const CommonSelectorValue = {
    today: 'TODAY',
    yesterday: 'YESTERDAY',
    last7Days: 'LASTSEVEN',
    last28Days: 'LASTTWENTYEIGHT',
    lastMonth: 'LASTMONTH',
    custom: 'CUSTOM',
};
const commonDateSelector = {
    [CommonSelectorValue.today]: {
        label: 'Today',
        value: CommonSelectorValue.today,
        dateRange: today,
    },
    [CommonSelectorValue.yesterday]: {
        label: 'Yesterday',
        value: CommonSelectorValue.yesterday,
        dateRange: yesterday,
    },
    [CommonSelectorValue.last7Days]: {
        label: 'Last 7 Days',
        value: CommonSelectorValue.last7Days,
        dateRange: { start: subDays(yesterday, 6), end: yesterday },
    },
    [CommonSelectorValue.last28Days]: {
        label: 'Last 28 Days',
        value: CommonSelectorValue.last28Days,
        dateRange: { start: subDays(yesterday, 27), end: yesterday },
    },
    [CommonSelectorValue.lastMonth]: {
        label: 'Last Month',
        value: CommonSelectorValue.lastMonth,
        dateRange: { start: firstDay, end: lastDay },
    },
};
const commonDateSelectors = [
    commonDateSelector[CommonSelectorValue.today],
    commonDateSelector[CommonSelectorValue.yesterday],
    commonDateSelector[CommonSelectorValue.last7Days],
    commonDateSelector[CommonSelectorValue.last28Days],
    commonDateSelector[CommonSelectorValue.lastMonth],
];
const getSelectors = (selectors = []) => {
    var _a;
    const customSelector = (_a = selectors.find((s) => s.value === CommonSelectorValue.custom)) !== null && _a !== void 0 ? _a : {
        label: 'Custom',
        value: CommonSelectorValue.custom,
    };
    return [
        ...selectors.filter(({ value }) => value !== CommonSelectorValue.custom),
        customSelector,
    ];
};
const shouldUseSelectorsProp = (selectors = []) => selectors.length > 1;
const popperOptions = {
    placement: 'bottom-start',
    modifiers: [
        {
            name: 'offset',
            options: {
                offset: [0, 2],
            },
        },
        {
            name: 'preventOverflow',
            options: {
                altAxis: true,
            },
        },
    ],
};
function getInitialMonths(month, year) {
    return [
        {
            month: getPrevMonth(month),
            year: getPrevYear(month, year),
        },
        {
            month: month,
            year: year,
        },
        {
            month: getNextMonth(month),
            year: getNextYear(month, year),
        },
    ];
}
function getCurrentMonth(currentDate, selectedRange) {
    if (selectedRange) {
        if (selectedRange instanceof Date) {
            return selectedRange.getMonth();
        }
        return selectedRange.start.getMonth();
    }
    return currentDate.getMonth();
}
function formatInputValue(dateRange, locale, variant, defaultRange) {
    const isSameDay = defaultRange
        ? getIsSameDay(defaultRange.start, defaultRange.end)
        : getIsSameDay(dateRange.start, dateRange.end);
    if (isSameDay) {
        const dateString = formatDate(dateRange.start, locale, variant);
        return dateString;
    }
    const start = formatDate(dateRange.start, locale, variant);
    const end = formatDate(dateRange.end, locale, variant);
    return `${start} - ${end}`;
}
const JS_MONTHS = {
    January: 0,
    December: 11,
};

export { CommonSelectorValue, JS_MONTHS, commonDateSelector, commonDateSelectors, formatInputValue, getCurrentMonth, getInitialMonths, getSelectors, popperOptions, prevMonth, shouldUseSelectorsProp, year };
