import * as tslib_1 from "tslib";
import * as React from 'react';
import { BaseComponent, css, getRTL } from '../../Utilities';
import { FocusZone } from '../../FocusZone';
import { addYears, setMonth, getYearStart, getYearEnd, getMonthStart, getMonthEnd, compareDatePart } from '../../utilities/dateMath/DateMath';
import { Icon } from '../../Icon';
import * as stylesImport from './Calendar.scss';
import { CalendarYear } from './CalendarYear';
var styles = stylesImport;
var CalendarMonth = /** @class */ (function (_super) {
    tslib_1.__extends(CalendarMonth, _super);
    function CalendarMonth(props) {
        var _this = _super.call(this, props) || this;
        _this._onCalendarYearRef = function (ref) {
            _this._calendarYearRef = ref;
        };
        _this._onKeyDown = function (callback, ev) {
            if (ev.which === 13 /* enter */) {
                callback();
            }
        };
        _this._onSelectYear = function (selectedYear) {
            _this._focusOnUpdate = true;
            var _a = _this.props, navigatedDate = _a.navigatedDate, onNavigateDate = _a.onNavigateDate, maxDate = _a.maxDate, minDate = _a.minDate;
            var navYear = navigatedDate.getFullYear();
            if (navYear !== selectedYear) {
                var newNavigationDate = new Date(navigatedDate.getTime());
                newNavigationDate.setFullYear(selectedYear);
                // for min and max dates, adjust the new navigation date - perhaps this should be
                // checked on the master navigation date handler (i.e. in Calendar)
                if (maxDate && newNavigationDate > maxDate) {
                    newNavigationDate = setMonth(newNavigationDate, maxDate.getMonth());
                }
                else if (minDate && newNavigationDate < minDate) {
                    newNavigationDate = setMonth(newNavigationDate, minDate.getMonth());
                }
                onNavigateDate(newNavigationDate, true);
            }
            _this.setState({ isYearPickerVisible: false });
        };
        _this._yearToString = function (year) {
            var _a = _this.props, navigatedDate = _a.navigatedDate, dateTimeFormatter = _a.dateTimeFormatter;
            if (dateTimeFormatter) {
                // create a date based on the current nav date
                var yearFormattingDate = new Date(navigatedDate.getTime());
                yearFormattingDate.setFullYear(year);
                return dateTimeFormatter.formatYear(yearFormattingDate);
            }
            return String(year);
        };
        _this._yearRangeToString = function (yearRange) {
            return _this._yearToString(yearRange.fromYear) + " - " + _this._yearToString(yearRange.toYear);
        };
        _this._onRenderYear = function (year) {
            return _this._yearToString(year);
        };
        _this._onSelectNextYear = function () {
            var _a = _this.props, navigatedDate = _a.navigatedDate, onNavigateDate = _a.onNavigateDate;
            onNavigateDate(addYears(navigatedDate, 1), false);
        };
        _this._onSelectNextYearKeyDown = function (ev) {
            if (ev.which === 13 /* enter */) {
                _this._onKeyDown(_this._onSelectNextYear, ev);
            }
        };
        _this._onSelectPrevYear = function () {
            var _a = _this.props, navigatedDate = _a.navigatedDate, onNavigateDate = _a.onNavigateDate;
            onNavigateDate(addYears(navigatedDate, -1), false);
        };
        _this._onSelectPrevYearKeyDown = function (ev) {
            if (ev.which === 13 /* enter */) {
                _this._onKeyDown(_this._onSelectPrevYear, ev);
            }
        };
        _this._onSelectMonthKeyDown = function (index) {
            return function (ev) { return _this._onKeyDown(function () { return _this._onSelectMonth(index); }, ev); };
        };
        _this._onSelectMonth = function (newMonth) {
            var _a = _this.props, navigatedDate = _a.navigatedDate, onNavigateDate = _a.onNavigateDate, onHeaderSelect = _a.onHeaderSelect;
            // If header is clickable the calendars are overlayed, switch back to day picker when month is clicked
            if (onHeaderSelect) {
                onHeaderSelect(true);
            }
            onNavigateDate(setMonth(navigatedDate, newMonth), true);
        };
        _this._onHeaderSelect = function () {
            var _a = _this.props, onHeaderSelect = _a.onHeaderSelect, yearPickerHidden = _a.yearPickerHidden;
            if (!yearPickerHidden) {
                _this._focusOnUpdate = true;
                _this.setState({ isYearPickerVisible: true });
            }
            else if (onHeaderSelect) {
                onHeaderSelect(true);
            }
        };
        _this._onYearPickerHeaderSelect = function (focus) {
            _this._focusOnUpdate = focus;
            _this.setState({ isYearPickerVisible: false });
        };
        _this._onHeaderKeyDown = function (ev) {
            var onHeaderSelect = _this.props.onHeaderSelect;
            if (onHeaderSelect && (ev.which === 13 /* enter */ || ev.which === 32 /* space */)) {
                onHeaderSelect(true);
            }
        };
        _this._selectMonthCallbacks = [];
        props.strings.shortMonths.map(function (month, index) {
            _this._selectMonthCallbacks[index] = _this._onSelectMonth.bind(_this, index);
        });
        _this._isCurrentMonth = _this._isCurrentMonth.bind(_this);
        _this._onSelectNextYear = _this._onSelectNextYear.bind(_this);
        _this._onSelectPrevYear = _this._onSelectPrevYear.bind(_this);
        _this._onSelectMonth = _this._onSelectMonth.bind(_this);
        _this.state = { isYearPickerVisible: false };
        return _this;
    }
    CalendarMonth.prototype.componentDidUpdate = function () {
        if (this._focusOnUpdate) {
            this.focus();
            this._focusOnUpdate = false;
        }
    };
    CalendarMonth.prototype.render = function () {
        var _this = this;
        var _a = this.props, navigatedDate = _a.navigatedDate, selectedDate = _a.selectedDate, strings = _a.strings, today = _a.today, highlightCurrentMonth = _a.highlightCurrentMonth, highlightSelectedMonth = _a.highlightSelectedMonth, navigationIcons = _a.navigationIcons, dateTimeFormatter = _a.dateTimeFormatter, minDate = _a.minDate, maxDate = _a.maxDate, yearPickerHidden = _a.yearPickerHidden;
        if (this.state.isYearPickerVisible) {
            return (React.createElement(CalendarYear, { minYear: minDate ? minDate.getFullYear() : undefined, maxYear: maxDate ? maxDate.getFullYear() : undefined, onSelectYear: this._onSelectYear, navigationIcons: navigationIcons, onHeaderSelect: this._onYearPickerHeaderSelect, selectedYear: selectedDate ? selectedDate.getFullYear() : navigatedDate ? navigatedDate.getFullYear() : undefined, onRenderYear: this._onRenderYear, strings: {
                    rangeAriaLabel: this._yearRangeToString
                }, ref: this._onCalendarYearRef }));
        }
        var leftNavigationIcon = navigationIcons.leftNavigation;
        var rightNavigationIcon = navigationIcons.rightNavigation;
        // determine if previous/next years are in bounds
        var isPrevYearInBounds = minDate ? compareDatePart(minDate, getYearStart(navigatedDate)) < 0 : true;
        var isNextYearInBounds = maxDate ? compareDatePart(getYearEnd(navigatedDate), maxDate) < 0 : true;
        return (React.createElement("div", { className: css('ms-DatePicker-monthPicker', styles.monthPicker) },
            React.createElement("div", { className: css('ms-DatePicker-header', styles.header) },
                this.props.onHeaderSelect || !yearPickerHidden ? (React.createElement("div", { className: css('ms-DatePicker-currentYear js-showYearPicker', styles.currentYear, styles.headerToggleView), onClick: this._onHeaderSelect, onKeyDown: this._onHeaderKeyDown, "aria-label": dateTimeFormatter.formatYear(navigatedDate), role: "button", tabIndex: 0 }, dateTimeFormatter.formatYear(navigatedDate))) : (React.createElement("div", { className: css('ms-DatePicker-currentYear js-showYearPicker', styles.currentYear) }, dateTimeFormatter.formatYear(navigatedDate))),
                React.createElement("div", { className: css('ms-DatePicker-yearComponents', styles.yearComponents) },
                    React.createElement("div", { className: css('ms-DatePicker-navContainer', styles.navContainer) },
                        React.createElement("button", { className: css('ms-DatePicker-prevYear js-prevYear', styles.prevYear, (_b = {},
                                _b['ms-DatePicker-prevYear--disabled ' + styles.prevYearIsDisabled] = !isPrevYearInBounds,
                                _b)), disabled: !isPrevYearInBounds, onClick: isPrevYearInBounds ? this._onSelectPrevYear : undefined, onKeyDown: isPrevYearInBounds ? this._onSelectPrevYearKeyDown : undefined, "aria-label": strings.prevYearAriaLabel
                                ? strings.prevYearAriaLabel + ' ' + dateTimeFormatter.formatYear(addYears(navigatedDate, -1))
                                : undefined, role: "button" },
                            React.createElement(Icon, { iconName: getRTL() ? rightNavigationIcon : leftNavigationIcon })),
                        React.createElement("button", { className: css('ms-DatePicker-nextYear js-nextYear', styles.nextYear, (_c = {},
                                _c['ms-DatePicker-nextYear--disabled ' + styles.nextYearIsDisabled] = !isNextYearInBounds,
                                _c)), disabled: !isNextYearInBounds, onClick: isNextYearInBounds ? this._onSelectNextYear : undefined, onKeyDown: isNextYearInBounds ? this._onSelectNextYearKeyDown : undefined, "aria-label": strings.nextYearAriaLabel
                                ? strings.nextYearAriaLabel + ' ' + dateTimeFormatter.formatYear(addYears(navigatedDate, 1))
                                : undefined, role: "button" },
                            React.createElement(Icon, { iconName: getRTL() ? leftNavigationIcon : rightNavigationIcon }))))),
            React.createElement(FocusZone, null,
                React.createElement("div", { className: css('ms-DatePicker-optionGrid', styles.optionGrid), role: "grid", "aria-readonly": "true" },
                    React.createElement("div", { role: "row" }, strings.shortMonths.map(function (month, index) {
                        var indexedMonth = setMonth(navigatedDate, index);
                        var isCurrentMonth = _this._isCurrentMonth(index, navigatedDate.getFullYear(), today);
                        var isNavigatedMonth = navigatedDate.getMonth() === index;
                        var isSelectedMonth = selectedDate.getMonth() === index;
                        var isSelectedYear = selectedDate.getFullYear() === navigatedDate.getFullYear();
                        var isInBounds = (minDate ? compareDatePart(minDate, getMonthEnd(indexedMonth)) < 1 : true) &&
                            (maxDate ? compareDatePart(getMonthStart(indexedMonth), maxDate) < 1 : true);
                        return (React.createElement("button", { role: 'gridcell', className: css('ms-DatePicker-monthOption', styles.monthOption, (_a = {},
                                _a['ms-DatePicker-day--today ' + styles.monthIsCurrentMonth] = highlightCurrentMonth && isCurrentMonth,
                                _a['ms-DatePicker-day--highlighted ' + styles.monthIsHighlighted] = (highlightCurrentMonth || highlightSelectedMonth) && isSelectedMonth && isSelectedYear,
                                _a['ms-DatePicker-monthOption--disabled ' + styles.monthOptionIsDisabled] = !isInBounds,
                                _a)), disabled: !isInBounds, key: index, onClick: isInBounds ? _this._selectMonthCallbacks[index] : undefined, onKeyDown: isInBounds ? _this._onSelectMonthKeyDown(index) : undefined, "aria-label": dateTimeFormatter.formatMonthYear(indexedMonth, strings), "aria-selected": isCurrentMonth || isNavigatedMonth, "data-is-focusable": isInBounds ? true : undefined, ref: isNavigatedMonth ? 'navigatedMonth' : undefined }, month));
                        var _a;
                    }))))));
        var _b, _c;
    };
    CalendarMonth.prototype.focus = function () {
        if (this._calendarYearRef) {
            this._calendarYearRef.focus();
        }
        else if (this.refs.navigatedMonth) {
            this.refs.navigatedMonth.tabIndex = 0;
            this.refs.navigatedMonth.focus();
        }
    };
    CalendarMonth.prototype._isCurrentMonth = function (month, year, today) {
        return today.getFullYear() === year && today.getMonth() === month;
    };
    return CalendarMonth;
}(BaseComponent));
export { CalendarMonth };
//# sourceMappingURL=CalendarMonth.js.map