"use strict";

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

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

var _arrowRight = _interopRequireDefault(require("../icon/arrow-right.js"));

var _arrowLeft = _interopRequireDefault(require("../icon/arrow-left.js"));

var _triangleDown = _interopRequireDefault(require("../icon/triangle-down.js"));

var _index = require("../menu/index.js");

var _index2 = require("../popover/index.js");

var _index3 = require("../locale/index.js");

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

var _index4 = require("./utils/index.js");

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

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

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

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || 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) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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 _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); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

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

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 } }); if (superClass) _setPrototypeOf(subClass, superClass); }

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

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

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;

function yearMonthToId(year, month) {
  return "".concat(year, "-").concat(month);
}

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

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

  function CalendarHeader() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, CalendarHeader);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(CalendarHeader)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      isMonthYearDropdownOpen: false
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleMonthChange", function (_ref2) {
      var value = _ref2.value;

      if (_this.props.onMonthChange) {
        // $FlowFixMe
        _this.props.onMonthChange({
          date: (0, _index4.setMonth)(_this.props.date, value[0].id)
        });
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleYearChange", function (_ref3) {
      var value = _ref3.value;

      if (_this.props.onYearChange) {
        // $FlowFixMe
        _this.props.onYearChange({
          date: (0, _index4.setYear)(_this.props.date, value[0].id)
        });
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "increaseMonth", function () {
      if (_this.props.onMonthChange) {
        // $FlowFixMe
        _this.props.onMonthChange({
          date: (0, _index4.addMonths)(_this.props.date, 1)
        });
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "decreaseMonth", function () {
      if (_this.props.onMonthChange) {
        // $FlowFixMe
        _this.props.onMonthChange({
          date: (0, _index4.subMonths)(_this.props.date, 1)
        });
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "renderPreviousMonthButton", function (_ref4) {
      var locale = _ref4.locale;
      var _this$props = _this.props,
          date = _this$props.date,
          _this$props$overrides = _this$props.overrides,
          overrides = _this$props$overrides === void 0 ? {} : _this$props$overrides;
      var allPrevDaysDisabled = (0, _index4.monthDisabledBefore)(date, _this.props);
      var isDisabled = false;

      if (allPrevDaysDisabled) {
        isDisabled = true;
      }

      var nextMonth = (0, _index4.subMonths)(date, 1);

      if (_this.props.minDate) {
        if ((0, _index4.isBefore)(nextMonth, _this.props.minDate)) {
          isDisabled = true;
        }
      } else {
        if ((0, _index4.getYear)(nextMonth) < MIN_YEAR) {
          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, _arrowLeft.default),
          _getOverrides4 = _slicedToArray(_getOverrides3, 2),
          PrevButtonIcon = _getOverrides4[0],
          prevButtonIconProps = _getOverrides4[1];

      var clickHandler = _this.decreaseMonth;

      if (allPrevDaysDisabled) {
        clickHandler = null;
      }

      return React.createElement(PrevButton, _extends({
        "aria-label": locale.datepicker.previousMonth,
        tabIndex: 0,
        onClick: clickHandler,
        disabled: isDisabled,
        $disabled: isDisabled
      }, prevButtonProps), React.createElement(PrevButtonIcon, _extends({
        overrides: {
          Svg: {
            style: navBtnStyle
          }
        }
      }, prevButtonIconProps)));
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "renderNextMonthButton", function (_ref5) {
      var locale = _ref5.locale;
      var _this$props2 = _this.props,
          date = _this$props2.date,
          _this$props2$override = _this$props2.overrides,
          overrides = _this$props2$override === void 0 ? {} : _this$props2$override;
      var allNextDaysDisabled = (0, _index4.monthDisabledAfter)(date, _this.props);
      var isDisabled = false;

      if (allNextDaysDisabled) {
        isDisabled = true;
      }

      var nextMonth = (0, _index4.addMonths)(date, 1);

      if (_this.props.maxDate) {
        if ((0, _index4.isAfter)(nextMonth, _this.props.maxDate)) {
          isDisabled = true;
        }
      } else {
        if ((0, _index4.getYear)(nextMonth) > MAX_YEAR) {
          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, _arrowRight.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 React.createElement(NextButton, _extends({
        "aria-label": locale.datepicker.nextMonth,
        tabIndex: 0,
        onClick: clickHandler,
        disabled: isDisabled,
        $disabled: isDisabled
      }, nextButtonProps), React.createElement(NextButtonIcon, _extends({
        overrides: {
          Svg: {
            style: navBtnStyle
          }
        }
      }, nextButtonIconProps)));
    });

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

      return false;
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "renderMonthYearDropdown", function () {
      var _this$props3 = _this.props,
          date = _this$props3.date,
          locale = _this$props3.locale,
          maxDate = _this$props3.maxDate,
          minDate = _this$props3.minDate,
          _this$props3$override = _this$props3.overrides,
          overrides = _this$props3$override === void 0 ? {} : _this$props3$override;

      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, _index2.Popover),
          _getOverrides14 = _slicedToArray(_getOverrides13, 2),
          OverriddenPopover = _getOverrides14[0],
          popoverProps = _getOverrides14[1];

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

      var menuOverrides = (0, _overrides.mergeOverrides)({
        List: {
          style: {
            height: '257px'
          }
        }
      }, // $FlowFixMe
      menuProps && menuProps.overrides); // $FlowFixMe

      menuProps.overrides = menuOverrides;
      var MONTHS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
      var maxYear = maxDate ? (0, _index4.getYear)(maxDate) : MAX_YEAR;
      var minYear = minDate ? (0, _index4.getYear)(minDate) : MIN_YEAR;
      var items = [];

      var _loop = function _loop(i) {
        MONTHS.forEach(function (month) {
          items.push({
            id: yearMonthToId(i, month),
            label: "".concat((0, _index4.getMonthInLocale)(month, locale), " ").concat(i)
          });
        });
      };

      for (var i = minYear; i <= maxYear; i++) {
        _loop(i);
      }

      var initialIndex = items.findIndex(function (item) {
        return item.id === yearMonthToId((0, _index4.getYear)(date), (0, _index4.getMonth)(date));
      });
      return React.createElement(OverriddenPopover, _extends({
        placement: "bottom",
        isOpen: _this.state.isMonthYearDropdownOpen,
        onClick: function onClick() {
          _this.setState(function (prev) {
            return {
              isMonthYearDropdownOpen: !prev.isMonthYearDropdownOpen
            };
          });
        },
        onClickOutside: function onClickOutside() {
          return _this.setState({
            isMonthYearDropdownOpen: false
          });
        },
        content: function content() {
          return React.createElement(OverriddenStatefulMenu, _extends({
            initialState: {
              highlightedIndex: initialIndex,
              isFocused: true
            },
            items: items,
            onItemSelect: function onItemSelect(_ref6) {
              var item = _ref6.item,
                  event = _ref6.event;
              event.preventDefault();

              var _idToYearMonth = idToYearMonth(item.id),
                  _idToYearMonth2 = _slicedToArray(_idToYearMonth, 2),
                  year = _idToYearMonth2[0],
                  month = _idToYearMonth2[1];

              date.setFullYear(year, month);
              _this.props.onMonthChange && _this.props.onMonthChange({
                date: date
              });
              _this.props.onYearChange && _this.props.onYearChange({
                date: date
              });

              _this.setState({
                isMonthYearDropdownOpen: false
              });
            }
          }, menuProps));
        }
      }, popoverProps), React.createElement(MonthYearSelectButton, _extends({
        onKeyUp: function onKeyUp(event) {
          if (_this.canArrowsOpenDropdown(event)) {
            _this.setState({
              isMonthYearDropdownOpen: true
            });
          }
        },
        onKeyDown: function onKeyDown(event) {
          if (_this.canArrowsOpenDropdown(event)) {
            // disables page scroll
            event.preventDefault();
          }

          if (event.key === 'Tab') {
            _this.setState({
              isMonthYearDropdownOpen: false
            });
          }
        }
      }, monthYearSelectButtonProps), "".concat((0, _index4.getMonthInLocale)((0, _index4.getMonth)(date), locale), " ").concat((0, _index4.getYear)(date)), React.createElement(MonthYearSelectIconContainer, monthYearSelectIconContainerProps, React.createElement(_triangleDown.default, null))));
    });

    return _this;
  }

  _createClass(CalendarHeader, [{
    key: "render",
    value: function render() {
      var _this2 = this;

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

      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.StyledDay),
          _getOverrides22 = _slicedToArray(_getOverrides21, 2),
          WeekdayHeader = _getOverrides22[0],
          weekdayHeaderProps = _getOverrides22[1];

      var startOfWeek = (0, _index4.getStartOfWeek)(this.props.date, this.props.locale);
      return React.createElement(_index3.LocaleContext.Consumer, null, function (locale) {
        return React.createElement(React.Fragment, null, React.createElement(CalendarHeader, calendarHeaderProps, _this2.renderPreviousMonthButton({
          locale: locale
        }), _this2.renderMonthYearDropdown(), _this2.renderNextMonthButton({
          locale: locale
        })), React.createElement(MonthHeader, _extends({
          role: "presentation"
        }, monthHeaderProps), _constants.WEEKDAYS.map(function (offset) {
          var day = (0, _index4.addDays)(startOfWeek, offset);
          return React.createElement(WeekdayHeader, _extends({
            $isHeader: true,
            key: offset
          }, weekdayHeaderProps), (0, _index4.getWeekdayMinInLocale)(day, _this2.props.locale));
        })));
      });
    }
  }]);

  return CalendarHeader;
}(React.Component);

exports.default = CalendarHeader;

_defineProperty(CalendarHeader, "defaultProps", {
  date: new Date(),
  locale: null,
  maxDate: null,
  minDate: null,
  onYearChange: function onYearChange() {},
  overrides: {}
});