"use strict";var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.Month = void 0;var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));var _react = _interopRequireDefault(require("react"));

var _ThemeContext = require("../../lib/theming/ThemeContext");





var _MonthView = require("./MonthView");
var _DayCellView = require("./DayCellView");
var CalendarScrollEvents = _interopRequireWildcard(require("./CalendarScrollEvents"));
var _MonthView2 = require("./MonthView.styles");
var _DayCellView2 = require("./DayCellView.styles");var







Month = exports.Month = /*#__PURE__*/function (_React$Component) {function Month() {var _this;for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;_this.


    monthSelect = null;_this.
    yearSelect = null;_this.

















































    closeSelects = function () {
      if (_this.monthSelect) {
        _this.monthSelect.close();
      }
      if (_this.yearSelect) {
        _this.yearSelect.close();
      }
    };_this.

    monthRef = function (monthSelect) {
      _this.monthSelect = monthSelect;
    };_this.

    yearRef = function (yearSelect) {
      _this.yearSelect = yearSelect;
    };_this.

    handleMonthSelect = function (month) {
      _this.props.onMonthYearChange(month, _this.props.month.year);
    };_this.

    handleYearSelect = function (year) {
      _this.props.onMonthYearChange(_this.props.month.month, year);
    };return _this;}(0, _inheritsLoose2.default)(Month, _React$Component);var _proto = Month.prototype;_proto.shouldComponentUpdate = function shouldComponentUpdate(nextProps) {if (this.props.top !== nextProps.top) {return true;}return this.props.month !== nextProps.month;};_proto.componentDidMount = function componentDidMount() {CalendarScrollEvents.addListener(this.closeSelects);};_proto.render = function render() {var _this2 = this;return /*#__PURE__*/_react.default.createElement(_ThemeContext.ThemeContext.Consumer, null, function (theme) {_this2.theme = theme;return _this2.renderMain();});};_proto.renderMain = function renderMain() {var _this$props = this.props,month = _this$props.month,top = _this$props.top;return /*#__PURE__*/_react.default.createElement(_MonthView.MonthView, { firstDayOffset: month.offset, height: month.getHeight(this.theme), isFirstInYear: month.isFirstInYear, isLastInYear: month.isLastInYear, month: month.month, top: top, year: month.year, onMonthSelect: this.handleMonthSelect, onYearSelect: this.handleYearSelect, monthSelectRef: this.monthRef, yearSelectRef: this.yearRef }, this.renderCells());};_proto.renderCells = function renderCells() {return /*#__PURE__*/_react.default.createElement(MonthDayGrid, { days: this.props.month.days, offset: this.props.month.offset });};return Month;}(_react.default.Component);


var DAYS_PER_WEEK = 7;var






MonthDayGrid = /*#__PURE__*/function (_React$Component2) {function MonthDayGrid() {return _React$Component2.apply(this, arguments) || this;}(0, _inheritsLoose2.default)(MonthDayGrid, _React$Component2);var _proto2 = MonthDayGrid.prototype;_proto2.


  shouldComponentUpdate = function shouldComponentUpdate(nextProps) {
    return this.props.days !== nextProps.days;
  };_proto2.

  render = function render() {var _this3 = this;
    return /*#__PURE__*/(
      _react.default.createElement(_ThemeContext.ThemeContext.Consumer, null,
      function (theme) {
        _this3.theme = theme;
        return _this3.renderMain();
      }
      ));

  };_proto2.

  renderMain = function renderMain() {var _this4 = this;
    var leadingDays = Array.from({ length: this.props.offset }, function (_, i) {return /*#__PURE__*/(
        _react.default.createElement("div", { key: "leading_" + i, className: _DayCellView2.styles.cell(_this4.theme) }));}
    );
    var trailingOffset = DAYS_PER_WEEK - (this.props.offset + this.props.days.length) % DAYS_PER_WEEK;
    var trailingDays = Array.from({ length: trailingOffset }, function (_, i) {return /*#__PURE__*/(
        _react.default.createElement("div", { key: "trailing_" + i, className: _DayCellView2.styles.cell(_this4.theme) }));}
    );
    var days = this.props.days.map(function (day) {
      return /*#__PURE__*/_react.default.createElement(_DayCellView.DayCellView, { date: day, key: day.date + "." + day.month + "." + day.year });
    });
    var weeks = divideToWeeks(leadingDays.concat(days, trailingDays));
    return /*#__PURE__*/(
      _react.default.createElement("div", { className: _MonthView2.styles.monthDayGrid(this.theme) },
      weeks.map(function (week, i) {return /*#__PURE__*/(
          _react.default.createElement("div", { className: _MonthView2.styles.monthDayRow(_this4.theme), key: "week_" + i },
          week
          ));}
      )
      ));

  };return MonthDayGrid;}(_react.default.Component);


function divideToWeeks(days) {
  var weeks = [];
  for (var i = 0; i < days.length; i += DAYS_PER_WEEK) {
    var week = days.slice(i, i + DAYS_PER_WEEK);
    weeks.push(week);
  }
  return weeks;
}