'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var IconDisclosureLeft = require('../../../_Icon/Icons/IconDisclosureLeft.js');
var IconDisclosureRight = require('../../../_Icon/Icons/IconDisclosureRight.js');
var types = require('../../store/types.js');
var Calendar_tokens = require('../../Calendar.tokens.js');
var reducer = require('../../store/reducer.js');
var index = require('../../../../utils/index.js');
var CalendarHeader_styles = require('./CalendarHeader.styles.js');
var constants = require('../../utils/constants.js');
var calendarGridHelper = require('../../utils/calendarGridHelper.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

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

exports.CalendarHeader = CalendarHeader;
