import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { Component } from 'react';
import classnames from 'classnames';
var ROW = 4;
var COL = 3;

function goYear(direction) {
  var next = this.state.value.clone();
  next.add(direction, 'years');
  this.setState({
    value: next
  });
}

function chooseDecade(year, event) {
  var next = this.state.value.clone();
  next.year(year);
  next.month(this.state.value.month());
  this.props.onSelect(next);
  event.preventDefault();
}

var DecadePanel = /*#__PURE__*/function (_Component) {
  _inherits(DecadePanel, _Component);

  var _super = _createSuper(DecadePanel);

  function DecadePanel(props) {
    var _this;

    _classCallCheck(this, DecadePanel);

    _this = _super.call(this, props);
    _this.state = {
      value: props.value || props.defaultValue
    }; // bind methods

    _this.prefixCls = "".concat(props.rootPrefixCls, "-decade-panel");
    _this.nextCentury = goYear.bind(_assertThisInitialized(_this), 100);
    _this.previousCentury = goYear.bind(_assertThisInitialized(_this), -100);
    return _this;
  }

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

      var value = this.state.value;
      var locale = this.props.locale;
      var currentYear = value.year();
      var startYear = parseInt(currentYear / 100, 10) * 100;
      var preYear = startYear - 10;
      var endYear = startYear + 99;
      var decades = [];
      var index = 0;
      var prefixCls = this.prefixCls;

      for (var rowIndex = 0; rowIndex < ROW; rowIndex++) {
        decades[rowIndex] = [];

        for (var colIndex = 0; colIndex < COL; colIndex++) {
          var startDecade = preYear + index * 10;
          var endDecade = preYear + index * 10 + 9;
          decades[rowIndex][colIndex] = {
            startDecade: startDecade,
            endDecade: endDecade
          };
          index++;
        }
      }

      var decadesEls = decades.map(function (row, decadeIndex) {
        var tds = row.map(function (decadeData) {
          var _classNameMap;

          var dStartDecade = decadeData.startDecade;
          var dEndDecade = decadeData.endDecade;
          var isLast = dStartDecade < startYear;
          var isNext = dEndDecade > endYear;
          var classNameMap = (_classNameMap = {}, _defineProperty(_classNameMap, "".concat(prefixCls, "-cell"), 1), _defineProperty(_classNameMap, "".concat(prefixCls, "-selected-cell"), dStartDecade <= currentYear && currentYear <= dEndDecade), _defineProperty(_classNameMap, "".concat(prefixCls, "-last-century-cell"), isLast), _defineProperty(_classNameMap, "".concat(prefixCls, "-next-century-cell"), isNext), _classNameMap);
          var content = "".concat(dStartDecade, "-").concat(dEndDecade);
          var clickHandler;

          if (isLast) {
            clickHandler = _this2.previousCentury;
          } else if (isNext) {
            clickHandler = _this2.nextCentury;
          } else {
            clickHandler = chooseDecade.bind(_this2, dStartDecade);
          }

          return /*#__PURE__*/React.createElement("td", {
            key: dStartDecade,
            onClick: clickHandler,
            role: "gridcell",
            className: classnames(classNameMap)
          }, /*#__PURE__*/React.createElement("a", {
            className: "".concat(prefixCls, "-decade")
          }, content));
        });
        return /*#__PURE__*/React.createElement("tr", {
          key: decadeIndex,
          role: "row"
        }, tds);
      });
      return /*#__PURE__*/React.createElement("div", {
        className: this.prefixCls
      }, /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-header")
      }, /*#__PURE__*/React.createElement("a", {
        className: "".concat(prefixCls, "-prev-century-btn"),
        role: "button",
        onClick: this.previousCentury,
        title: locale.previousCentury
      }), /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-century")
      }, startYear, "-", endYear), /*#__PURE__*/React.createElement("a", {
        className: "".concat(prefixCls, "-next-century-btn"),
        role: "button",
        onClick: this.nextCentury,
        title: locale.nextCentury
      })), /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-body")
      }, /*#__PURE__*/React.createElement("table", {
        className: "".concat(prefixCls, "-table"),
        cellSpacing: "0",
        role: "grid"
      }, /*#__PURE__*/React.createElement("tbody", {
        className: "".concat(prefixCls, "-tbody")
      }, decadesEls))));
    }
  }]);

  return DecadePanel;
}(Component);

_defineProperty(DecadePanel, "defaultProps", {
  onSelect: function onSelect() {}
});

export { DecadePanel as default };
//# sourceMappingURL=DecadePanel.js.map
