"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

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

var _enum = require("../_util/enum");

var _select = _interopRequireDefault(require("../select"));

var _radio = require("../radio");

var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));

var Option = _select["default"].Option;

var Header = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(Header, _Component);

  var _super = (0, _createSuper2["default"])(Header);

  function Header() {
    var _this;

    (0, _classCallCheck2["default"])(this, Header);
    _this = _super.apply(this, arguments);

    _this.onYearChange = function (year) {
      var _this$props = _this.props,
          value = _this$props.value,
          validRange = _this$props.validRange;
      var newValue = value.clone();
      newValue.year(parseInt(year, 10)); // switch the month so that it remains within range when year changes

      if (validRange) {
        var _validRange = (0, _slicedToArray2["default"])(validRange, 2),
            start = _validRange[0],
            end = _validRange[1];

        var newYear = newValue.get('year');
        var newMonth = newValue.get('month');

        if (newYear === end.get('year') && newMonth > end.get('month')) {
          newValue.month(end.get('month'));
        }

        if (newYear === start.get('year') && newMonth < start.get('month')) {
          newValue.month(start.get('month'));
        }
      }

      var onValueChange = _this.props.onValueChange;

      if (onValueChange) {
        onValueChange(newValue);
      }
    };

    _this.onMonthChange = function (month) {
      var _this$props2 = _this.props,
          onValueChange = _this$props2.onValueChange,
          value = _this$props2.value;
      var newValue = value.clone();
      newValue.month(parseInt(month, 10));

      if (onValueChange) {
        onValueChange(newValue);
      }
    };

    _this.onTypeChange = function (e) {
      var onTypeChange = _this.props.onTypeChange;

      if (onTypeChange) {
        onTypeChange(e.target.value);
      }
    };

    _this.getCalenderHeaderNode = function (node) {
      _this.calenderHeaderNode = node;
    };

    return _this;
  }

  (0, _createClass2["default"])(Header, [{
    key: "getYearSelectElement",
    value: function getYearSelectElement(year) {
      var _this2 = this;

      var _this$props3 = this.props,
          yearSelectOffset = _this$props3.yearSelectOffset,
          yearSelectTotal = _this$props3.yearSelectTotal,
          locale = _this$props3.locale,
          prefixCls = _this$props3.prefixCls,
          selectProps = _this$props3.selectProps,
          fullscreen = _this$props3.fullscreen,
          validRange = _this$props3.validRange;
      var start = year - yearSelectOffset;
      var end = start + yearSelectTotal;

      if (validRange) {
        start = validRange[0].get('year');
        end = validRange[1].get('year') + 1;
      }

      var suffix = locale.year === '年' ? '年' : '';
      var options = [];

      for (var index = start; index < end; index++) {
        options.push( /*#__PURE__*/_react["default"].createElement(Option, {
          key: "".concat(index)
        }, index + suffix));
      }

      return /*#__PURE__*/_react["default"].createElement(_select["default"], (0, _extends2["default"])({}, selectProps, {
        size: fullscreen ? _enum.Size["default"] : _enum.Size.small,
        dropdownMatchSelectWidth: false,
        className: "".concat(prefixCls, "-year-select"),
        onChange: this.onYearChange,
        value: String(year),
        getPopupContainer: function getPopupContainer() {
          return _this2.calenderHeaderNode;
        }
      }), options);
    }
  }, {
    key: "getMonthsLocale",
    value: function getMonthsLocale(value) {
      var current = value.clone();
      var localeData = value.localeData();
      var months = [];

      for (var i = 0; i < 12; i++) {
        current.month(i);
        months.push(localeData.monthsShort(current));
      }

      return months;
    }
  }, {
    key: "getMonthSelectElement",
    value: function getMonthSelectElement(month, months) {
      var _this3 = this;

      var props = this.props;
      var prefixCls = props.prefixCls,
          selectProps = props.selectProps,
          fullscreen = props.fullscreen,
          validRange = props.validRange,
          value = props.value;
      var options = [];
      var start = 0;
      var end = 12;

      if (validRange) {
        var _validRange2 = (0, _slicedToArray2["default"])(validRange, 2),
            rangeStart = _validRange2[0],
            rangeEnd = _validRange2[1];

        var currentYear = value.get('year');

        if (rangeEnd.get('year') === currentYear) {
          end = rangeEnd.get('month') + 1;
        } else {
          start = rangeStart.get('month');
        }
      }

      for (var index = start; index < end; index++) {
        options.push( /*#__PURE__*/_react["default"].createElement(Option, {
          key: "".concat(index)
        }, months[index]));
      }

      return /*#__PURE__*/_react["default"].createElement(_select["default"], (0, _extends2["default"])({}, selectProps, {
        size: fullscreen ? _enum.Size["default"] : _enum.Size.small,
        dropdownMatchSelectWidth: false,
        className: "".concat(prefixCls, "-month-select"),
        value: String(month),
        onChange: this.onMonthChange,
        getPopupContainer: function getPopupContainer() {
          return _this3.calenderHeaderNode;
        }
      }), options);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props4 = this.props,
          type = _this$props4.type,
          value = _this$props4.value,
          prefixCls = _this$props4.prefixCls,
          radioProps = _this$props4.radioProps,
          locale = _this$props4.locale,
          fullscreen = _this$props4.fullscreen;
      var yearSelect = this.getYearSelectElement(value.year());
      var monthSelect = type === 'date' ? this.getMonthSelectElement(value.month(), this.getMonthsLocale(value)) : null;

      var typeSwitch = /*#__PURE__*/_react["default"].createElement(_radio.Group, (0, _extends2["default"])({}, radioProps, {
        onChange: this.onTypeChange,
        value: type,
        size: fullscreen ? _enum.Size["default"] : _enum.Size.small
      }), /*#__PURE__*/_react["default"].createElement(_radio.Button, (0, _extends2["default"])({}, radioProps, {
        value: "date"
      }), locale.month), /*#__PURE__*/_react["default"].createElement(_radio.Button, (0, _extends2["default"])({}, radioProps, {
        value: "month"
      }), locale.year));

      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-header"),
        ref: this.getCalenderHeaderNode
      }, yearSelect, monthSelect, typeSwitch);
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _ConfigContext["default"];
    }
  }]);
  return Header;
}(_react.Component);

exports["default"] = Header;
Header.displayName = 'Header';
Header.defaultProps = {
  yearSelectOffset: 10,
  yearSelectTotal: 20
};
//# sourceMappingURL=Header.js.map
