"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 _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

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

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

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

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

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _classnames = _interopRequireDefault(require("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) {
  (0, _inherits2["default"])(DecadePanel, _Component);

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

  function DecadePanel(props) {
    var _this;

    (0, _classCallCheck2["default"])(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((0, _assertThisInitialized2["default"])(_this), 100);
    _this.previousCentury = goYear.bind((0, _assertThisInitialized2["default"])(_this), -100);
    return _this;
  }

  (0, _createClass2["default"])(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 = {}, (0, _defineProperty2["default"])(_classNameMap, "".concat(prefixCls, "-cell"), 1), (0, _defineProperty2["default"])(_classNameMap, "".concat(prefixCls, "-selected-cell"), dStartDecade <= currentYear && currentYear <= dEndDecade), (0, _defineProperty2["default"])(_classNameMap, "".concat(prefixCls, "-last-century-cell"), isLast), (0, _defineProperty2["default"])(_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["default"].createElement("td", {
            key: dStartDecade,
            onClick: clickHandler,
            role: "gridcell",
            className: (0, _classnames["default"])(classNameMap)
          }, /*#__PURE__*/_react["default"].createElement("a", {
            className: "".concat(prefixCls, "-decade")
          }, content));
        });
        return /*#__PURE__*/_react["default"].createElement("tr", {
          key: decadeIndex,
          role: "row"
        }, tds);
      });
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: this.prefixCls
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-header")
      }, /*#__PURE__*/_react["default"].createElement("a", {
        className: "".concat(prefixCls, "-prev-century-btn"),
        role: "button",
        onClick: this.previousCentury,
        title: locale.previousCentury
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-century")
      }, startYear, "-", endYear), /*#__PURE__*/_react["default"].createElement("a", {
        className: "".concat(prefixCls, "-next-century-btn"),
        role: "button",
        onClick: this.nextCentury,
        title: locale.nextCentury
      })), /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-body")
      }, /*#__PURE__*/_react["default"].createElement("table", {
        className: "".concat(prefixCls, "-table"),
        cellSpacing: "0",
        role: "grid"
      }, /*#__PURE__*/_react["default"].createElement("tbody", {
        className: "".concat(prefixCls, "-tbody")
      }, decadesEls))));
    }
  }]);
  return DecadePanel;
}(_react.Component);

exports["default"] = DecadePanel;
(0, _defineProperty2["default"])(DecadePanel, "defaultProps", {
  onSelect: function onSelect() {}
});
//# sourceMappingURL=DecadePanel.js.map
