"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 lodash_1 = __importDefault(require("lodash"));
var react_dates_1 = require("react-dates");
var datePickerHelpers_1 = require("./datePickerHelpers");
var moment_1 = __importDefault(require("moment"));
var PreDefinedRangesBlock_1 = require("./PreDefinedRangesBlock");
var DatePickerBase_1 = require("./DatePickerBase");
var renderCalendarDayContent_1 = require("./renderCalendarDayContent");
var index_en_US_1 = __importDefault(require("../../../../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: datePickerHelpers_1.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 (lodash_1.default.isFunction(onChangeFromProp)) {
                onChangeFromProp({ startDate: startDate, endDate: endDate });
            }
            else if (lodash_1.default.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_1.default.createElement(PreDefinedRangesBlock_1.PreDefinedRangesBlock, { ranges: ranges, startDate: startDate, endDate: endDate, onPredefinedRangeButtonClick: _this.onPredefinedRangeButtonClick }));
        };
        _this.isOutsideRange = function (dayValue) {
            var day = moment_1.default(dayValue).startOf('date');
            var _a = _this.props, maxDate = _a.maxDate, minDate = _a.minDate;
            var maxDateMoment = maxDate ? moment_1.default(maxDate) : maxDate;
            var minDateMoment = minDate ? moment_1.default(minDate) : minDate;
            if (maxDateMoment && minDateMoment) {
                return moment_1.default(minDateMoment).isAfter(day) || moment_1.default(maxDateMoment).isSameOrBefore(day);
            }
            if (maxDateMoment) {
                return moment_1.default(maxDateMoment).isBefore(day);
            }
            if (minDateMoment) {
                return moment_1.default(minDateMoment).isAfter(day);
            }
            return false;
        };
        _this.handleCloseDayPicker = function (dateRange) {
            var focusedInput = _this.state.focusedInput;
            _this.toggleIsInputFocusedFromOutside(false);
            _this.closeDayPicker();
            _this.setState({ focusedInput: datePickerHelpers_1.REACT_DATES_START_DATE_INPUT_MODE });
            if (!lodash_1.default.isEmpty(dateRange)) {
                _this.onDatesChange(dateRange);
            }
            if (focusedInput !== datePickerHelpers_1.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 (lodash_1.default.isEmpty(startDate) || lodash_1.default.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_1.default.createElement(react_dates_1.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_1.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 (!datePickerHelpers_1.isSameDate(startDate, newStartDate)) {
            newStates.startDate = newStartDate;
        }
        if (!datePickerHelpers_1.isSameDate(endDate, newEndDate)) {
            newStates.endDate = newEndDate;
        }
        if (!lodash_1.default.isEmpty(newStates)) {
            this.setState(newStates);
        }
    };
    /**@override*/
    DayRangePickerControl.prototype.closeDayPicker = function () {
        this.setState({
            focusedInput: datePickerHelpers_1.REACT_DATES_START_DATE_INPUT_MODE
        });
        return _super.prototype.closeDayPicker.call(this);
    };
    DayRangePickerControl.propTypes = __assign({}, TextInputControl_1.TextInputControl.propTypes, DatePickerBase_1.DatePickerBase.propTypes, { startDate: prop_types_1.default.oneOfType([prop_types_1.default.number, prop_types_1.default.object]), endDate: prop_types_1.default.oneOfType([prop_types_1.default.number, prop_types_1.default.object]), valueFormatter: prop_types_1.default.func, onChange: prop_types_1.default.func, numberOfMonths: prop_types_1.default.number, dayPickerProps: prop_types_1.default.object, inputProps: prop_types_1.default.object, ranges: prop_types_1.default.object, maxDate: prop_types_1.default.oneOfType([prop_types_1.default.number, prop_types_1.default.object]), minDate: prop_types_1.default.oneOfType([prop_types_1.default.number, prop_types_1.default.object]) });
    DayRangePickerControl.defaultProps = __assign({}, TextInputControl_1.TextInputControl.defaultProps, DatePickerBase_1.DatePickerBase.defaultProps, { testAutomationId: 'dayRangePicker', startDate: null, endDate: null, valueFormatter: datePickerHelpers_1.dateRangePickerValueFormatter, dayPickerProps: {}, numberOfMonths: 2, inputProps: {}, ranges: null, maxDate: null, minDate: null, get ariaLabel() {
            return index_en_US_1.default.CHOOSE_DATE_RANGE;
        } });
    return DayRangePickerControl;
}(DatePickerBase_1.DatePickerBase));
exports.DayRangePickerControl = DayRangePickerControl;
//# sourceMappingURL=DayRangePickerControl.js.map