import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
import React from 'react';
import { ThemeContext } from "../../../lib/theming/ThemeContext";
import { MonthView } from "../MonthView";
import { DayCellView } from "../DayCellView";
import * as CalendarScrollEvents from "../CalendarScrollEvents";
import { styles } from "../MonthView.styles";
import { styles as cellStyles } from "../DayCellView.styles";
export var 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;
  }
  _inheritsLoose(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.createElement(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.createElement(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.createElement(MonthDayGrid, {
      days: this.props.month.days,
      offset: this.props.month.offset
    });
  };
  return Month;
}(React.Component);
var DAYS_PER_WEEK = 7;
var MonthDayGrid = /*#__PURE__*/function (_React$Component2) {
  function MonthDayGrid() {
    return _React$Component2.apply(this, arguments) || this;
  }
  _inheritsLoose(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.createElement(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.createElement("div", {
          key: "leading_" + i,
          className: cellStyles.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.createElement("div", {
          key: "trailing_" + i,
          className: cellStyles.cell(_this4.theme)
        })
      );
    });
    var days = this.props.days.map(function (day) {
      return /*#__PURE__*/React.createElement(DayCellView, {
        date: day,
        key: day.date + "." + day.month + "." + day.year
      });
    });
    var weeks = divideToWeeks(leadingDays.concat(days, trailingDays));
    return /*#__PURE__*/(
      React.createElement("div", {
        className: styles.monthDayGrid(this.theme)
      }, weeks.map(function (week, i) {
        return /*#__PURE__*/(
          React.createElement("div", {
            className: styles.monthDayRow(_this4.theme),
            key: "week_" + i
          }, week)
        );
      }))
    );
  };
  return MonthDayGrid;
}(React.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;
}