var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import React from 'react';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { withRenderEnvironment } from '../../lib/renderEnvironment/index.js';
import { MonthView } from './MonthView.js';
import { DayCellView } from './DayCellView.js';
import * as CalendarScrollEvents from './CalendarScrollEvents.js';
import { getStyles } from './MonthView.styles.js';
import { getStyles as getCellStyles } from './DayCellView.styles.js';
var Month = /** @class */ (function (_super) {
    __extends(Month, _super);
    function Month() {
        var _this = _super !== null && _super.apply(this, arguments) || 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;
    }
    Month.prototype.shouldComponentUpdate = function (nextProps) {
        if (this.props.top !== nextProps.top) {
            return true;
        }
        return this.props.month !== nextProps.month;
    };
    Month.prototype.componentDidMount = function () {
        CalendarScrollEvents.addListener(this.closeSelects);
    };
    Month.prototype.render = function () {
        var _this = this;
        return (React.createElement(ThemeContext.Consumer, null, function (theme) {
            _this.theme = theme;
            return _this.renderMain();
        }));
    };
    Month.prototype.renderMain = function () {
        var _a = this.props, month = _a.month, top = _a.top;
        return (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()));
    };
    Month.prototype.renderCells = function () {
        return React.createElement(MonthDayGrid, { days: this.props.month.days, offset: this.props.month.offset });
    };
    return Month;
}(React.Component));
export { Month };
var DAYS_PER_WEEK = 7;
var MonthDayGrid = /** @class */ (function (_super) {
    __extends(MonthDayGrid, _super);
    function MonthDayGrid() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    MonthDayGrid.prototype.shouldComponentUpdate = function (nextProps) {
        return this.props.days !== nextProps.days;
    };
    MonthDayGrid.prototype.render = function () {
        var _this = this;
        this.styles = getStyles(this.emotion);
        this.cellStyles = getCellStyles(this.emotion);
        return (React.createElement(ThemeContext.Consumer, null, function (theme) {
            _this.theme = theme;
            return _this.renderMain();
        }));
    };
    MonthDayGrid.prototype.renderMain = function () {
        var _this = this;
        var leadingDays = Array.from({ length: this.props.offset }, function (_, i) { return (React.createElement("div", { key: "leading_".concat(i), className: _this.cellStyles.cell(_this.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 (React.createElement("div", { key: "trailing_".concat(i), className: _this.cellStyles.cell(_this.theme) })); });
        var days = this.props.days.map(function (day) {
            return React.createElement(DayCellView, { date: day, key: "".concat(day.date, ".").concat(day.month, ".").concat(day.year) });
        });
        var weeks = divideToWeeks(leadingDays.concat(days, trailingDays));
        return (React.createElement("div", { className: this.styles.monthDayGrid(this.theme) }, weeks.map(function (week, i) { return (React.createElement("div", { className: _this.styles.monthDayRow(_this.theme), key: "week_".concat(i) }, week)); })));
    };
    MonthDayGrid = __decorate([
        withRenderEnvironment
    ], MonthDayGrid);
    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;
}
//# sourceMappingURL=Month.js.map