import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

/**
 * typeSelectPanel.jsx
 *
 * @Author: apple
 * @Date: 2017/12/14 13:20
 */
import React from 'react';
import moment from 'moment';
import ChevronLeft from '../icons/internals/ChevronLeft';
import ChevronRight from '../icons/internals/ChevronRight';
import Button, { IconButton } from '../button';
var sign = {
  prev: -1,
  next: 1
};
var nextType = {
  time: 'date',
  date: 'month',
  month: 'year',
  year: 'year'
};

function getTitle(date, type) {
  if (type === 'time') {
    return date.format('YYYY-MM-DD');
  }

  if (type === 'date') {
    return date.format('YYYY-MM');
  }

  if (type === 'month') {
    return date.format('YYYY');
  }

  var currentYear = date.year();
  var startOfDecade = Math.floor(currentYear / 10) * 10;
  var endOfDecade = Math.floor(currentYear / 10 + 1) * 10;
  return "".concat(startOfDecade, " - ").concat(endOfDecade);
}
/**
 * @ignore
 * */


var TypeSelect =
/*#__PURE__*/
function (_React$PureComponent) {
  _inherits(TypeSelect, _React$PureComponent);

  function TypeSelect() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, TypeSelect);

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

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

    _defineProperty(_assertThisInitialized(_this), "handlePanelDateChange", function (type) {
      var _this$props = _this.props,
          onPanelDateChange = _this$props.onPanelDateChange,
          panelDate = _this$props.panelDate,
          panelType = _this$props.panelType;
      var newDate = moment(panelDate);

      if (panelType === 'date') {
        newDate.add(sign[type], 'month');
      } else if (panelType === 'month') {
        newDate.add(sign[type], 'year');
      } else if (panelType === 'year') {
        newDate.add(sign[type] * 10, 'year');
      }

      onPanelDateChange && onPanelDateChange(newDate);
    });

    return _this;
  }

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

      var _this$props2 = this.props,
          onPanelTypeChange = _this$props2.onPanelTypeChange,
          panelType = _this$props2.panelType,
          prefixCls = _this$props2.prefixCls,
          panelDate = _this$props2.panelDate,
          disabledMonth = _this$props2.disabledMonth,
          disabledYear = _this$props2.disabledYear,
          rangeType = _this$props2.rangeType;
      var headPrefixCls = "".concat(prefixCls, "-head");
      var title = getTitle(panelDate, panelType);
      var disabledCondition = rangeType;

      if (disabledCondition && panelType === 'date') {
        disabledCondition = disabledMonth;
      } else if (disabledCondition && (panelType === 'month' || panelType === 'year')) {
        disabledCondition = disabledYear;
      } else {
        disabledCondition = null;
      }

      var prevDisabled = disabledCondition && disabledCondition(panelType === 'date' ? moment(panelDate).subtract(1, 'month').startOf('month') : panelType === 'month' ? moment(panelDate).subtract(1, 'year').startOf('year') : moment(panelDate).subtract(panelDate.year() % 10 + 1, 'year').startOf('year'));
      var nextDisabled = disabledCondition && disabledCondition(panelType === 'date' ? moment(panelDate).add(1, 'month').startOf('month') : panelType === 'month' ? moment(panelDate).add(1, 'year').startOf('year') : moment(panelDate).add(10 - panelDate.year() % 10, 'year').startOf('year'));
      var Title = panelType === 'year' ? 'span' : Button;
      return React.createElement("div", {
        className: headPrefixCls
      }, React.createElement(IconButton, {
        disabled: prevDisabled || panelType === 'time',
        className: "".concat(prefixCls, "-prev"),
        variant: "text",
        onClick: function onClick() {
          return _this2.handlePanelDateChange('prev');
        },
        ref: function ref(prevBtn) {
          return _this2.prevBtn = prevBtn;
        }
      }, React.createElement(ChevronLeft, null)), React.createElement(Title, {
        variant: "text",
        className: "".concat(headPrefixCls, "-title ").concat(headPrefixCls, "-").concat(panelType, "-title"),
        onClick: function onClick(e) {
          e.stopPropagation();
          onPanelTypeChange && onPanelTypeChange(nextType[panelType]);
        }
      }, title), React.createElement(IconButton, {
        disabled: nextDisabled || panelType === 'time',
        className: "".concat(prefixCls, "-next"),
        variant: "text",
        onClick: function onClick() {
          return _this2.handlePanelDateChange('next');
        }
      }, React.createElement(ChevronRight, null)));
    }
  }]);

  return TypeSelect;
}(React.PureComponent);

export { TypeSelect as default };