import * as tslib_1 from "tslib";
import * as React from 'react';
import { Calendar, DayOfWeek } from '../../Calendar';
import { FirstWeekOfYear } from '../../utilities/dateValues/DateValues';
import { Callout } from '../../Callout';
import { TextField } from '../../TextField';
import { BaseComponent, css, createRef } from '../../Utilities';
import { compareDates, compareDatePart } from '../../utilities/dateMath/DateMath';
import * as stylesImport from './DatePicker.scss';
import { FocusTrapZone } from '../../FocusTrapZone';
var styles = stylesImport;
var DEFAULT_STRINGS = {
    months: [
        'January',
        'February',
        'March',
        'April',
        'May',
        'June',
        'July',
        'August',
        'September',
        'October',
        'November',
        'December'
    ],
    shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
    shortDays: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
    goToToday: 'Go to today',
    prevMonthAriaLabel: 'Go to previous month',
    nextMonthAriaLabel: 'Go to next month',
    prevYearAriaLabel: 'Go to previous year',
    nextYearAriaLabel: 'Go to next year'
};
var DatePicker = /** @class */ (function (_super) {
    tslib_1.__extends(DatePicker, _super);
    function DatePicker(props) {
        var _this = _super.call(this, props) || this;
        _this._calendar = createRef();
        _this._datePickerDiv = createRef();
        _this._textField = createRef();
        _this._onSelectDate = function (date) {
            var _a = _this.props, formatDate = _a.formatDate, onSelectDate = _a.onSelectDate;
            if (_this.props.calendarProps && _this.props.calendarProps.onSelectDate) {
                _this.props.calendarProps.onSelectDate(date);
            }
            _this.setState({
                selectedDate: date,
                formattedDate: formatDate && date ? formatDate(date) : ''
            });
            if (onSelectDate) {
                onSelectDate(date);
            }
            _this._calendarDismissed();
        };
        _this._onCalloutPositioned = function () {
            if (_this._calendar.current && !_this.props.disableAutoFocus) {
                _this._calendar.current.focus();
            }
        };
        _this._onTextFieldFocus = function (ev) {
            if (_this.props.disableAutoFocus) {
                return;
            }
            if (!_this.props.allowTextInput) {
                if (!_this._preventFocusOpeningPicker) {
                    _this._showDatePickerPopup();
                }
                else {
                    _this._preventFocusOpeningPicker = false;
                }
            }
        };
        _this._onTextFieldBlur = function (ev) {
            _this._validateTextInput();
        };
        _this._onTextFieldChanged = function (newValue) {
            if (_this.props.allowTextInput) {
                if (_this.state.isDatePickerShown) {
                    _this._dismissDatePickerPopup();
                }
                var _a = _this.props, isRequired = _a.isRequired, value = _a.value, strings = _a.strings;
                _this.setState({
                    errorMessage: isRequired && !value ? strings.isRequiredErrorMessage || ' ' : undefined,
                    formattedDate: newValue
                });
            }
        };
        _this._onTextFieldKeyDown = function (ev) {
            switch (ev.which) {
                case 13 /* enter */:
                    ev.preventDefault();
                    ev.stopPropagation();
                    if (!_this.state.isDatePickerShown) {
                        _this._showDatePickerPopup();
                    }
                    else {
                        // When DatePicker allows input date string directly,
                        // it is expected to hit another enter to close the popup
                        if (_this.props.allowTextInput) {
                            _this._dismissDatePickerPopup();
                        }
                    }
                    break;
                case 27 /* escape */:
                    _this._handleEscKey(ev);
                    break;
                default:
                    break;
            }
        };
        _this._onTextFieldClick = function (ev) {
            if (!_this.state.isDatePickerShown && !_this.props.disabled) {
                _this._showDatePickerPopup();
            }
            else {
                if (_this.props.allowTextInput) {
                    _this.setState({
                        isDatePickerShown: false
                    });
                }
            }
        };
        _this._onIconClick = function (ev) {
            ev.stopPropagation();
            _this._onTextFieldClick(ev);
        };
        _this._dismissDatePickerPopup = function () {
            if (_this.state.isDatePickerShown) {
                _this.setState({
                    isDatePickerShown: false
                });
                _this._validateTextInput();
            }
        };
        /**
         * Callback for closing the calendar callout
         */
        _this._calendarDismissed = function () {
            _this._preventFocusOpeningPicker = true;
            _this._dismissDatePickerPopup();
            // don't need to focus the text box, if necessary the focusTrapZone will do it
        };
        _this._handleEscKey = function (ev) {
            ev.stopPropagation();
            _this._calendarDismissed();
        };
        _this._validateTextInput = function () {
            var _a = _this.props, isRequired = _a.isRequired, allowTextInput = _a.allowTextInput, strings = _a.strings, parseDateFromString = _a.parseDateFromString, onSelectDate = _a.onSelectDate, formatDate = _a.formatDate, minDate = _a.minDate, maxDate = _a.maxDate;
            var inputValue = _this.state.formattedDate;
            // Do validation only if DatePicker's popup is dismissed
            if (_this.state.isDatePickerShown) {
                return;
            }
            // Check when DatePicker is a required field but has NO input value
            if (isRequired && !inputValue) {
                _this.setState({
                    errorMessage: strings.isRequiredErrorMessage || ' '
                });
                return;
            }
            if (allowTextInput) {
                var date = null;
                if (inputValue) {
                    // Don't parse if the selected date has the same formatted string as what we're about to parse.
                    // The formatted string might be ambiguous (ex: "1/2/3" or "New Year Eve") and the parser might
                    // not be able to come up with the exact same date.
                    if (_this.state.selectedDate && formatDate && formatDate(_this.state.selectedDate) === inputValue) {
                        date = _this.state.selectedDate;
                    }
                    else {
                        date = parseDateFromString(inputValue);
                        // Check if date is null, or date is Invalid Date
                        if (!date || isNaN(date.getTime())) {
                            // Reset invalid input field, if formatting is available
                            if (formatDate) {
                                date = _this.state.selectedDate;
                                _this.setState({
                                    formattedDate: formatDate(date).toString()
                                });
                            }
                            _this.setState({
                                errorMessage: strings.invalidInputErrorMessage || ' '
                            });
                        }
                        else {
                            // Check against optional date boundaries
                            if (_this._isDateOutOfBounds(date, minDate, maxDate)) {
                                _this.setState({
                                    errorMessage: strings.isOutOfBoundsErrorMessage || ' '
                                });
                            }
                            else {
                                _this.setState({
                                    selectedDate: date,
                                    errorMessage: ''
                                });
                                // When formatting is available. If formatted date is valid, but is different from input, update with formatted date
                                // This occurs when an invalid date is entered twice
                                if (formatDate && formatDate(date) !== inputValue) {
                                    _this.setState({
                                        formattedDate: formatDate(date).toString()
                                    });
                                }
                            }
                        }
                    }
                }
                else {
                    // No input date string shouldn't be an error if field is not required
                    _this.setState({
                        errorMessage: ''
                    });
                }
                // Execute onSelectDate callback
                if (onSelectDate) {
                    // If no input date string or input date string is invalid
                    // date variable will be null, callback should expect null value for this case
                    onSelectDate(date);
                }
            }
        };
        _this.state = _this._getDefaultState();
        _this._preventFocusOpeningPicker = false;
        return _this;
    }
    DatePicker.prototype.componentWillReceiveProps = function (nextProps) {
        var formatDate = nextProps.formatDate, isRequired = nextProps.isRequired, strings = nextProps.strings, value = nextProps.value, minDate = nextProps.minDate, maxDate = nextProps.maxDate;
        if (compareDates(this.props.minDate, nextProps.minDate) &&
            compareDates(this.props.maxDate, nextProps.maxDate) &&
            this.props.isRequired === nextProps.isRequired &&
            compareDates(this.state.selectedDate, value) &&
            this.props.formatDate === formatDate) {
            // if the props we care about haven't changed, don't run validation or updates
            return;
        }
        var errorMessage = isRequired && !value ? strings.isRequiredErrorMessage || ' ' : undefined;
        if (!errorMessage && value) {
            errorMessage = this._isDateOutOfBounds(value, minDate, maxDate)
                ? strings.isOutOfBoundsErrorMessage || ' '
                : undefined;
        }
        // Set error message
        this.setState({
            errorMessage: errorMessage
        });
        // Issue# 1274: Check if the date value changed from old value, i.e., if indeed a new date is being
        // passed in or if the formatting function was modified. We only update the selected date if either of these
        // had a legit change. Note tha the bug will still repro when only the formatDate was passed in props and this
        // is the result of the onSelectDate callback, but this should be a rare scenario.
        var oldValue = this.state.selectedDate;
        if (!compareDates(oldValue, value) || this.props.formatDate !== formatDate) {
            this.setState({
                selectedDate: value || undefined,
                formattedDate: formatDate && value ? formatDate(value) : ''
            });
        }
    };
    DatePicker.prototype.componentDidUpdate = function (prevProps, prevState) {
        // If DatePicker's menu (Calendar) is closed, run onAfterMenuDismiss
        if (this.props.onAfterMenuDismiss && prevState.isDatePickerShown && !this.state.isDatePickerShown) {
            this.props.onAfterMenuDismiss();
        }
    };
    DatePicker.prototype.render = function () {
        var _a = this.props, firstDayOfWeek = _a.firstDayOfWeek, strings = _a.strings, label = _a.label, initialPickerDate = _a.initialPickerDate, isRequired = _a.isRequired, disabled = _a.disabled, ariaLabel = _a.ariaLabel, pickerAriaLabel = _a.pickerAriaLabel, placeholder = _a.placeholder, allowTextInput = _a.allowTextInput, borderless = _a.borderless, className = _a.className, minDate = _a.minDate, maxDate = _a.maxDate, calendarProps = _a.calendarProps;
        var _b = this.state, isDatePickerShown = _b.isDatePickerShown, formattedDate = _b.formattedDate, selectedDate = _b.selectedDate, errorMessage = _b.errorMessage;
        return (React.createElement("div", { className: css('ms-DatePicker', styles.root, isDatePickerShown && 'is-open', className) },
            React.createElement("div", { ref: this._datePickerDiv },
                React.createElement(TextField, { label: label, className: styles.textField, ariaLabel: ariaLabel, "aria-haspopup": "true", "aria-expanded": isDatePickerShown, required: isRequired, disabled: disabled, onKeyDown: this._onTextFieldKeyDown, onFocus: this._onTextFieldFocus, onBlur: this._onTextFieldBlur, onClick: this._onTextFieldClick, onChanged: this._onTextFieldChanged, errorMessage: errorMessage, placeholder: placeholder, borderless: borderless, iconProps: {
                        iconName: 'Calendar',
                        onClick: this._onIconClick,
                        className: css(disabled && styles.msDatePickerDisabled, label ? 'ms-DatePicker-event--with-label' : 'ms-DatePicker-event--without-label', label ? styles.eventWithLabel : styles.eventWithoutLabel)
                    }, readOnly: !allowTextInput, value: formattedDate, componentRef: this._textField, role: 'button' })),
            isDatePickerShown && (React.createElement(Callout, { role: "dialog", ariaLabel: pickerAriaLabel, isBeakVisible: false, className: css('ms-DatePicker-callout'), gapSpace: 0, doNotLayer: false, target: this._datePickerDiv.current, directionalHint: 4 /* bottomLeftEdge */, onDismiss: this._calendarDismissed, onPositioned: this._onCalloutPositioned },
                React.createElement(FocusTrapZone, { isClickableOutsideFocusTrap: true, disableFirstFocus: this.props.disableAutoFocus },
                    React.createElement(Calendar, tslib_1.__assign({}, calendarProps, { onSelectDate: this._onSelectDate, onDismiss: this._calendarDismissed, isMonthPickerVisible: this.props.isMonthPickerVisible, showMonthPickerAsOverlay: this.props.showMonthPickerAsOverlay, today: this.props.today, value: selectedDate || initialPickerDate, firstDayOfWeek: firstDayOfWeek, strings: strings, highlightCurrentMonth: this.props.highlightCurrentMonth, highlightSelectedMonth: this.props.highlightSelectedMonth, showWeekNumbers: this.props.showWeekNumbers, firstWeekOfYear: this.props.firstWeekOfYear, showGoToToday: this.props.showGoToToday, dateTimeFormatter: this.props.dateTimeFormatter, minDate: minDate, maxDate: maxDate, componentRef: this._calendar })))))));
    };
    DatePicker.prototype.focus = function () {
        if (this._textField.current) {
            this._textField.current.focus();
        }
    };
    DatePicker.prototype.reset = function () {
        this.setState(this._getDefaultState());
    };
    DatePicker.prototype._showDatePickerPopup = function () {
        if (!this.state.isDatePickerShown) {
            this._preventFocusOpeningPicker = true;
            this.setState({
                isDatePickerShown: true,
                errorMessage: ''
            });
        }
    };
    DatePicker.prototype._getDefaultState = function (props) {
        if (props === void 0) { props = this.props; }
        return {
            selectedDate: props.value || undefined,
            formattedDate: props.formatDate && props.value ? props.formatDate(props.value) : '',
            isDatePickerShown: false,
            errorMessage: undefined
        };
    };
    DatePicker.prototype._isDateOutOfBounds = function (date, minDate, maxDate) {
        return (!!minDate && compareDatePart(minDate, date) > 0) || (!!maxDate && compareDatePart(maxDate, date) < 0);
    };
    DatePicker.defaultProps = {
        allowTextInput: false,
        formatDate: function (date) {
            if (date) {
                return date.toDateString();
            }
            return '';
        },
        parseDateFromString: function (dateStr) {
            var date = Date.parse(dateStr);
            if (date) {
                return new Date(date);
            }
            return null;
        },
        firstDayOfWeek: DayOfWeek.Sunday,
        initialPickerDate: new Date(),
        isRequired: false,
        isMonthPickerVisible: true,
        showMonthPickerAsOverlay: false,
        strings: DEFAULT_STRINGS,
        highlightCurrentMonth: false,
        highlightSelectedMonth: false,
        borderless: false,
        pickerAriaLabel: 'Calender',
        showWeekNumbers: false,
        firstWeekOfYear: FirstWeekOfYear.FirstDay,
        showGoToToday: true,
        dateTimeFormatter: undefined
    };
    return DatePicker;
}(BaseComponent));
export { DatePicker };
//# sourceMappingURL=DatePicker.js.map