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 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) {
  _inherits(YearPanel, _Component);

  var _super = _createSuper(YearPanel);

  function YearPanel(props) {
    var _this;

    _classCallCheck(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(_assertThisInitialized(_this), 10);
    _this.previousDecade = goYear.bind(_assertThisInitialized(_this), -10);
    return _this;
  }

  _createClass(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 = {}, _defineProperty(_classNameMap, "".concat(prefixCls, "-cell"), 1), _defineProperty(_classNameMap, "".concat(prefixCls, "-selected-cell"), yearData.year === currentYear), _defineProperty(_classNameMap, "".concat(prefixCls, "-last-decade-cell"), yearData.year < startYear), _defineProperty(_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.createElement("td", {
            role: "gridcell",
            title: yearData.title,
            key: yearData.content,
            onClick: clickHandler,
            className: classnames(classNameMap)
          }, /*#__PURE__*/React.createElement("a", {
            className: "".concat(prefixCls, "-year")
          }, yearData.content));
        });
        return /*#__PURE__*/React.createElement("tr", {
          key: index,
          role: "row"
        }, tds);
      });
      return /*#__PURE__*/React.createElement("div", {
        className: this.prefixCls
      }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-header")
      }, /*#__PURE__*/React.createElement("a", {
        className: "".concat(prefixCls, "-prev-decade-btn"),
        role: "button",
        onClick: this.previousDecade,
        title: locale.previousDecade
      }), /*#__PURE__*/React.createElement("a", {
        className: "".concat(prefixCls, "-decade-select"),
        role: "button",
        onClick: props.onDecadePanelShow,
        title: locale.decadeSelect
      }, /*#__PURE__*/React.createElement("span", {
        className: "".concat(prefixCls, "-decade-select-content")
      }, startYear, "-", endYear), /*#__PURE__*/React.createElement("span", {
        className: "".concat(prefixCls, "-decade-select-arrow")
      }, "x")), /*#__PURE__*/React.createElement("a", {
        className: "".concat(prefixCls, "-next-decade-btn"),
        role: "button",
        onClick: this.nextDecade,
        title: locale.nextDecade
      })), /*#__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")
      }, yeasEls)))));
    }
  }]);

  return YearPanel;
}(Component);

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

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