import React from "react";
import { IconDisclosureLeft, IconDisclosureRight } from "../../../_Icon";
import { CalendarState } from "../../store/types";
import { getCalendarType, MONTH_NAMES, YEAR_RENDER_COUNT, I18N } from "../../utils";
import { classes, tokens } from "../../Calendar.tokens";
import { sizeMap } from "../../store/reducer";
import { cx } from "../../../../utils";
import { StyledArrow, StyledArrows, StyledCalendarHeader, StyledDoubleHeaderWrapper, StyledHeader, StyledHeaderDate, StyledHeaderDouble, StyledNavigation, StyledHeaderArrow } from "./CalendarHeader.styles";
/**
 * Компонент шапки календаря.
 */ export const CalendarHeader = ({ type = 'Days', startYear = 0, size, firstDate, secondDate, isDouble, onPrev, onNext, onUpdateCalendarState, locale })=>{
    const handleCalendarState = ()=>{
        const newSize = isDouble ? sizeMap.Months.double : sizeMap.Months.single;
        if (type === CalendarState.Days) {
            onUpdateCalendarState?.(CalendarState.Months, newSize);
        }
        if (type === CalendarState.Months || type === CalendarState.Quarters) {
            onUpdateCalendarState?.(CalendarState.Years, newSize);
        }
    };
    const handlePrev = (event)=>{
        event.stopPropagation();
        if (startYear <= 0) {
            return;
        }
        onPrev();
    };
    const handleNext = (event)=>{
        event.stopPropagation();
        onNext();
    };
    const getHeaderContent = (date, secondPart)=>{
        if (!date) {
            return '';
        }
        if (type === CalendarState.Days) {
            return /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(StyledHeaderDate, null, MONTH_NAMES[locale][date.monthIndex]), /*#__PURE__*/ React.createElement(StyledHeaderDate, null, date.year, /*#__PURE__*/ React.createElement(StyledHeaderArrow, {
                color: "inherit",
                size: size === 'xs' ? 'xs' : 's'
            })));
        }
        if (type === CalendarState.Months || type === CalendarState.Quarters) {
            return /*#__PURE__*/ React.createElement(StyledHeaderDate, null, date.year, /*#__PURE__*/ React.createElement(StyledHeaderArrow, {
                color: "inherit",
                size: size === 'xs' ? 'xs' : 's'
            }));
        }
        if (type === CalendarState.Years) {
            const yearValue = secondPart ? startYear + 12 : startYear;
            return /*#__PURE__*/ React.createElement(StyledHeaderDate, null, yearValue, "—", yearValue + YEAR_RENDER_COUNT - 1, /*#__PURE__*/ React.createElement(StyledHeaderArrow, {
                color: "inherit",
                size: size === 'xs' ? 'xs' : 's'
            }));
        }
        return '';
    };
    const currentCalendarType = getCalendarType(type, locale);
    const PreviousButton = ()=>/*#__PURE__*/ React.createElement(StyledArrow, {
            className: cx(startYear <= 0 && classes.disabledPrevButton),
            "aria-label": `${I18N.previous[locale]} ${currentCalendarType}`,
            onClick: handlePrev
        }, /*#__PURE__*/ React.createElement(IconDisclosureLeft, {
            color: "inherit",
            sizeCustomProperty: tokens.calendarArrowHeaderCustomSize
        }));
    const NextButton = ()=>/*#__PURE__*/ React.createElement(StyledArrow, {
            "aria-label": `${I18N.next[locale]} ${currentCalendarType}`,
            onClick: handleNext
        }, /*#__PURE__*/ React.createElement(IconDisclosureRight, {
            color: "inherit",
            sizeCustomProperty: tokens.calendarArrowHeaderCustomSize
        }));
    return /*#__PURE__*/ React.createElement(StyledCalendarHeader, null, isDouble ? /*#__PURE__*/ React.createElement(StyledNavigation, null, /*#__PURE__*/ React.createElement(StyledDoubleHeaderWrapper, null, /*#__PURE__*/ React.createElement(PreviousButton, null), /*#__PURE__*/ React.createElement(StyledHeaderDouble, {
        onClick: handleCalendarState,
        "aria-live": "polite"
    }, getHeaderContent(firstDate))), /*#__PURE__*/ React.createElement(StyledDoubleHeaderWrapper, {
        className: classes.doubleHeaderLastDateWrapper
    }, /*#__PURE__*/ React.createElement(StyledHeaderDouble, {
        onClick: handleCalendarState,
        "aria-live": "polite"
    }, getHeaderContent(secondDate, true)), /*#__PURE__*/ React.createElement(NextButton, null))) : /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(StyledHeader, {
        type: "button",
        "aria-live": "polite",
        id: "id-grid-label",
        onClick: handleCalendarState,
        tabIndex: 0
    }, getHeaderContent(firstDate)), /*#__PURE__*/ React.createElement(StyledArrows, null, /*#__PURE__*/ React.createElement(PreviousButton, null), /*#__PURE__*/ React.createElement(NextButton, null))));
};
