/**
 * DevExtreme (esm/__internal/ui/calendar/m_calendar.views.js)
 * Version: 23.2.9 (build 24294-0112)
 * Build date: Sun Oct 20 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import domAdapter from "../../../core/dom_adapter";
import $ from "../../../core/renderer";
import {
    noop
} from "../../../core/utils/common";
import dateUtils from "../../../core/utils/date";
import dateSerialization from "../../../core/utils/date_serialization";
import {
    extend
} from "../../../core/utils/extend";
import dateLocalization from "../../../localization/date";
import BaseView from "./m_calendar.base_view";
var CALENDAR_OTHER_MONTH_CLASS = "dx-calendar-other-month";
var CALENDAR_OTHER_VIEW_CLASS = "dx-calendar-other-view";
var CALENDAR_WEEK_NUMBER_CELL_CLASS = "dx-calendar-week-number-cell";
var CALENDAR_WEEK_SELECTION_CLASS = "dx-calendar-week-selection";
var Views = {
    month: BaseView.inherit({
        _getViewName: () => "month",
        _getCurrentDateFormat: () => "longdate",
        _getDefaultOptions() {
            return extend(this.callBase(), {
                firstDayOfWeek: 0,
                rowCount: 6,
                colCount: 7
            })
        },
        _renderImpl() {
            this.callBase();
            this._renderHeader()
        },
        _renderBody() {
            this.callBase();
            this._$table.find(".".concat(CALENDAR_OTHER_VIEW_CLASS)).addClass(CALENDAR_OTHER_MONTH_CLASS)
        },
        _renderFocusTarget: noop,
        getCellAriaLabel: date => dateLocalization.format(date, "longdate"),
        _renderHeader() {
            var $headerRow = $("<tr>");
            var $header = $("<thead>").append($headerRow);
            this._$table.prepend($header);
            for (var colIndex = 0, colCount = this.option("colCount"); colIndex < colCount; colIndex++) {
                this._renderHeaderCell(colIndex, $headerRow)
            }
            if (this.option("showWeekNumbers")) {
                this._renderWeekHeaderCell($headerRow)
            }
        },
        _renderHeaderCell(cellIndex, $headerRow) {
            var {
                firstDayOfWeek: firstDayOfWeek
            } = this.option();
            var {
                full: fullCaption,
                abbreviated: abbrCaption
            } = this._getDayCaption(firstDayOfWeek + cellIndex);
            var $cell = $("<th>").attr({
                scope: "col",
                abbr: fullCaption
            }).text(abbrCaption);
            $headerRow.append($cell)
        },
        _renderWeekHeaderCell($headerRow) {
            var $weekNumberHeaderCell = $("<th>").attr({
                scope: "col",
                abbr: "WeekNumber",
                class: "dx-week-number-header"
            });
            $headerRow.prepend($weekNumberHeaderCell)
        },
        _renderWeekNumberCell(rowData) {
            var {
                showWeekNumbers: showWeekNumbers,
                cellTemplate: cellTemplate,
                selectionMode: selectionMode,
                selectWeekOnClick: selectWeekOnClick
            } = this.option();
            if (!showWeekNumbers) {
                return
            }
            var weekNumber = this._getWeekNumber(rowData.prevCellDate);
            var cell = domAdapter.createElement("td");
            var $cell = $(cell);
            cell.className = CALENDAR_WEEK_NUMBER_CELL_CLASS;
            if ("single" !== selectionMode && selectWeekOnClick) {
                $cell.addClass(CALENDAR_WEEK_SELECTION_CLASS)
            }
            if (cellTemplate) {
                cellTemplate.render(this._prepareCellTemplateData(weekNumber, -1, $cell))
            } else {
                cell.innerHTML = weekNumber
            }
            rowData.row.prepend(cell);
            this.setAria({
                role: "gridcell",
                label: "Week ".concat(weekNumber)
            }, $cell)
        },
        _getWeekNumber(date) {
            var {
                weekNumberRule: weekNumberRule,
                firstDayOfWeek: firstDayOfWeek
            } = this.option();
            if ("auto" === weekNumberRule) {
                return dateUtils.getWeekNumber(date, firstDayOfWeek, 1 === firstDayOfWeek ? "firstFourDays" : "firstDay")
            }
            return dateUtils.getWeekNumber(date, firstDayOfWeek, weekNumberRule)
        },
        getNavigatorCaption() {
            return dateLocalization.format(this.option("date"), "monthandyear")
        },
        _isTodayCell(cellDate) {
            var today = this.option("_todayDate")();
            return dateUtils.sameDate(cellDate, today)
        },
        _isDateOutOfRange(cellDate) {
            var minDate = this.option("min");
            var maxDate = this.option("max");
            return !dateUtils.dateInRange(cellDate, minDate, maxDate, "date")
        },
        _isOtherView(cellDate) {
            return cellDate.getMonth() !== this.option("date").getMonth()
        },
        _isStartDayOfMonth(cellDate) {
            return dateUtils.sameDate(cellDate, dateUtils.getFirstMonthDate(this.option("date")))
        },
        _isEndDayOfMonth(cellDate) {
            return dateUtils.sameDate(cellDate, dateUtils.getLastMonthDate(this.option("date")))
        },
        _getCellText: cellDate => dateLocalization.format(cellDate, "d"),
        _getDayCaption(day) {
            var daysInWeek = this.option("colCount");
            var dayIndex = day % daysInWeek;
            return {
                full: dateLocalization.getDayNames()[dayIndex],
                abbreviated: dateLocalization.getDayNames("abbreviated")[dayIndex]
            }
        },
        _getFirstCellData() {
            var {
                firstDayOfWeek: firstDayOfWeek
            } = this.option();
            var firstDay = dateUtils.getFirstMonthDate(this.option("date"));
            var firstMonthDayOffset = firstDayOfWeek - firstDay.getDay();
            var daysInWeek = this.option("colCount");
            if (firstMonthDayOffset >= 0) {
                firstMonthDayOffset -= daysInWeek
            }
            firstDay.setDate(firstDay.getDate() + firstMonthDayOffset);
            return firstDay
        },
        _getNextCellData(date) {
            date = new Date(date);
            date.setDate(date.getDate() + 1);
            return date
        },
        _getCellByDate(date) {
            return this._$table.find("td[data-value='".concat(dateSerialization.serializeDate(date, dateUtils.getShortDateFormat()), "']"))
        },
        isBoundary(date) {
            return dateUtils.sameMonthAndYear(date, this.option("min")) || dateUtils.sameMonthAndYear(date, this.option("max"))
        },
        _getDefaultDisabledDatesHandler: disabledDates => function(args) {
            var isDisabledDate = disabledDates.some(item => dateUtils.sameDate(item, args.date));
            if (isDisabledDate) {
                return true
            }
        }
    }),
    year: BaseView.inherit({
        _getViewName: () => "year",
        _getCurrentDateFormat: () => "monthandyear",
        _isTodayCell(cellDate) {
            var today = this.option("_todayDate")();
            return dateUtils.sameMonthAndYear(cellDate, today)
        },
        _isDateOutOfRange(cellDate) {
            return !dateUtils.dateInRange(cellDate, dateUtils.getFirstMonthDate(this.option("min")), dateUtils.getLastMonthDate(this.option("max")))
        },
        _isOtherView: () => false,
        _isStartDayOfMonth: () => false,
        _isEndDayOfMonth: () => false,
        _getCellText: cellDate => dateLocalization.getMonthNames("abbreviated")[cellDate.getMonth()],
        _getFirstCellData() {
            var currentDate = this.option("date");
            var data = new Date(currentDate);
            data.setDate(1);
            data.setMonth(0);
            return data
        },
        _getNextCellData(date) {
            date = new Date(date);
            date.setMonth(date.getMonth() + 1);
            return date
        },
        _getCellByDate(date) {
            var foundDate = new Date(date);
            foundDate.setDate(1);
            return this._$table.find("td[data-value='".concat(dateSerialization.serializeDate(foundDate, dateUtils.getShortDateFormat()), "']"))
        },
        getCellAriaLabel: date => dateLocalization.format(date, "monthandyear"),
        getNavigatorCaption() {
            return dateLocalization.format(this.option("date"), "yyyy")
        },
        isBoundary(date) {
            return dateUtils.sameYear(date, this.option("min")) || dateUtils.sameYear(date, this.option("max"))
        },
        _renderWeekNumberCell: noop
    }),
    decade: BaseView.inherit({
        _getViewName: () => "decade",
        _isTodayCell(cellDate) {
            var today = this.option("_todayDate")();
            return dateUtils.sameYear(cellDate, today)
        },
        _isDateOutOfRange(cellDate) {
            var min = this.option("min");
            var max = this.option("max");
            return !dateUtils.dateInRange(cellDate.getFullYear(), min && min.getFullYear(), max && max.getFullYear())
        },
        _isOtherView(cellDate) {
            var date = new Date(cellDate);
            date.setMonth(1);
            return !dateUtils.sameDecade(date, this.option("date"))
        },
        _isStartDayOfMonth: () => false,
        _isEndDayOfMonth: () => false,
        _getCellText: cellDate => dateLocalization.format(cellDate, "yyyy"),
        _getFirstCellData() {
            var year = dateUtils.getFirstYearInDecade(this.option("date")) - 1;
            return dateUtils.createDateWithFullYear(year, 0, 1)
        },
        _getNextCellData(date) {
            date = new Date(date);
            date.setFullYear(date.getFullYear() + 1);
            return date
        },
        getNavigatorCaption() {
            var currentDate = this.option("date");
            var firstYearInDecade = dateUtils.getFirstYearInDecade(currentDate);
            var startDate = new Date(currentDate);
            var endDate = new Date(currentDate);
            startDate.setFullYear(firstYearInDecade);
            endDate.setFullYear(firstYearInDecade + 9);
            return "".concat(dateLocalization.format(startDate, "yyyy"), "-").concat(dateLocalization.format(endDate, "yyyy"))
        },
        _isValueOnCurrentView: (currentDate, value) => dateUtils.sameDecade(currentDate, value),
        _getCellByDate(date) {
            var foundDate = new Date(date);
            foundDate.setDate(1);
            foundDate.setMonth(0);
            return this._$table.find("td[data-value='".concat(dateSerialization.serializeDate(foundDate, dateUtils.getShortDateFormat()), "']"))
        },
        isBoundary(date) {
            return dateUtils.sameDecade(date, this.option("min")) || dateUtils.sameDecade(date, this.option("max"))
        },
        _renderWeekNumberCell: noop
    }),
    century: BaseView.inherit({
        _getViewName: () => "century",
        _isTodayCell(cellDate) {
            var today = this.option("_todayDate")();
            return dateUtils.sameDecade(cellDate, today)
        },
        _isDateOutOfRange(cellDate) {
            var decade = dateUtils.getFirstYearInDecade(cellDate);
            var minDecade = dateUtils.getFirstYearInDecade(this.option("min"));
            var maxDecade = dateUtils.getFirstYearInDecade(this.option("max"));
            return !dateUtils.dateInRange(decade, minDecade, maxDecade)
        },
        _isOtherView(cellDate) {
            var date = new Date(cellDate);
            date.setMonth(1);
            return !dateUtils.sameCentury(date, this.option("date"))
        },
        _isStartDayOfMonth: () => false,
        _isEndDayOfMonth: () => false,
        _getCellText(cellDate) {
            var startDate = dateLocalization.format(cellDate, "yyyy");
            var endDate = new Date(cellDate);
            endDate.setFullYear(endDate.getFullYear() + 9);
            return "".concat(startDate, " - ").concat(dateLocalization.format(endDate, "yyyy"))
        },
        _getFirstCellData() {
            var decade = dateUtils.getFirstDecadeInCentury(this.option("date")) - 10;
            return dateUtils.createDateWithFullYear(decade, 0, 1)
        },
        _getNextCellData(date) {
            date = new Date(date);
            date.setFullYear(date.getFullYear() + 10);
            return date
        },
        _getCellByDate(date) {
            var foundDate = new Date(date);
            foundDate.setDate(1);
            foundDate.setMonth(0);
            foundDate.setFullYear(dateUtils.getFirstYearInDecade(foundDate));
            return this._$table.find("td[data-value='".concat(dateSerialization.serializeDate(foundDate, dateUtils.getShortDateFormat()), "']"))
        },
        getNavigatorCaption() {
            var currentDate = this.option("date");
            var firstDecadeInCentury = dateUtils.getFirstDecadeInCentury(currentDate);
            var startDate = new Date(currentDate);
            var endDate = new Date(currentDate);
            startDate.setFullYear(firstDecadeInCentury);
            endDate.setFullYear(firstDecadeInCentury + 99);
            return "".concat(dateLocalization.format(startDate, "yyyy"), "-").concat(dateLocalization.format(endDate, "yyyy"))
        },
        isBoundary(date) {
            return dateUtils.sameCentury(date, this.option("min")) || dateUtils.sameCentury(date, this.option("max"))
        },
        _renderWeekNumberCell: noop
    })
};
export default Views;
