function _typeof(obj) { "@babel/helpers - typeof"; 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 _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) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; 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 _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 _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; } 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 { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

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

import * as React from 'react';
import Select from '../select';
import { Group, Button } from '../radio';
import { ConfigConsumer } from '../config-provider';
var Option = Select.Option;

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;
}

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

  var _super = _createSuper(Header);

  function Header() {
    var _this;

    _classCallCheck(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 = _slicedToArray(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 newValue = _this.props.value.clone();

      newValue.month(parseInt(month, 10));
      var onValueChange = _this.props.onValueChange;

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

    _this.onInternalTypeChange = function (e) {
      _this.onTypeChange(e.target.value);
    };

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

      if (onTypeChange) {
        onTypeChange(type);
      }
    };

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

    _this.getMonthYearSelections = function (getPrefixCls) {
      var _this$props2 = _this.props,
          customizePrefixCls = _this$props2.prefixCls,
          type = _this$props2.type,
          value = _this$props2.value;
      var prefixCls = getPrefixCls('fullcalendar', customizePrefixCls);

      var yearReactNode = _this.getYearSelectElement(prefixCls, value.year());

      var monthReactNode = type === 'month' ? _this.getMonthSelectElement(prefixCls, value.month(), getMonthsLocale(value)) : null;
      return {
        yearReactNode: yearReactNode,
        monthReactNode: monthReactNode
      };
    };

    _this.getTypeSwitch = function () {
      var _this$props3 = _this.props,
          _this$props3$locale = _this$props3.locale,
          locale = _this$props3$locale === void 0 ? {} : _this$props3$locale,
          type = _this$props3.type,
          fullscreen = _this$props3.fullscreen;
      var size = fullscreen ? 'default' : 'small';
      return /*#__PURE__*/React.createElement(Group, {
        onChange: _this.onInternalTypeChange,
        value: type,
        size: size
      }, /*#__PURE__*/React.createElement(Button, {
        value: "month"
      }, locale.month), /*#__PURE__*/React.createElement(Button, {
        value: "year"
      }, locale.year));
    };

    _this.headerRenderCustom = function (headerRender) {
      var _this$props4 = _this.props,
          type = _this$props4.type,
          onValueChange = _this$props4.onValueChange,
          value = _this$props4.value;
      return headerRender({
        value: value,
        type: type || 'month',
        onChange: onValueChange,
        onTypeChange: _this.onTypeChange
      });
    };

    _this.renderHeader = function (_ref) {
      var getPrefixCls = _ref.getPrefixCls;
      var _this$props5 = _this.props,
          prefixCls = _this$props5.prefixCls,
          headerRender = _this$props5.headerRender;

      var typeSwitch = _this.getTypeSwitch();

      var _this$getMonthYearSel = _this.getMonthYearSelections(getPrefixCls),
          yearReactNode = _this$getMonthYearSel.yearReactNode,
          monthReactNode = _this$getMonthYearSel.monthReactNode;

      return headerRender ? _this.headerRenderCustom(headerRender) : /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-header"),
        ref: _this.getCalenderHeaderNode
      }, yearReactNode, monthReactNode, typeSwitch);
    };

    return _this;
  }

  _createClass(Header, [{
    key: "getYearSelectElement",
    value: function getYearSelectElement(prefixCls, year) {
      var _this2 = this;

      var _this$props6 = this.props,
          yearSelectOffset = _this$props6.yearSelectOffset,
          yearSelectTotal = _this$props6.yearSelectTotal,
          _this$props6$locale = _this$props6.locale,
          locale = _this$props6$locale === void 0 ? {} : _this$props6$locale,
          fullscreen = _this$props6.fullscreen,
          validRange = _this$props6.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.createElement(Option, {
          key: "".concat(index)
        }, index + suffix));
      }

      return /*#__PURE__*/React.createElement(Select, {
        size: fullscreen ? 'default' : 'small',
        dropdownMatchSelectWidth: false,
        className: "".concat(prefixCls, "-year-select"),
        onChange: this.onYearChange,
        value: String(year),
        getPopupContainer: function getPopupContainer() {
          return _this2.calenderHeaderNode;
        }
      }, options);
    }
  }, {
    key: "getMonthSelectElement",
    value: function getMonthSelectElement(prefixCls, month, months) {
      var _this3 = this;

      var _this$props7 = this.props,
          fullscreen = _this$props7.fullscreen,
          validRange = _this$props7.validRange,
          value = _this$props7.value;
      var options = [];
      var start = 0;
      var end = 12;

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

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

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

        if (rangeStart.get('year') === currentYear) {
          start = rangeStart.get('month');
        }
      }

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

      return /*#__PURE__*/React.createElement(Select, {
        size: fullscreen ? 'default' : '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() {
      return /*#__PURE__*/React.createElement(ConfigConsumer, null, this.renderHeader);
    }
  }]);

  return Header;
}(React.Component);

export { Header as default };
Header.defaultProps = {
  yearSelectOffset: 10,
  yearSelectTotal: 20
};