"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var React = _interopRequireWildcard(require("react"));

var _chevronRight = _interopRequireDefault(require("../icon/chevron-right"));

var _chevronLeft = _interopRequireDefault(require("../icon/chevron-left"));

var _chevronDown = _interopRequireDefault(require("../icon/chevron-down"));

var _dateFnsAdapter = _interopRequireDefault(require("./utils/date-fns-adapter"));

var _dateHelpers = _interopRequireDefault(require("./utils/date-helpers"));

var _calendarHeaderHelpers = require("./utils/calendar-header-helpers");

var _menu = require("../menu");

var _popover = require("../popover");

var _locale = require("../locale");

var _themeProvider = require("../styles/theme-provider");

var _styledComponents = require("./styled-components");

var _constants = require("./constants");

var _overrides = require("../helpers/overrides");

var _focusVisible = require("../utils/focusVisible");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var navBtnStyle = function navBtnStyle(_ref) {
  var $theme = _ref.$theme;
  return {
    cursor: 'pointer'
  };
};

var MIN_YEAR = 2000;
var MAX_YEAR = 2030;
var MIN_MONTH = 0;
var MAX_MONTH = 11;
var DIRECTION = {
  NEXT: 'next',
  PREVIOUS: 'previous'
};

function idToYearMonth(id) {
  return id.split('-').map(Number);
}

