import { memo } from '../../lib/memo.js';
import { getMonthInHumanFormat } from './CalendarUtils.js';
import { themeConfig } from './config.js';
import { DayCellViewModel } from './DayCellViewModel.js';
var getCurrentYear = function (month, year) {
    if (month < 0) {
        return year - Math.ceil(-month / 12);
    }
    if (month > 11) {
        return year + Math.floor(month / 12);
    }
    return year;
};
var getCurrentMonth = function (month) {
    if (month < 0) {
        return 12 + (month % 12);
    }
    if (month > 11) {
        return month % 12;
    }
    return month;
};
var MonthViewModel = /** @class */ (function () {
    function MonthViewModel(month, year) {
        var currentYear = getCurrentYear(month, year);
        var currentMonth = getCurrentMonth(month);
        var daysCount = getMonthsDays(currentMonth, currentYear);
        var offset = getMonthOffset(currentMonth, currentYear);
        this.daysCount = daysCount;
        this.offset = offset;
        this.month = currentMonth;
        this.year = currentYear;
        this.isLastInYear = currentMonth === 11;
        this.isFirstInYear = currentMonth === 0;
        this.days = Array.from({ length: daysCount }, function (_, i) {
            var isWeekend = (i + getMonthOffset(currentMonth, currentYear)) % 7 >= 5;
            return DayCellViewModel.create(i + 1, currentMonth, currentYear, isWeekend);
        });
    }
    MonthViewModel.prototype.getHeight = function (theme) {
        var _a = themeConfig(theme), DAY_HEIGHT = _a.DAY_HEIGHT, MONTH_TITLE_OFFSET_HEIGHT = _a.MONTH_TITLE_OFFSET_HEIGHT, MONTH_BOTTOM_MARGIN = _a.MONTH_BOTTOM_MARGIN, CALENDAR_GRID_ROW_SPACING = _a.CALENDAR_GRID_ROW_SPACING;
        return getMonthHeight(this.daysCount, this.offset, DAY_HEIGHT, MONTH_TITLE_OFFSET_HEIGHT, MONTH_BOTTOM_MARGIN, CALENDAR_GRID_ROW_SPACING);
    };
    MonthViewModel.create = memo(function (month, year) { return new MonthViewModel(month, year); });
    return MonthViewModel;
}());
export { MonthViewModel };
var getMonthHeight = memo(function (daysCount, offset, dayHeight, titleHeight, marginBottom, rowSpacing) {
    var weeks = Math.ceil((daysCount + offset) / 7);
    return weeks * dayHeight + titleHeight + marginBottom + rowSpacing * 2 + rowSpacing * (weeks - 1);
});
var getMonthsDays = memo(function (month, year) { return new Date(year, getMonthInHumanFormat(month), 0).getDate(); });
var getMonthOffset = memo(function (month, year) {
    var day = new Date(year, month, 1).getDay() - 1;
    if (day === -1) {
        return 6;
    }
    return day;
});
//# sourceMappingURL=MonthViewModel.js.map