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 var CalendarHeader = function CalendarHeader(param) {
    var _param_type = param.type, type = _param_type === void 0 ? 'Days' : _param_type, _param_startYear = param.startYear, startYear = _param_startYear === void 0 ? 0 : _param_startYear, size = param.size, firstDate = param.firstDate, secondDate = param.secondDate, isDouble = param.isDouble, onPrev = param.onPrev, onNext = param.onNext, onUpdateCalendarState = param.onUpdateCalendarState, locale = param.locale;
    var handleCalendarState = function handleCalendarState() {
        var newSize = isDouble ? sizeMap.Months.double : sizeMap.Months.single;
        if (type === CalendarState.Days) {
            onUpdateCalendarState === null || onUpdateCalendarState === void 0 ? void 0 : onUpdateCalendarState(CalendarState.Months, newSize);
        }
        if (type === CalendarState.Months || type === CalendarState.Quarters) {
            onUpdateCalendarState === null || onUpdateCalendarState === void 0 ? void 0 : onUpdateCalendarState(CalendarState.Years, newSize);
        }
    };
    var handlePrev = function handlePrev(event) {
        event.stopPropagation();
        if (startYear <= 0) {
            return;
        }
        onPrev();
    };
    var handleNext = function handleNext(event) {
        event.stopPropagation();
        onNext();
    };
    var getHeaderContent = function 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) {
            var 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 '';
    };
    var currentCalendarType = getCalendarType(type, locale);
    var PreviousButton = function PreviousButton() {
        return /*#__PURE__*/ React.createElement(StyledArrow, {
            className: cx(startYear <= 0 && classes.disabledPrevButton),
            "aria-label": "".concat(I18N.previous[locale], " ").concat(currentCalendarType),
            onClick: handlePrev
        }, /*#__PURE__*/ React.createElement(IconDisclosureLeft, {
            color: "inherit",
            sizeCustomProperty: tokens.calendarArrowHeaderCustomSize
        }));
    };
    var NextButton = function NextButton() {
        return /*#__PURE__*/ React.createElement(StyledArrow, {
            "aria-label": "".concat(I18N.next[locale], " ").concat(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))));
};
