import React__default from 'react';
import { IconDisclosureLeft } from '../../../_Icon/Icons/IconDisclosureLeft.js';
import { IconDisclosureRight } from '../../../_Icon/Icons/IconDisclosureRight.js';
import { CalendarState } from '../../store/types.js';
import { classes, tokens } from '../../Calendar.tokens.js';
import { sizeMap } from '../../store/reducer.js';
import { cx } from '../../../../utils/index.js';
import { StyledCalendarHeader, StyledNavigation, StyledDoubleHeaderWrapper, StyledHeaderDouble, StyledHeader, StyledArrows, StyledHeaderDate, StyledHeaderArrow, StyledArrow } from './CalendarHeader.styles.js';
import { MONTH_NAMES, YEAR_RENDER_COUNT, I18N } from '../../utils/constants.js';
import { getCalendarType } from '../../utils/calendarGridHelper.js';

/**
 * Компонент шапки календаря.
 */
var CalendarHeader = function CalendarHeader(_ref) {
  var _ref$type = _ref.type,
    type = _ref$type === void 0 ? 'Days' : _ref$type,
    _ref$startYear = _ref.startYear,
    startYear = _ref$startYear === void 0 ? 0 : _ref$startYear,
    size = _ref.size,
    firstDate = _ref.firstDate,
    secondDate = _ref.secondDate,
    isDouble = _ref.isDouble,
    onPrev = _ref.onPrev,
    onNext = _ref.onNext,
    onUpdateCalendarState = _ref.onUpdateCalendarState,
    locale = _ref.locale;
  var handleCalendarState = function handleCalendarState() {
    var newSize = isDouble ? sizeMap.Months["double"] : sizeMap.Months.single;
    if (type === CalendarState.Days) {
      onUpdateCalendarState === null || onUpdateCalendarState === void 0 || onUpdateCalendarState(CalendarState.Months, newSize);
    }
    if (type === CalendarState.Months || type === CalendarState.Quarters) {
      onUpdateCalendarState === null || onUpdateCalendarState === 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__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(StyledHeaderDate, null, MONTH_NAMES[locale][date.monthIndex]), /*#__PURE__*/React__default.createElement(StyledHeaderDate, null, date.year, /*#__PURE__*/React__default.createElement(StyledHeaderArrow, {
        color: "inherit",
        size: size === 'xs' ? 'xs' : 's'
      })));
    }
    if (type === CalendarState.Months || type === CalendarState.Quarters) {
      return /*#__PURE__*/React__default.createElement(StyledHeaderDate, null, date.year, /*#__PURE__*/React__default.createElement(StyledHeaderArrow, {
        color: "inherit",
        size: size === 'xs' ? 'xs' : 's'
      }));
    }
    if (type === CalendarState.Years) {
      var yearValue = secondPart ? startYear + 12 : startYear;
      return /*#__PURE__*/React__default.createElement(StyledHeaderDate, null, yearValue, "\u2014", yearValue + YEAR_RENDER_COUNT - 1, /*#__PURE__*/React__default.createElement(StyledHeaderArrow, {
        color: "inherit",
        size: size === 'xs' ? 'xs' : 's'
      }));
    }
    return '';
  };
  var currentCalendarType = getCalendarType(type, locale);
  var PreviousButton = function PreviousButton() {
    return /*#__PURE__*/React__default.createElement(StyledArrow, {
      className: cx(startYear <= 0 && classes.disabledPrevButton),
      "aria-label": "".concat(I18N.previous[locale], " ").concat(currentCalendarType),
      onClick: handlePrev
    }, /*#__PURE__*/React__default.createElement(IconDisclosureLeft, {
      color: "inherit",
      sizeCustomProperty: tokens.calendarArrowHeaderCustomSize
    }));
  };
  var NextButton = function NextButton() {
    return /*#__PURE__*/React__default.createElement(StyledArrow, {
      "aria-label": "".concat(I18N.next[locale], " ").concat(currentCalendarType),
      onClick: handleNext
    }, /*#__PURE__*/React__default.createElement(IconDisclosureRight, {
      color: "inherit",
      sizeCustomProperty: tokens.calendarArrowHeaderCustomSize
    }));
  };
  return /*#__PURE__*/React__default.createElement(StyledCalendarHeader, null, isDouble ? /*#__PURE__*/React__default.createElement(StyledNavigation, null, /*#__PURE__*/React__default.createElement(StyledDoubleHeaderWrapper, null, /*#__PURE__*/React__default.createElement(PreviousButton, null), /*#__PURE__*/React__default.createElement(StyledHeaderDouble, {
    onClick: handleCalendarState,
    "aria-live": "polite"
  }, getHeaderContent(firstDate))), /*#__PURE__*/React__default.createElement(StyledDoubleHeaderWrapper, {
    className: classes.doubleHeaderLastDateWrapper
  }, /*#__PURE__*/React__default.createElement(StyledHeaderDouble, {
    onClick: handleCalendarState,
    "aria-live": "polite"
  }, getHeaderContent(secondDate, true)), /*#__PURE__*/React__default.createElement(NextButton, null))) : /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement(StyledHeader, {
    type: "button",
    "aria-live": "polite",
    id: "id-grid-label",
    onClick: handleCalendarState,
    tabIndex: 0
  }, getHeaderContent(firstDate)), /*#__PURE__*/React__default.createElement(StyledArrows, null, /*#__PURE__*/React__default.createElement(PreviousButton, null), /*#__PURE__*/React__default.createElement(NextButton, null))));
};

export { CalendarHeader };
