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);
};
import React from 'react';
import PropTypes from 'prop-types';
import { TextInputControl } from '../TextInputControl';
import _ from 'lodash';
import { DayPickerRangeController } from 'react-dates';
import { dateRangePickerValueFormatter, isSameDate, REACT_DATES_START_DATE_INPUT_MODE } from './datePickerHelpers';
import moment from 'moment';
import { PreDefinedRangesBlock } from './PreDefinedRangesBlock';
import { DatePickerBase } from './DatePickerBase';
import { renderCalendarDayContent } from './renderCalendarDayContent';
import lang from '../../../../lang/common/index-en_US';
var DayRangePickerControl = /** @class */ (function (_super) {
    __extends(DayRangePickerControl, _super);
    function DayRangePickerControl() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = __assign({}, _super.prototype['state'], { startDate: _this.props.startDate, endDate: _this.props.endDate, focusedInput: REACT_DATES_START_DATE_INPUT_MODE, value: _this.getPropsFromContext().value });
        _this.onCalendarFocusChange = function (focusedInput) { return _this.setState({ focusedInput: focusedInput }); };
        _this.notifyDateTimeChange = function (_a) {
            var startDate = _a.startDate, endDateValue = _a.endDate;
            var onChangeFromContext = _this.getPropsFromContext().onChange;
            var onChangeFromProp = _this.props.onChange;
            var endDate = endDateValue === null ? startDate : endDateValue;
            if (_.isFunction(onChangeFromProp)) {
                onChangeFromProp({ startDate: startDate, endDate: endDate });
            }
            else if (_.isFunction(onChangeFromContext)) {
                onChangeFromContext({ startDate: startDate, endDate: endDate });
            }
        };
        _this.onDatesChange = function (_a) {
            var startDate = _a.startDate, endDate = _a.endDate;
            _this.setState({ startDate: startDate, endDate: endDate }, function () { return _this.notifyDateTimeChange({ startDate: startDate, endDate: endDate }); });
        };
        _this.onPredefinedRangeButtonClick = function (_a) {
            var startDate = _a.startDate, endDate = _a.endDate;
            _this.onDatesChange({ startDate: startDate, endDate: endDate });
            _this.closeDayPicker();
            _this.toggleIsInputFocusedFromOutside(false);
            _this.focusToInput();
        };
        _this.renderPredefinedRanges = function () {
            var ranges = _this.props.ranges;
            var _a = _this.state, startDate = _a.startDate, endDate = _a.endDate;
            return (React.createElement(PreDefinedRangesBlock, { ranges: ranges, startDate: startDate, endDate: endDate, onPredefinedRangeButtonClick: _this.onPredefinedRangeButtonClick }));
        };
        _this.isOutsideRange = function (dayValue) {
            var day = moment(dayValue).startOf('date');
            var _a = _this.props, maxDate = _a.maxDate, minDate = _a.minDate;
            var maxDateMoment = maxDate ? moment(maxDate) : maxDate;
            var minDateMoment = minDate ? moment(minDate) : minDate;
            if (maxDateMoment && minDateMoment) {
                return moment(minDateMoment).isAfter(day) || moment(maxDateMoment).isSameOrBefore(day);
            }
            if (maxDateMoment) {
                return moment(maxDateMoment).isBefore(day);
            }
            if (minDateMoment) {
                return moment(minDateMoment).isAfter(day);
            }
            return false;
        };
        _this.handleCloseDayPicker = function (dateRange) {
            var focusedInput = _this.state.focusedInput;
            _this.toggleIsInputFocusedFromOutside(false);
            _this.closeDayPicker();
            _this.setState({ focusedInput: REACT_DATES_START_DATE_INPUT_MODE });
            if (!_.isEmpty(dateRange)) {
                _this.onDatesChange(dateRange);
            }
            if (focusedInput !== REACT_DATES_START_DATE_INPUT_MODE) {
                _this.focusToInput();
            }
        };
        /** @override */
        _this.getDisplayValue = function () {
            var _a = _this.state, startDate = _a.startDate, endDate = _a.endDate;
            var valueFormatter = _this.props.valueFormatter;
            if (_.isEmpty(startDate) || _.isEmpty(endDate)) {
                return '';
            }
            return valueFormatter(startDate, endDate);
        };
        /** @override */
        _this.renderDatePicker = function () {
            var _a = _this.props, numberOfMonths = _a.numberOfMonths, dayPickerProps = _a.dayPickerProps;
            var _b = _this
                .state, startDate = _b.startDate, endDate = _b.endDate, focusedInput = _b.focusedInput, isDayPickerFocused = _b.isDayPickerFocused, isDayPickerVisible = _b.isDayPickerVisible;
            if (!isDayPickerVisible) {
                return null;
            }
            return (React.createElement(DayPickerRangeController, __assign({ isFocused: isDayPickerFocused, focusedInput: focusedInput, numberOfMonths: numberOfMonths, onFocusChange: _this.onCalendarFocusChange, onDatesChange: _this.onDatesChange, onClose: _this.handleCloseDayPicker, startDate: startDate, endDate: endDate, renderCalendarInfo: _this.renderPredefinedRanges, isOutsideRange: _this.isOutsideRange, calendarInfoPosition: "before", hideKeyboardShortcutsPanel: true, phrases: _this.getPhrases(), renderDayContents: renderCalendarDayContent, minimumNights: 0 }, dayPickerProps)));
        };
        return _this;
    }
    DayRangePickerControl.prototype.componentWillReceiveProps = function (newProps) {
        var newStartDate = newProps.startDate, newEndDate = newProps.endDate;
        var _a = this.props, startDate = _a.startDate, endDate = _a.endDate;
        var newStates = {};
        if (!isSameDate(startDate, newStartDate)) {
            newStates.startDate = newStartDate;
        }
        if (!isSameDate(endDate, newEndDate)) {
            newStates.endDate = newEndDate;
        }
        if (!_.isEmpty(newStates)) {
            this.setState(newStates);
        }
    };
    /**@override*/
    DayRangePickerControl.prototype.closeDayPicker = function () {
        this.setState({
            focusedInput: REACT_DATES_START_DATE_INPUT_MODE
        });
        return _super.prototype.closeDayPicker.call(this);
    };
    DayRangePickerControl.propTypes = __assign({}, TextInputControl.propTypes, DatePickerBase.propTypes, { startDate: PropTypes.oneOfType([PropTypes.number, PropTypes.object]), endDate: PropTypes.oneOfType([PropTypes.number, PropTypes.object]), valueFormatter: PropTypes.func, onChange: PropTypes.func, numberOfMonths: PropTypes.number, dayPickerProps: PropTypes.object, inputProps: PropTypes.object, ranges: PropTypes.object, maxDate: PropTypes.oneOfType([PropTypes.number, PropTypes.object]), minDate: PropTypes.oneOfType([PropTypes.number, PropTypes.object]) });
    DayRangePickerControl.defaultProps = __assign({}, TextInputControl.defaultProps, DatePickerBase.defaultProps, { testAutomationId: 'dayRangePicker', startDate: null, endDate: null, valueFormatter: dateRangePickerValueFormatter, dayPickerProps: {}, numberOfMonths: 2, inputProps: {}, ranges: null, maxDate: null, minDate: null, get ariaLabel() {
            return lang.CHOOSE_DATE_RANGE;
        } });
    return DayRangePickerControl;
}(DatePickerBase));
export { DayRangePickerControl };
//# sourceMappingURL=DayRangePickerControl.js.map