"use strict";
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 (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var TextInputControl_1 = require("../TextInputControl");
var react_dates_1 = require("react-dates");
var moment_1 = __importDefault(require("moment"));
var TimePicker_1 = require("./TimePicker");
var datePickerHelpers_1 = require("./datePickerHelpers");
var DatePickerBase_1 = require("./DatePickerBase");
var MonthYearDropDown_1 = require("./MonthYearDropDown");
var Button_1 = require("../../Button");
var utils_1 = require("../../../../utils");
var playMessage_1 = require("../../../utils/playMessage");
var renderCalendarDayContent_1 = require("./renderCalendarDayContent");
var __1 = require("../../..");
var index_en_US_1 = __importDefault(require("../../../../lang/common/index-en_US"));
var WAIT_CALENDAR_READY_TIMEOUT = 1;
var SingleDatePickerInputControl = /** @class */ (function (_super) {
    __extends(SingleDatePickerInputControl, _super);
    function SingleDatePickerInputControl() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = __assign({}, _super.prototype['state'], { isMonthYearEditing: false, value: _this.getDateTimeValue() });
        _this.notifyDateTimeChange = function (momentValue) {
            var value = moment_1.default(momentValue);
            var onChange = _this.getPropsFromContext().onChange;
            onChange(_this.getFormattedReturnValue(value));
        };
        /**
         * @param {Moment} newValue
         * */
        _this.onDateChange = function (newValue) {
            var oldValue = _this.getDateTimeValue();
            var oldMomentValue = moment_1.default(oldValue);
            var value = moment_1.default(newValue);
            if (oldValue) {
                value = value
                    .hour(oldMomentValue.hour())
                    .minute(oldMomentValue.minute())
                    .second(oldMomentValue.second());
            }
            _this.setState({ value: value }, function () { return _this.notifyDateTimeChange(value); });
            _this.closeDayPicker();
            _this.toggleIsInputFocusedFromOutside(false);
            _this.focusToInput();
        };
        _this.onTimeChange = function (_a) {
            var hours = _a.hours, minutes = _a.minutes;
            var newValue = moment_1.default(_this.getDateTimeMomentValue())
                .hour(hours)
                .minute(minutes);
            _this.setState({ value: newValue }, function () {
                _this.notifyDateTimeChange(newValue);
                playMessage_1.playMessage(_this.getTimePickerPromptMessage(newValue));
            });
        };
        _this.initialVisibleMonth = function () {
            var value = _this.getDateTimeValue();
            return value ? moment_1.default(value) : moment_1.default();
        };
        _this.handleSwitchToMonthYearEditing = function () {
            return _this.blurDateCell().then(function () { return _this.setState({ isMonthYearEditing: true }); });
        };
        _this.onSwitchToMonthYearText = function () { return _this.setState({ isMonthYearEditing: false }); };
        /* eslint-disable react/prop-types */
        _this.renderMonthsDropDown = function (_a) {
            var month = _a.month, onMonthSelect = _a.onMonthSelect, onYearSelect = _a.onYearSelect;
            var _b = _this.props, monthAriaLabel = _b.monthAriaLabel, yearAriaLabel = _b.yearAriaLabel, yearsAfterCurrent = _b.yearsAfterCurrent, yearsBeforeCurrent = _b.yearsBeforeCurrent;
            var onMonthSelectHandler = function (month, value) {
                onMonthSelect(month, value);
                _this.onSwitchToMonthYearText();
            };
            var onYearSelectHandler = function (month, value) {
                onYearSelect(month, value);
                _this.onSwitchToMonthYearText();
            };
            return (react_1.default.createElement(MonthYearDropDown_1.MonthYearDropDown, { month: month, onMonthSelect: onMonthSelectHandler, onYearSelect: onYearSelectHandler, monthAriaLabel: monthAriaLabel, yearAriaLabel: yearAriaLabel, yearsBeforeCurrent: yearsBeforeCurrent, yearsAfterCurrent: yearsAfterCurrent }));
        };
        _this.renderMonthYearText = function (_a) {
            var month = _a.month;
            var _b = _this.props, monthYearFormat = _b.monthYearFormat, monthYearBtnAriaLabel = _b.monthYearBtnAriaLabel;
            var ariaLabel = utils_1.translate(monthYearBtnAriaLabel, {
                month: month.format('MMMM'),
                year: month.year()
            });
            return (react_1.default.createElement(Button_1.Button, { testAutomationId: "editYearMonthButton", bsStyle: "link", onClick: _this.handleSwitchToMonthYearEditing, "aria-label": ariaLabel },
                react_1.default.createElement("strong", null, month.format(monthYearFormat))));
        };
        _this.handleBackFromTimePicker = function () { return __1.wait(WAIT_CALENDAR_READY_TIMEOUT).then(_this.focusOnDateCell); };
        _this.renderTimePicker = function () {
            var _a = _this.props, hasOnlyDate = _a.hasOnlyDate, timeFormat = _a.timeFormat, timePickerHourDecreaseBtnAriaLabel = _a.timePickerHourDecreaseBtnAriaLabel, timePickerHourIncreaseBtnAriaLabel = _a.timePickerHourIncreaseBtnAriaLabel, timePickerMinuteDecreaseBtnAriaLabel = _a.timePickerMinuteDecreaseBtnAriaLabel, timePickerMinuteIncreaseBtnAriaLabel = _a.timePickerMinuteIncreaseBtnAriaLabel, timeAmAriaLabel = _a.timeAmAriaLabel, timePmAriaLabel = _a.timePmAriaLabel, amPmSwitcherLabel = _a.amPmSwitcherLabel, enableAMTitle = _a.enableAMTitle, enablePMTitle = _a.enablePMTitle, timePickerAriaDescription = _a.timePickerAriaDescription;
            if (hasOnlyDate) {
                return null;
            }
            var value = _this.getTimeValue();
            var ariaDescription = utils_1.translate(timePickerAriaDescription, {
                time: moment_1.default(value).format(timeFormat)
            });
            return (react_1.default.createElement("div", { onMouseUp: _this.blurDateCell },
                react_1.default.createElement(TimePicker_1.TimePicker, { onChange: _this.onTimeChange, value: value, is24hour: datePickerHelpers_1.is24HoursMode(timeFormat), onPanelFocus: _this.blurDateCell, onBackToCalendar: _this.handleBackFromTimePicker, ariaDescription: ariaDescription, hourIncreaseBtnAriaLabel: timePickerHourIncreaseBtnAriaLabel, hourDecreaseBtnAriaLabel: timePickerHourDecreaseBtnAriaLabel, minuteIncreaseBtnAriaLabel: timePickerMinuteIncreaseBtnAriaLabel, minuteDecreaseBtnAriaLabel: timePickerMinuteDecreaseBtnAriaLabel, amAriaLabel: timeAmAriaLabel, pmAriaLabel: timePmAriaLabel, amPmSwitcherLabel: amPmSwitcherLabel, enablePMTitle: enablePMTitle, enableAMTitle: enableAMTitle })));
        };
        _this.getTimePickerPromptMessage = function (timeValue) {
            var _a = _this.props, timeFormat = _a.timeFormat, timePickerCurrentTimeSettingAriaPrompt = _a.timePickerCurrentTimeSettingAriaPrompt;
            return utils_1.translate(timePickerCurrentTimeSettingAriaPrompt, {
                time: moment_1.default(timeValue).format(timeFormat)
            });
        };
        _this.handleMouseUpDayPickerContainer = function () {
            _this.onSwitchToMonthYearText();
        };
        /** @override */
        _this.getDisplayValue = function () {
            var value = _this.getDateTimeValue();
            var _a = _this.props, dateFormat = _a.dateFormat, timeFormat = _a.timeFormat, hasOnlyDate = _a.hasOnlyDate, hasOnlyTime = _a.hasOnlyTime;
            if (!value) {
                return '';
            }
            if (!moment_1.default(value).isValid()) {
                return value;
            }
            var formatArr = [];
            if (!hasOnlyTime) {
                formatArr.push(dateFormat);
            }
            if (!hasOnlyDate) {
                formatArr.push(timeFormat);
            }
            return moment_1.default(value).format(formatArr.join(' '));
        };
        /** @override */
        _this.renderDatePicker = function () {
            var hasOnlyTime = _this.props.hasOnlyTime;
            return hasOnlyTime ? _this.renderTimePicker() : _this.renderSingleDatePicker();
        };
        return _this;
    }
    SingleDatePickerInputControl.prototype.parseUtcValue = function (value) {
        var utc = this.props.utc;
        if (value) {
            return utc ? moment_1.default.utc(value) : moment_1.default(value);
        }
        return null;
    };
    SingleDatePickerInputControl.prototype.getDateTimeValue = function () {
        var value = this.getPropsFromContext().value;
        return this.parseUtcValue(value);
    };
    SingleDatePickerInputControl.prototype.getFormattedReturnValue = function (value) {
        var _a = this.props, hasStringFormat = _a.hasStringFormat, hasTimestampFormat = _a.hasTimestampFormat, hasMomentFormat = _a.hasMomentFormat;
        if (!value) {
            return null;
        }
        if (typeof value === 'string') {
            return value;
        }
        if (hasStringFormat) {
            return value.format();
        }
        if (hasTimestampFormat) {
            return Number(value.toDate());
        }
        if (hasMomentFormat) {
            return value;
        }
        return value.toDate();
    };
    SingleDatePickerInputControl.prototype.getDateTimeMomentValue = function () {
        var value = this.getDateTimeValue();
        if (!value || !moment_1.default(value).isValid()) {
            value = moment_1.default();
        }
        else {
            value = moment_1.default(value);
        }
        return value;
    };
    SingleDatePickerInputControl.prototype.getTimeValue = function () {
        var value = this.getDateTimeMomentValue();
        return {
            hours: value.hour(),
            minutes: value.minute()
        };
    };
    /* eslint-enable react/prop-types */
    SingleDatePickerInputControl.prototype.renderSingleDatePicker = function () {
        var _this = this;
        var _a = this.props, numberOfMonths = _a.numberOfMonths, singleDatePickerProps = _a.singleDatePickerProps, isValidDate = _a.isValidDate;
        var _b = this.state, isDayPickerFocused = _b.isDayPickerFocused, isDayPickerVisible = _b.isDayPickerVisible;
        var value = this.getDateTimeValue();
        var renderMonthElement = this.state.isMonthYearEditing
            ? this.renderMonthsDropDown
            : this.renderMonthYearText;
        return isDayPickerVisible ? (react_1.default.createElement(react_dates_1.DayPickerSingleDateController, __assign({ renderCalendarInfo: this.renderTimePicker, calendarInfoPosition: 'after', isFocused: isDayPickerFocused, focused: true, initialVisibleMonth: this.initialVisibleMonth, onBlur: this.closeDayPicker, numberOfMonths: numberOfMonths, onDateChange: function (newValue) { return _this.onDateChange(newValue); }, renderMonthElement: renderMonthElement, date: moment_1.default(value), isOutsideRange: function (day) { return !isValidDate('days', day); }, hideKeyboardShortcutsPanel: true, phrases: this.getPhrases(), renderDayContents: renderCalendarDayContent_1.renderCalendarDayContent }, singleDatePickerProps))) : null;
    };
    SingleDatePickerInputControl.prototype.getDatePickerContainerClassName = function () {
        return 'rc-datepicker-single';
    };
    SingleDatePickerInputControl.propTypes = __assign({}, TextInputControl_1.TextInputControl.propTypes, DatePickerBase_1.DatePickerBase.propTypes, { dateFormat: prop_types_1.default.string, timeFormat: prop_types_1.default.string, utc: prop_types_1.default.bool, hasOnlyDate: prop_types_1.default.bool, hasOnlyTime: prop_types_1.default.bool, hasStringFormat: prop_types_1.default.bool, hasMomentFormat: prop_types_1.default.bool, hasTimestampFormat: prop_types_1.default.bool, onChange: prop_types_1.default.func, numberOfMonths: prop_types_1.default.number, isValidDate: prop_types_1.default.func, singleDatePickerProps: prop_types_1.default.object, inputProps: prop_types_1.default.object, monthYearFormat: prop_types_1.default.string, ariaTextDatePickerDescribe: prop_types_1.default.string, ariaTextSelectedDateValue: prop_types_1.default.string, ariaTextNextMonth: prop_types_1.default.string, ariaTextPrevMonth: prop_types_1.default.string, timePickerAriaDescription: prop_types_1.default.string, timePickerCurrentTimeSettingAriaPrompt: prop_types_1.default.string, timePickerHourIncreaseBtnAriaLabel: prop_types_1.default.string.isRequired, timePickerHourDecreaseBtnAriaLabel: prop_types_1.default.string.isRequired, timePickerMinuteIncreaseBtnAriaLabel: prop_types_1.default.string.isRequired, timePickerMinuteDecreaseBtnAriaLabel: prop_types_1.default.string.isRequired, timeAmAriaLabel: prop_types_1.default.string, timePmAriaLabel: prop_types_1.default.string, amPmSwitcherLabel: prop_types_1.default.string, enablePMTitle: prop_types_1.default.string, enableAMTitle: prop_types_1.default.string, monthYearBtnAriaLabel: prop_types_1.default.string, monthAriaLabel: prop_types_1.default.string, yearAriaLabel: prop_types_1.default.string, yearsBeforeCurrent: prop_types_1.default.number, yearsAfterCurrent: prop_types_1.default.number });
    SingleDatePickerInputControl.defaultProps = __assign({}, DatePickerBase_1.DatePickerBase.defaultProps, TextInputControl_1.TextInputControl.defaultProps, { testAutomationId: 'dateTimePicker', dateFormat: 'L', timeFormat: 'h:mm A', utc: false, hasOnlyTime: false, hasOnlyDate: false, numberOfMonths: 1, isValidDate: function (mode, currentDay) { return true; }, singleDatePickerProps: {}, inputProps: {}, monthYearFormat: 'MMMM YYYY', get timePickerCurrentTimeSettingAriaPrompt() {
            return index_en_US_1.default.TIME_PICKER_ARIA_CURRENT_TIME;
        },
        get timePickerAriaDescription() {
            return index_en_US_1.default.TIME_PICKER_ARIA_DESCRIPTION;
        },
        get timePickerHourIncreaseBtnAriaLabel() {
            return index_en_US_1.default.TIME_PICKER_HOUR_INCREASE_ARIA_LABEL;
        },
        get timePickerHourDecreaseBtnAriaLabel() {
            return index_en_US_1.default.TIME_PICKER_HOUR_DECREASE_ARIA_LABEL;
        },
        get timePickerMinuteIncreaseBtnAriaLabel() {
            return index_en_US_1.default.TIME_PICKER_MINUTE_INCREASE_ARIA_LABEL;
        },
        get timePickerMinuteDecreaseBtnAriaLabel() {
            return index_en_US_1.default.TIME_PICKER_MINUTE_DECREASE_ARIA_LABEL;
        },
        get timeAmAriaLabel() {
            return index_en_US_1.default.TIME_PICKER_AM;
        },
        get timePmAriaLabel() {
            return index_en_US_1.default.TIME_PICKER_PM;
        },
        get monthYearBtnAriaLabel() {
            return index_en_US_1.default.DATE_PICKER_MONTH_BUTTON_ARIA_LABEL;
        },
        get monthAriaLabel() {
            return index_en_US_1.default.DATE_PICKER_MONTH_DROPDOWN_ARIA_LABEL;
        },
        get yearAriaLabel() {
            return index_en_US_1.default.DATE_PICKER_YEAR_DROPDOWN_ARIA_LABEL;
        },
        get amPmSwitcherLabel() {
            return index_en_US_1.default.AM_PM_SWITCHER;
        },
        get enablePMTitle() {
            return index_en_US_1.default.CLICK_TO_ENABLE_PM;
        },
        get enableAMTitle() {
            return index_en_US_1.default.CLICK_TO_ENABLE_AM;
        }, yearsBeforeCurrent: 100, yearsAfterCurrent: 100, hasStringFormat: false, hasMomentFormat: false, hasTimestampFormat: false });
    return SingleDatePickerInputControl;
}(DatePickerBase_1.DatePickerBase));
exports.SingleDatePickerInputControl = SingleDatePickerInputControl;
//# sourceMappingURL=SingleDatePickerInputControl.js.map