import { isSelectProcess } from "./calendarRangeHelper";
import { MONTHS, YEAR_RENDER_COUNT, I18N } from "./constants";
export const getDaysInMonth = (monthIndex, year)=>new Date(year, monthIndex + 1, 0).getDate();
export const getOffsetDayInWeek = (monthIndex, year)=>(new Date(year, monthIndex).getDay() || 7) - 1;
export const getStartYear = (year)=>Math.floor(year / YEAR_RENDER_COUNT) * YEAR_RENDER_COUNT;
export const getNextDate = (currentYear, currentMonth)=>currentMonth + 1 === MONTHS.length ? [
        currentYear + 1,
        0
    ] : [
        currentYear,
        currentMonth + 1
    ];
export const getPrevDate = (currentYear, currentMonth)=>currentMonth - 1 < 0 ? [
        Math.abs(currentYear - 1) || 0,
        11
    ] : [
        currentYear,
        currentMonth - 1
    ];
export const getDateFromValue = (date)=>{
    const state = date || new Date();
    return {
        day: date !== undefined ? state.getDate() : 0,
        monthIndex: state.getMonth(),
        year: Math.abs(state.getFullYear())
    };
};
export const getDateFromNow = ()=>{
    const nowDate = new Date();
    return {
        day: nowDate.getDate(),
        monthIndex: nowDate.getMonth(),
        year: nowDate.getFullYear()
    };
};
export const IsCurrentDay = (date, currentDay)=>{
    const { day, monthIndex: currentMonthIndex, year: currentYear } = getDateFromNow();
    return day === currentDay && date.monthIndex === currentMonthIndex && date.year === currentYear;
};
export const isSelectedDay = (date, currentDay, value)=>{
    if (!value) {
        return false;
    }
    const { day, monthIndex, year } = getDateFromValue(value);
    return day === currentDay && date.monthIndex === monthIndex && date.year === year;
};
export const isCurrentMonth = (date, monthIndex)=>{
    const { monthIndex: currentMonthIndex, year: currentYear } = getDateFromNow();
    return monthIndex === currentMonthIndex && date.year === currentYear;
};
export const isCurrentQuarter = (date, monthIndex)=>{
    const { monthIndex: currentMonthIndex, year: currentYear } = getDateFromNow();
    const currentQuarterCondition = currentMonthIndex - monthIndex <= 3 && currentMonthIndex - monthIndex > 0;
    return currentQuarterCondition && date.year === currentYear;
};
export const isSelectedMonth = (date, currentMonth, value)=>{
    if (!value) {
        return false;
    }
    const { monthIndex, year } = getDateFromValue(value);
    return currentMonth === monthIndex && date.year === year;
};
export const isCurrentYear = (year)=>{
    const { year: currentYear } = getDateFromNow();
    return year === currentYear;
};
export const isSelectedYear = (yearValue, value)=>{
    if (!value) {
        return false;
    }
    const { year } = getDateFromValue(value);
    return yearValue === year;
};
/**
 * Метод для получения набора неповторяющихся дат.
 */ export const getPropsMap = (props)=>props.reduce((acc, prop)=>{
        const { year, monthIndex, day } = getDateFromValue(prop.date);
        const key = `${year}-${monthIndex}-${day}`;
        const propList = acc.get(key) || [];
        propList.push(prop);
        return acc.set(key, propList);
    }, new Map());
/**
 * Метод проверяет, можно ли выбрать день.
 */ export const canSelectDate = ({ year, monthIndex, day }, value, disabledList)=>{
    if (!isSelectProcess(value)) {
        return true;
    }
    const hoverDate = new Date(year, monthIndex, day);
    const [startDate] = value;
    if (!disabledList?.length) {
        return true;
    }
    const offDisabledRange = disabledList.some(({ date })=>startDate < date && date < hoverDate || startDate > date && date > hoverDate);
    return !offDisabledRange;
};
/**
 * Метод для получения двумерного массива и возвращения выбранного элемента.
 */ export const getMatrix = (items, rowSize = 7)=>{
    const newItems = [
        ...items
    ];
    let selected;
    const result = newItems.reduce((acc, item, index)=>{
        if (index % rowSize === 0) {
            acc.push([]);
        }
        acc[acc.length - 1].push(item);
        if (item.isSelected) {
            selected = [
                acc.length - 1,
                index % rowSize
            ];
        }
        return acc;
    }, []);
    return [
        result,
        selected
    ];
};
export const getCalendarType = (type, locale)=>{
    switch(type){
        case 'Months':
        case 'Quarters':
            return I18N.year[locale];
        case 'Years':
            return I18N.period[locale];
        default:
            return I18N.month[locale];
    }
};