var CalendarHeader = /*#__PURE__*/function (_React$Component) {
  _inherits(CalendarHeader, _React$Component);

  var _super = _createSuper(CalendarHeader);

  function CalendarHeader(props) {
    var _this;

    _classCallCheck(this, CalendarHeader);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "dateHelpers", void 0);

    _defineProperty(_assertThisInitialized(_this), "monthItems", void 0);

    _defineProperty(_assertThisInitialized(_this), "yearItems", void 0);

    _defineProperty(_assertThisInitialized(_this), "state", {
      isMonthDropdownOpen: false,
      isYearDropdownOpen: false,
      isFocusVisible: false
    });

    _defineProperty(_assertThisInitialized(_this), "getDateProp", function () {
      return _this.props.date || _this.dateHelpers.date();
    });

    _defineProperty(_assertThisInitialized(_this), "getYearItems", function () {
      var date = _this.getDateProp();

      var maxDate = _this.props.maxDate;
      var minDate = _this.props.minDate;
      var maxYear = maxDate ? _this.dateHelpers.getYear(maxDate) : MAX_YEAR;
      var minYear = minDate ? _this.dateHelpers.getYear(minDate) : MIN_YEAR;

      var selectedMonth = _this.dateHelpers.getMonth(date); // TODO: this logic can be optimized to only run when minDate / maxDate change


      _this.yearItems = Array.from({
        length: maxYear - minYear + 1
      }, function (_, i) {
        return minYear + i;
      }).map(function (year) {
        return {
          id: year.toString(),
          label: year.toString()
        };
      });
      var monthOfMaxDate = maxDate ? _this.dateHelpers.getMonth(maxDate) : MAX_MONTH;
      var monthOfMinDate = minDate ? _this.dateHelpers.getMonth(minDate) : MIN_MONTH; // Generates array like [0,1,.... monthOfMaxDate]

      var maxYearMonths = Array.from({
        length: monthOfMaxDate + 1
      }, function (x, i) {
        return i;
      }); // Generates array like [monthOfMinDate, ...., 10, 11]

      var minYearMonths = Array.from({
        length: 12 - monthOfMinDate
      }, function (x, i) {
        return i + monthOfMinDate;
      });

      if (selectedMonth > maxYearMonths[maxYearMonths.length - 1]) {
        var lastIdx = _this.yearItems.length - 1;
        _this.yearItems[lastIdx] = _objectSpread(_objectSpread({}, _this.yearItems[lastIdx]), {}, {
          disabled: true
        });
      }

      if (selectedMonth < minYearMonths[0]) {
        _this.yearItems[0] = _objectSpread(_objectSpread({}, _this.yearItems[0]), {}, {
          disabled: true
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "getMonthItems", function () {
      var date = _this.getDateProp();

      var year = _this.dateHelpers.getYear(date);

      var maxDate = _this.props.maxDate;
      var minDate = _this.props.minDate;
      var maxYear = maxDate ? _this.dateHelpers.getYear(maxDate) : MAX_YEAR;
      var minYear = minDate ? _this.dateHelpers.getYear(minDate) : MIN_YEAR;
      var monthOfMaxDate = maxDate ? _this.dateHelpers.getMonth(maxDate) : MAX_MONTH; // Generates array like [0,1,.... monthOfMaxDate]

      var maxYearMonths = Array.from({
        length: monthOfMaxDate + 1
      }, function (x, i) {
        return i;
      });
      var monthOfMinDate = minDate ? _this.dateHelpers.getMonth(minDate) : MIN_MONTH; // Generates array like [monthOfMinDate, ...., 10, 11]

      var minYearMonths = Array.from({
        length: 12 - monthOfMinDate
      }, function (x, i) {
        return i + monthOfMinDate;
      });
      var maxMinYearMonthsIntersection = maxYearMonths.filter(function (year) {
        return minYearMonths.includes(year);
      });
      var filterMonthsList = year === maxYear && year === minYear ? maxMinYearMonthsIntersection : year === maxYear ? maxYearMonths : year === minYear ? minYearMonths : null;

      var formatMonthLabel = function formatMonthLabel(month) {
        return _this.dateHelpers.getMonthInLocale(month, _this.props.locale);
      };

      _this.monthItems = (0, _calendarHeaderHelpers.getFilteredMonthItems)({
        filterMonthsList: filterMonthsList,
        formatMonthLabel: formatMonthLabel
      });
    });

    _defineProperty(_assertThisInitialized(_this), "increaseMonth", function () {
      if (_this.props.onMonthChange) {
        _this.props.onMonthChange({
          date: _this.dateHelpers.addMonths(_this.getDateProp(), // in a multi-month context, `order` is the number months ahead of
          // the root Calendar month that this CalendarHeader displays. We account
          // for this by incrementing the month by 1, less the value of `order`.
          1 - _this.props.order)
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "decreaseMonth", function () {
      if (_this.props.onMonthChange) {
        _this.props.onMonthChange({
          date: _this.dateHelpers.subMonths(_this.getDateProp(), 1)
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "isMultiMonthHorizontal", function () {
      var _this$props = _this.props,
          monthsShown = _this$props.monthsShown,
          orientation = _this$props.orientation;

      if (!monthsShown) {
        return false;
      }

      return orientation === _constants.ORIENTATION.horizontal && monthsShown > 1;
    });

    _defineProperty(_assertThisInitialized(_this), "isHiddenPaginationButton", function (direction) {
      var _this$props2 = _this.props,
          monthsShown = _this$props2.monthsShown,
          order = _this$props2.order;

      if (!!monthsShown && _this.isMultiMonthHorizontal()) {
        if (direction === DIRECTION.NEXT) {
          var isLastMonth = order === monthsShown - 1;
          return !isLastMonth;
        } else {
          var isFirstMonth = order === 0;
          return !isFirstMonth;
        }
      }

      return false;
    });

    _defineProperty(_assertThisInitialized(_this), "handleFocus", function (event) {
      if ((0, _focusVisible.isFocusVisible)(event)) {
        _this.setState({
          isFocusVisible: true
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleBlur", function (event) {
      if (_this.state.isFocusVisible !== false) {
        _this.setState({
          isFocusVisible: false
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "renderPreviousMonthButton", function (_ref2) {
      var locale = _ref2.locale,
          theme = _ref2.theme;

      var date = _this.getDateProp();

      var _this$props3 = _this.props,
          _this$props3$override = _this$props3.overrides,
          overrides = _this$props3$override === void 0 ? {} : _this$props3$override,
          density = _this$props3.density;

      var allPrevDaysDisabled = _this.dateHelpers.monthDisabledBefore(date, _this.props);

      var isDisabled = false;

      if (allPrevDaysDisabled) {
        isDisabled = true;
      }

      var nextMonth = _this.dateHelpers.subMonths(date, 1);

      var minYear = _this.props.minDate ? _this.dateHelpers.getYear(_this.props.minDate) : MIN_YEAR;

      if (_this.dateHelpers.getYear(nextMonth) < minYear) {
        isDisabled = true;
      }

      var isHidden = _this.isHiddenPaginationButton(DIRECTION.PREVIOUS);

      if (isHidden) {
        isDisabled = true;
      }

      var _getOverrides = (0, _overrides.getOverrides)(overrides.PrevButton, _styledComponents.StyledPrevButton),
          _getOverrides2 = _slicedToArray(_getOverrides, 2),
          PrevButton = _getOverrides2[0],
          prevButtonProps = _getOverrides2[1];

      var _getOverrides3 = (0, _overrides.getOverrides)(overrides.PrevButtonIcon, theme.direction === 'rtl' ? _chevronRight.default : _chevronLeft.default),
          _getOverrides4 = _slicedToArray(_getOverrides3, 2),
          PrevButtonIcon = _getOverrides4[0],
          prevButtonIconProps = _getOverrides4[1];

      var clickHandler = _this.decreaseMonth;

      if (allPrevDaysDisabled) {
        clickHandler = null;
      }

      return /*#__PURE__*/React.createElement(PrevButton, _extends({
        "aria-label": locale.datepicker.previousMonth,
        tabIndex: 0,
        onClick: clickHandler,
        disabled: isDisabled,
        $isFocusVisible: _this.state.isFocusVisible,
        type: "button",
        $disabled: isDisabled,
        $order: _this.props.order
      }, prevButtonProps), isHidden ? null : /*#__PURE__*/React.createElement(PrevButtonIcon, _extends({
        size: density === _constants.DENSITY.high ? 24 : 36,
        overrides: {
          Svg: {
            style: navBtnStyle
          }
        }
      }, prevButtonIconProps)));
    });

    _defineProperty(_assertThisInitialized(_this), "renderNextMonthButton", function (_ref3) {
      var locale = _ref3.locale,
          theme = _ref3.theme;

      var date = _this.getDateProp();

      var _this$props4 = _this.props,
          _this$props4$override = _this$props4.overrides,
          overrides = _this$props4$override === void 0 ? {} : _this$props4$override,
          density = _this$props4.density;

      var allNextDaysDisabled = _this.dateHelpers.monthDisabledAfter(date, _this.props);

      var isDisabled = false;

      if (allNextDaysDisabled) {
        isDisabled = true;
      }

      var nextMonth = _this.dateHelpers.addMonths(date, 1);

      var maxYear = _this.props.maxDate ? _this.dateHelpers.getYear(_this.props.maxDate) : MAX_YEAR;

      if (_this.dateHelpers.getYear(nextMonth) > maxYear) {
        isDisabled = true;
      }

      var isHidden = _this.isHiddenPaginationButton(DIRECTION.NEXT);

      if (isHidden) {
        isDisabled = true;
      }

      var _getOverrides5 = (0, _overrides.getOverrides)(overrides.NextButton, _styledComponents.StyledNextButton),
          _getOverrides6 = _slicedToArray(_getOverrides5, 2),
          NextButton = _getOverrides6[0],
          nextButtonProps = _getOverrides6[1];

      var _getOverrides7 = (0, _overrides.getOverrides)(overrides.NextButtonIcon, theme.direction === 'rtl' ? _chevronLeft.default : _chevronRight.default),
          _getOverrides8 = _slicedToArray(_getOverrides7, 2),
          NextButtonIcon = _getOverrides8[0],
          nextButtonIconProps = _getOverrides8[1];

      var clickHandler = _this.increaseMonth; // The other option is to always provide a click handler and let customers
      // override its functionality based on the `$allPrevDaysDisabled` prop
      // in a custom NextButton component override
      // Their options would be to render `null` or not apply the components handler
      // on click or do nothing

      if (allNextDaysDisabled) {
        clickHandler = null;
      }

      return /*#__PURE__*/React.createElement(NextButton, _extends({
        "aria-label": locale.datepicker.nextMonth,
        tabIndex: 0,
        onClick: clickHandler,
        disabled: isDisabled,
        type: "button",
        $disabled: isDisabled,
        $isFocusVisible: _this.state.isFocusVisible,
        $order: _this.props.order
      }, nextButtonProps), isHidden ? null : /*#__PURE__*/React.createElement(NextButtonIcon, _extends({
        size: density === _constants.DENSITY.high ? 24 : 36,
        overrides: {
          Svg: {
            style: navBtnStyle
          }
        }
      }, nextButtonIconProps)));
    });

    _defineProperty(_assertThisInitialized(_this), "canArrowsOpenDropdown", function (event) {
      if (!_this.state.isMonthDropdownOpen && !_this.state.isYearDropdownOpen) {
        if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
          return true;
        }
      }

      return false;
    });

    _defineProperty(_assertThisInitialized(_this), "renderMonthYearDropdown", function () {
      var date = _this.getDateProp();

      var month = _this.dateHelpers.getMonth(date);

      var year = _this.dateHelpers.getYear(date);

      var _this$props5 = _this.props,
          locale = _this$props5.locale,
          _this$props5$override = _this$props5.overrides,
          overrides = _this$props5$override === void 0 ? {} : _this$props5$override,
          density = _this$props5.density;

      var _getOverrides9 = (0, _overrides.getOverrides)(overrides.MonthYearSelectButton, _styledComponents.StyledMonthYearSelectButton),
          _getOverrides10 = _slicedToArray(_getOverrides9, 2),
          MonthYearSelectButton = _getOverrides10[0],
          monthYearSelectButtonProps = _getOverrides10[1];

      var _getOverrides11 = (0, _overrides.getOverrides)(overrides.MonthYearSelectIconContainer, _styledComponents.StyledMonthYearSelectIconContainer),
          _getOverrides12 = _slicedToArray(_getOverrides11, 2),
          MonthYearSelectIconContainer = _getOverrides12[0],
          monthYearSelectIconContainerProps = _getOverrides12[1];

      var _getOverrides13 = (0, _overrides.getOverrides)(overrides.MonthYearSelectPopover, _popover.Popover),
          _getOverrides14 = _slicedToArray(_getOverrides13, 2),
          OverriddenPopover = _getOverrides14[0],
          popoverProps = _getOverrides14[1];

      var _getOverrides15 = (0, _overrides.getOverrides)(overrides.MonthYearSelectStatefulMenu, _menu.StatefulMenu),
          _getOverrides16 = _slicedToArray(_getOverrides15, 2),
          OverriddenStatefulMenu = _getOverrides16[0],
          menuProps = _getOverrides16[1];

      menuProps.overrides = (0, _overrides.mergeOverrides)({
        List: {
          style: {
            height: 'auto',
            maxHeight: '257px'
          }
        }
      }, menuProps && menuProps.overrides);

      var initialMonthIndex = _this.monthItems.findIndex(function (month) {
        return month.id === _this.dateHelpers.getMonth(date).toString();
      });

      var initialYearIndex = _this.yearItems.findIndex(function (year) {
        return year.id === _this.dateHelpers.getYear(date).toString();
      });

      var monthTitle = "".concat(_this.dateHelpers.getMonthInLocale(_this.dateHelpers.getMonth(date), locale));
      var yearTitle = "".concat(_this.dateHelpers.getYear(date));
      return _this.isMultiMonthHorizontal() ? /*#__PURE__*/React.createElement("div", null, "".concat(monthTitle, " ").concat(yearTitle)) : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(OverriddenPopover, _extends({
        placement: "bottom",
        autoFocus: true,
        focusLock: true,
        isOpen: _this.state.isMonthDropdownOpen,
        onClick: function onClick() {
          _this.setState(function (prev) {
            return {
              isMonthDropdownOpen: !prev.isMonthDropdownOpen
            };
          });
        },
        onClickOutside: function onClickOutside() {
          return _this.setState({
            isMonthDropdownOpen: false
          });
        },
        onEsc: function onEsc() {
          return _this.setState({
            isMonthDropdownOpen: false
          });
        },
        content: function content() {
          return /*#__PURE__*/React.createElement(OverriddenStatefulMenu, _extends({
            initialState: {
              highlightedIndex: initialMonthIndex,
              isFocused: true
            },
            items: _this.monthItems,
            onItemSelect: function onItemSelect(_ref4) {
              var item = _ref4.item,
                  event = _ref4.event;
              event.preventDefault();
              var month = idToYearMonth(item.id);

              var updatedDate = _this.dateHelpers.set(date, {
                year: year,
                month: month
              });

              _this.props.onMonthChange && _this.props.onMonthChange({
                date: updatedDate
              });

              _this.setState({
                isMonthDropdownOpen: false
              });
            }
          }, menuProps));
        }
      }, popoverProps), /*#__PURE__*/React.createElement(MonthYearSelectButton, _extends({
        "aria-live": "polite",
        type: "button",
        $isFocusVisible: _this.state.isFocusVisible,
        $density: density,
        onKeyUp: function onKeyUp(event) {
          if (_this.canArrowsOpenDropdown(event)) {
            _this.setState({
              isMonthDropdownOpen: true
            });
          }
        },
        onKeyDown: function onKeyDown(event) {
          if (_this.canArrowsOpenDropdown(event)) {
            // disables page scroll
            event.preventDefault();
          }

          if (event.key === 'Tab') {
            _this.setState({
              isMonthDropdownOpen: false
            });
          }
        }
      }, monthYearSelectButtonProps), monthTitle, /*#__PURE__*/React.createElement(MonthYearSelectIconContainer, monthYearSelectIconContainerProps, /*#__PURE__*/React.createElement(_chevronDown.default, {
        title: "",
        overrides: {
          Svg: {
            props: {
              role: 'presentation'
            }
          }
        },
        size: density === _constants.DENSITY.high ? 16 : 24
      })))), /*#__PURE__*/React.createElement(OverriddenPopover, _extends({
        placement: "bottom",
        focusLock: true,
        isOpen: _this.state.isYearDropdownOpen,
        onClick: function onClick() {
          _this.setState(function (prev) {
            return {
              isYearDropdownOpen: !prev.isYearDropdownOpen
            };
          });
        },
        onClickOutside: function onClickOutside() {
          return _this.setState({
            isYearDropdownOpen: false
          });
        },
        onEsc: function onEsc() {
          return _this.setState({
            isYearDropdownOpen: false
          });
        },
        content: function content() {
          return /*#__PURE__*/React.createElement(OverriddenStatefulMenu, _extends({
            initialState: {
              highlightedIndex: initialYearIndex,
              isFocused: true
            },
            items: _this.yearItems,
            onItemSelect: function onItemSelect(_ref5) {
              var item = _ref5.item,
                  event = _ref5.event;
              event.preventDefault();
              var year = idToYearMonth(item.id);

              var updatedDate = _this.dateHelpers.set(date, {
                year: year,
                month: month
              });

              _this.props.onYearChange && _this.props.onYearChange({
                date: updatedDate
              });

              _this.setState({
                isYearDropdownOpen: false
              });
            }
          }, menuProps));
        }
      }, popoverProps), /*#__PURE__*/React.createElement(MonthYearSelectButton, _extends({
        "aria-live": "polite",
        type: "button",
        $isFocusVisible: _this.state.isFocusVisible,
        $density: density,
        onKeyUp: function onKeyUp(event) {
          if (_this.canArrowsOpenDropdown(event)) {
            _this.setState({
              isYearDropdownOpen: true
            });
          }
        },
        onKeyDown: function onKeyDown(event) {
          if (_this.canArrowsOpenDropdown(event)) {
            // disables page scroll
            event.preventDefault();
          }

          if (event.key === 'Tab') {
            _this.setState({
              isYearDropdownOpen: false
            });
          }
        }
      }, monthYearSelectButtonProps), yearTitle, /*#__PURE__*/React.createElement(MonthYearSelectIconContainer, monthYearSelectIconContainerProps, /*#__PURE__*/React.createElement(_chevronDown.default, {
        title: "",
        overrides: {
          Svg: {
            props: {
              role: 'presentation'
            }
          }
        },
        size: density === _constants.DENSITY.high ? 16 : 24
      })))));
    });

    _this.dateHelpers = new _dateHelpers.default(props.adapter);
    _this.monthItems = [];
    _this.yearItems = [];
    return _this;
  }

  _createClass(CalendarHeader, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.getYearItems();
      this.getMonthItems();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var selectedMonthDidChange = this.dateHelpers.getMonth(this.props.date) !== this.dateHelpers.getMonth(prevProps.date);
      var selectedYearDidChange = this.dateHelpers.getYear(this.props.date) !== this.dateHelpers.getYear(prevProps.date);

      if (selectedMonthDidChange) {
        // re-calculate yearItems
        this.getYearItems();
      }

      if (selectedYearDidChange) {
        // re-calculate monthItems
        this.getMonthItems();
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props6 = this.props,
          _this$props6$override = _this$props6.overrides,
          overrides = _this$props6$override === void 0 ? {} : _this$props6$override,
          density = _this$props6.density;

      var _getOverrides17 = (0, _overrides.getOverrides)(overrides.CalendarHeader, _styledComponents.StyledCalendarHeader),
          _getOverrides18 = _slicedToArray(_getOverrides17, 2),
          _CalendarHeader = _getOverrides18[0],
          calendarHeaderProps = _getOverrides18[1];

      var _getOverrides19 = (0, _overrides.getOverrides)(overrides.MonthHeader, _styledComponents.StyledMonthHeader),
          _getOverrides20 = _slicedToArray(_getOverrides19, 2),
          MonthHeader = _getOverrides20[0],
          monthHeaderProps = _getOverrides20[1];

      var _getOverrides21 = (0, _overrides.getOverrides)(overrides.WeekdayHeader, _styledComponents.StyledWeekdayHeader),
          _getOverrides22 = _slicedToArray(_getOverrides21, 2),
          WeekdayHeader = _getOverrides22[0],
          weekdayHeaderProps = _getOverrides22[1];

      var startOfWeek = this.dateHelpers.getStartOfWeek(this.getDateProp(), this.props.locale);
      return /*#__PURE__*/React.createElement(_themeProvider.ThemeContext.Consumer, null, function (theme) {
        return /*#__PURE__*/React.createElement(_locale.LocaleContext.Consumer, null, function (locale) {
          return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_CalendarHeader, _extends({}, calendarHeaderProps, {
            $density: _this2.props.density,
            onFocus: (0, _focusVisible.forkFocus)(calendarHeaderProps, _this2.handleFocus),
            onBlur: (0, _focusVisible.forkBlur)(calendarHeaderProps, _this2.handleBlur)
          }), _this2.renderPreviousMonthButton({
            locale: locale,
            theme: theme
          }), _this2.renderMonthYearDropdown(), _this2.renderNextMonthButton({
            locale: locale,
            theme: theme
          })), /*#__PURE__*/React.createElement(MonthHeader, _extends({
            role: "presentation"
          }, monthHeaderProps), _constants.WEEKDAYS.map(function (offset) {
            var day = _this2.dateHelpers.addDays(startOfWeek, offset);

            return /*#__PURE__*/React.createElement(WeekdayHeader, _extends({
              key: offset,
              alt: _this2.dateHelpers.getWeekdayInLocale(day, _this2.props.locale)
            }, weekdayHeaderProps, {
              $density: density
            }), _this2.dateHelpers.getWeekdayMinInLocale(day, _this2.props.locale));
          })));
        });
      });
    }
  }]);

  return CalendarHeader;
}(React.Component);

exports.default = CalendarHeader;

_defineProperty(CalendarHeader, "defaultProps", {
  adapter: _dateFnsAdapter.default,
  locale: null,
  maxDate: null,
  minDate: null,
  onYearChange: function onYearChange() {},
  overrides: {}
});