"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 value = this.state.value.clone();
  value.add(direction, 'year');
  this.setState({
    value: value
  });
}

function chooseYear(year) {
  var value = this.state.value.clone();
  value.year(year);
  value.month(this.state.value.month());
  this.props.onSelect(value);
}

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

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

  function YearPanel(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, YearPanel);
    _this = _super.call(this, props);
    _this.prefixCls = "".concat(props.rootPrefixCls, "-year-panel");
    _this.state = {
      value: props.value || props.defaultValue
    };
    _this.nextDecade = goYear.bind((0, _assertThisInitialized2["default"])(_this), 10);
    _this.previousDecade = goYear.bind((0, _assertThisInitialized2["default"])(_this), -10);
    return _this;
  }

  (0, _createClass2["default"])(YearPanel, [{
    key: "years",
    value: function years() {
      var value = this.state.value;
      var currentYear = value.year();
      var startYear = parseInt(currentYear / 10, 10) * 10;
      var previousYear = startYear - 1;
      var years = [];
      var index = 0;

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

        for (var colIndex = 0; colIndex < COL; colIndex++) {
          var year = previousYear + index;
          var content = String(year);
          years[rowIndex][colIndex] = {
            content: content,
            year: year,
            title: content
          };
          index++;
        }
      }

      return years;
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var props = this.props;
      var value = this.state.value;
      var locale = props.locale;
      var years = this.years();
      var currentYear = value.year();
      var startYear = parseInt(currentYear / 10, 10) * 10;
      var endYear = startYear + 9;
      var prefixCls = this.prefixCls;
      var yeasEls = years.map(function (row, index) {
        var tds = row.map(function (yearData) {
          var _classNameMap;

          var classNameMap = (_classNameMap = {}, (0, _defineProperty2["default"])(_classNameMap, "".concat(prefixCls, "-cell"), 1), (0, _defineProperty2["default"])(_classNameMap, "".concat(prefixCls, "-selected-cell"), yearData.year === currentYear), (0, _defineProperty2["default"])(_classNameMap, "".concat(prefixCls, "-last-decade-cell"), yearData.year < startYear), (0, _defineProperty2["default"])(_classNameMap, "".concat(prefixCls, "-next-decade-cell"), yearData.year > endYear), _classNameMap);
          var clickHandler;

          if (yearData.year < startYear) {
            clickHandler = _this2.previousDecade;
          } else if (yearData.year > endYear) {
            clickHandler = _this2.nextDecade;
          } else {
            clickHandler = chooseYear.bind(_this2, yearData.year);
          }

          return /*#__PURE__*/_react["default"].createElement("td", {
            role: "gridcell",
            title: yearData.title,
            key: yearData.content,
            onClick: clickHandler,
            className: (0, _classnames["default"])(classNameMap)
          }, /*#__PURE__*/_react["default"].createElement("a", {
            className: "".concat(prefixCls, "-year")
          }, yearData.content));
        });
        return /*#__PURE__*/_react["default"].createElement("tr", {
          key: index,
          role: "row"
        }, tds);
      });
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: this.prefixCls
      }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-header")
      }, /*#__PURE__*/_react["default"].createElement("a", {
        className: "".concat(prefixCls, "-prev-decade-btn"),
        role: "button",
        onClick: this.previousDecade,
        title: locale.previousDecade
      }), /*#__PURE__*/_react["default"].createElement("a", {
        className: "".concat(prefixCls, "-decade-select"),
        role: "button",
        onClick: props.onDecadePanelShow,
        title: locale.decadeSelect
      }, /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(prefixCls, "-decade-select-content")
      }, startYear, "-", endYear), /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(prefixCls, "-decade-select-arrow")
      }, "x")), /*#__PURE__*/_react["default"].createElement("a", {
        className: "".concat(prefixCls, "-next-decade-btn"),
        role: "button",
        onClick: this.nextDecade,
        title: locale.nextDecade
      })), /*#__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")
      }, yeasEls)))));
    }
  }]);
  return YearPanel;
}(_react.Component);

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