"use strict";

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

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

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

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

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

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

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

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

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 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)), "handleMonthChange", function (_ref2) {
      var value = _ref2.value;

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

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

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

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

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "decreaseMonth", function () {
      _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);

      if (allPrevDaysDisabled) {
        return;
      }

      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, _index.ArrowLeft),
          _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
      }, 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);

      if (allNextDaysDisabled) {
        return;
      }

      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, _index.ArrowRight),
          _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
      }, nextButtonProps), React.createElement(NextButtonIcon, _extends({
        overrides: {
          Svg: {
            style: navBtnStyle
          }
        }
      }, nextButtonIconProps)));
    });

    return _this;
  }

  _createClass(CalendarHeader, [{
    key: "getSelectOverrides",
    value: function getSelectOverrides(_ref6) {
      var width = _ref6.width;
      return {
        ControlContainer: {
          style: function style(props) {
            var $isFocused = props.$isFocused,
                $isPseudoFocused = props.$isPseudoFocused,
                colors = props.$theme.colors;
            return {
              width: width,
              backgroundColor: $isFocused || $isPseudoFocused ? colors.primary500 : colors.primary,
              color: colors.white,
              borderColor: 'transparent'
            };
          }
        },
        IconsContainer: {
          style: {
            paddingRight: '0'
          }
        },
        SelectArrow: {
          style: function style(_ref7) {
            var colors = _ref7.$theme.colors;
            return {
              color: 'inherit'
            };
          }
        },
        ValueContainer: {
          style: function style(_ref8) {
            var sizing = _ref8.$theme.sizing;
            return {
              paddingTop: '0',
              paddingBottom: '0',
              paddingLeft: sizing.scale200,
              paddingRight: '0'
            };
          }
        },
        SingleValue: {
          style: function style(_ref9) {
            var sizing = _ref9.$theme.sizing;
            return {
              paddingTop: '0',
              paddingBottom: '0',
              paddingLeft: sizing.scale200,
              paddingRight: '0'
            };
          }
        },
        DropdownContainer: {
          style: {
            paddingTop: '0',
            paddingBottom: '0',
            paddingLeft: '0',
            paddingRight: '0'
          }
        }
      };
    }
  }, {
    key: "renderMonthDropdown",
    value: function renderMonthDropdown() {
      var _this$props3 = this.props,
          date = _this$props3.date,
          locale = _this$props3.locale,
          _this$props3$override = _this$props3.overrides,
          overrides = _this$props3$override === void 0 ? {} : _this$props3$override;

      var _getOverrides9 = (0, _overrides.getOverrides)(overrides.MonthSelect, _index2.Select),
          _getOverrides10 = _slicedToArray(_getOverrides9, 2),
          MonthSelect = _getOverrides10[0],
          monthSelectProps = _getOverrides10[1];

      var selectOverrides = (0, _overrides.mergeOverrides)(this.getSelectOverrides({
        width: '115px'
      }), // $FlowFixMe
      monthSelectProps && monthSelectProps.overrides);
      var monthOptions = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map(function (M) {
        return {
          id: M,
          label: (0, _index4.getMonthInLocale)(M, locale)
        };
      });
      return React.createElement(MonthSelect, _extends({
        clearable: false,
        value: [{
          id: (0, _index4.getMonth)(date)
        }],
        maxDropdownHeight: '300px',
        options: monthOptions,
        searchable: false
      }, monthSelectProps, {
        // Adding event handlers after customers overrides in order to
        // make sure the components functions as expected
        // We can extract the handlers from props overrides
        // and call it along with internal handlers by creating an inline handler
        onChange: this.handleMonthChange // internal and incoming overrides are merged above
        ,
        overrides: selectOverrides
      }));
    }
  }, {
    key: "renderYearDropdown",
    value: function renderYearDropdown() {
      var _this$props4 = this.props,
          date = _this$props4.date,
          minDate = _this$props4.minDate,
          maxDate = _this$props4.maxDate,
          _this$props4$override = _this$props4.overrides,
          overrides = _this$props4$override === void 0 ? {} : _this$props4$override;

      var _getOverrides11 = (0, _overrides.getOverrides)(overrides.YearSelect, _index2.Select),
          _getOverrides12 = _slicedToArray(_getOverrides11, 2),
          YearSelect = _getOverrides12[0],
          yearSelectProps = _getOverrides12[1];

      var selectOverrides = (0, _overrides.mergeOverrides)(this.getSelectOverrides({
        width: '80px'
      }), // $FlowFixMe
      yearSelectProps && yearSelectProps.overrides);
      var maxYear = maxDate ? (0, _index4.getYear)(maxDate) : 2100;
      var minYear = minDate ? (0, _index4.getYear)(minDate) : 1900;
      var yearOptions = [];

      for (var i = minYear; i <= maxYear; i++) {
        yearOptions.push({
          id: i,
          label: i
        });
      }

      return React.createElement(YearSelect, _extends({
        clearable: false,
        value: [{
          id: (0, _index4.getYear)(date)
        }],
        maxDropdownHeight: '300px',
        options: yearOptions,
        searchable: false
      }, yearSelectProps, {
        // Adding event handlers after customers overrides in order to
        // make sure the components functions as expected
        // We can extract the handlers from props overrides
        // and call it along with internal handlers by creating an inline handler
        onChange: this.handleYearChange // internal and incoming overrides are merged above
        ,
        overrides: selectOverrides
      }));
    }
  }, {
    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 _getOverrides13 = (0, _overrides.getOverrides)(overrides.CalendarHeader, _styledComponents.StyledCalendarHeader),
          _getOverrides14 = _slicedToArray(_getOverrides13, 2),
          CalendarHeader = _getOverrides14[0],
          calendarHeaderProps = _getOverrides14[1];

      return React.createElement(_index3.LocaleContext.Consumer, null, function (locale) {
        return React.createElement(CalendarHeader, calendarHeaderProps, _this2.renderPreviousMonthButton({
          locale: locale
        }), _this2.renderMonthDropdown(), _this2.renderYearDropdown(), _this2.renderNextMonthButton({
          locale: locale
        }));
      });
    }
  }]);

  return CalendarHeader;
}(React.Component);

exports.default = CalendarHeader;

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