"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.SingleDatePickerInputControl = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _TextInputControl = require("../TextInputControl");

var _reactDates = require("react-dates");

var _moment = _interopRequireDefault(require("moment"));

var _TimePicker = require("./TimePicker");

var _datePickerHelpers = require("./datePickerHelpers");

var _DatePickerBase2 = require("./DatePickerBase");

var _MonthYearDropDown = require("./MonthYearDropDown");

var _Button = require("../../Button");

var _utils = require("../../../../utils");

var _playMessage = require("../../../utils/playMessage");

var _renderCalendarDayContent = require("./renderCalendarDayContent");

var _ = require("../../..");

var WAIT_CALENDAR_READY_TIMEOUT = 1;

var SingleDatePickerInputControl =
/*#__PURE__*/
function (_DatePickerBase) {
  (0, _inheritsLoose2.default)(SingleDatePickerInputControl, _DatePickerBase);

  function SingleDatePickerInputControl() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _DatePickerBase.call.apply(_DatePickerBase, [this].concat(args)) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", Object.assign({}, _DatePickerBase.prototype.state, {
      isMonthYearEditing: false,
      value: _this.getDateTimeValue()
    }));
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "notifyDateTimeChange", function (momentValue) {
      var value = (0, _moment.default)(momentValue);

      var _this$getPropsFromCon = _this.getPropsFromContext(),
          onChange = _this$getPropsFromCon.onChange;

      onChange(_this.getFormattedReturnValue(value));
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onDateChange", function (newValue) {
      var oldValue = _this.getDateTimeValue();

      var oldMomentValue = (0, _moment.default)(oldValue);
      var value = (0, _moment.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();
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onTimeChange", function (_ref) {
      var hours = _ref.hours,
          minutes = _ref.minutes;
      var newValue = (0, _moment.default)(_this.getDateTimeMomentValue()).hour(hours).minute(minutes);

      _this.setState({
        value: newValue
      }, function () {
        _this.notifyDateTimeChange(newValue);

        (0, _playMessage.playMessage)(_this.getTimePickerPromptMessage(newValue));
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "initialVisibleMonth", function () {
      var value = _this.getDateTimeValue();

      return value ? (0, _moment.default)(value) : (0, _moment.default)();
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleSwitchToMonthYearEditing", function () {
      return _this.blurDateCell().then(function () {
        return _this.setState({
          isMonthYearEditing: true
        });
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onSwitchToMonthYearText", function () {
      return _this.setState({
        isMonthYearEditing: false
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderMonthsDropDown", function (_ref2) {
      var month = _ref2.month,
          onMonthSelect = _ref2.onMonthSelect,
          onYearSelect = _ref2.onYearSelect;
      var _this$props = _this.props,
          monthAriaLabel = _this$props.monthAriaLabel,
          yearAriaLabel = _this$props.yearAriaLabel,
          yearsAfterCurrent = _this$props.yearsAfterCurrent,
          yearsBeforeCurrent = _this$props.yearsBeforeCurrent;

      var onMonthSelectHandler = function onMonthSelectHandler(month, value) {
        onMonthSelect(month, value);

        _this.onSwitchToMonthYearText();
      };

      var onYearSelectHandler = function onYearSelectHandler(month, value) {
        onYearSelect(month, value);

        _this.onSwitchToMonthYearText();
      };

      return _react.default.createElement(_MonthYearDropDown.MonthYearDropDown, {
        month: month,
        onMonthSelect: onMonthSelectHandler,
        onYearSelect: onYearSelectHandler,
        monthAriaLabel: monthAriaLabel,
        yearAriaLabel: yearAriaLabel,
        yearsBeforeCurrent: yearsBeforeCurrent,
        yearsAfterCurrent: yearsAfterCurrent
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderMonthYearText", function (_ref3) {
      var month = _ref3.month;
      var _this$props2 = _this.props,
          monthYearFormat = _this$props2.monthYearFormat,
          monthYearBtnAriaLabel = _this$props2.monthYearBtnAriaLabel;
      var ariaLabel = (0, _utils.translate)(monthYearBtnAriaLabel, {
        month: month.format('MMMM'),
        year: month.year()
      });
      return _react.default.createElement(_Button.Button, {
        testAutomationId: "editYearMonthButton",
        bsStyle: "link",
        onClick: _this.handleSwitchToMonthYearEditing,
        "aria-label": ariaLabel
      }, _react.default.createElement("strong", null, month.format(monthYearFormat)));
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleBackFromTimePicker", function () {
      return (0, _.wait)(WAIT_CALENDAR_READY_TIMEOUT).then(_this.focusOnDateCell);
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderTimePicker", function () {
      var _this$props3 = _this.props,
          hasOnlyDate = _this$props3.hasOnlyDate,
          timeFormat = _this$props3.timeFormat,
          timePickerHourDecreaseBtnAriaLabel = _this$props3.timePickerHourDecreaseBtnAriaLabel,
          timePickerHourIncreaseBtnAriaLabel = _this$props3.timePickerHourIncreaseBtnAriaLabel,
          timePickerMinuteDecreaseBtnAriaLabel = _this$props3.timePickerMinuteDecreaseBtnAriaLabel,
          timePickerMinuteIncreaseBtnAriaLabel = _this$props3.timePickerMinuteIncreaseBtnAriaLabel,
          timeAmAriaLabel = _this$props3.timeAmAriaLabel,
          timePmAriaLabel = _this$props3.timePmAriaLabel,
          amPmSwitcherLabel = _this$props3.amPmSwitcherLabel,
          enableAMTitle = _this$props3.enableAMTitle,
          enablePMTitle = _this$props3.enablePMTitle,
          timePickerAriaDescription = _this$props3.timePickerAriaDescription;

      if (hasOnlyDate) {
        return null;
      }

      var value = _this.getTimeValue();

      var ariaDescription = (0, _utils.translate)(timePickerAriaDescription, {
        time: (0, _moment.default)(value).format(timeFormat)
      });
      return _react.default.createElement("div", {
        onMouseUp: _this.blurDateCell
      }, _react.default.createElement(_TimePicker.TimePicker, {
        onChange: _this.onTimeChange,
        value: value,
        is24hour: (0, _datePickerHelpers.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
      }));
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getTimePickerPromptMessage", function (timeValue) {
      var _this$props4 = _this.props,
          timeFormat = _this$props4.timeFormat,
          timePickerCurrentTimeSettingAriaPrompt = _this$props4.timePickerCurrentTimeSettingAriaPrompt;
      return (0, _utils.translate)(timePickerCurrentTimeSettingAriaPrompt, {
        time: (0, _moment.default)(timeValue).format(timeFormat)
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleMouseUpDayPickerContainer", function () {
      _this.onSwitchToMonthYearText();
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getDisplayValue", function () {
      var value = _this.getDateTimeValue();

      var _this$props5 = _this.props,
          dateFormat = _this$props5.dateFormat,
          timeFormat = _this$props5.timeFormat,
          hasOnlyDate = _this$props5.hasOnlyDate,
          hasOnlyTime = _this$props5.hasOnlyTime;

      if (!value) {
        return '';
      }

      if (!(0, _moment.default)(value).isValid()) {
        return value;
      }

      var formatArr = [];

      if (!hasOnlyTime) {
        formatArr.push(dateFormat);
      }

      if (!hasOnlyDate) {
        formatArr.push(timeFormat);
      }

      return (0, _moment.default)(value).format(formatArr.join(' '));
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderDatePicker", function () {
      var hasOnlyTime = _this.props.hasOnlyTime;
      return hasOnlyTime ? _this.renderTimePicker() : _this.renderSingleDatePicker();
    });
    return _this;
  }

  var _proto = SingleDatePickerInputControl.prototype;

  _proto.parseUtcValue = function parseUtcValue(value) {
    var utc = this.props.utc;

    if (value) {
      return utc ? _moment.default.utc(value) : (0, _moment.default)(value);
    }

    return null;
  };

  _proto.getDateTimeValue = function getDateTimeValue() {
    var _this$getPropsFromCon2 = this.getPropsFromContext(),
        value = _this$getPropsFromCon2.value;

    return this.parseUtcValue(value);
  };

  _proto.getFormattedReturnValue = function getFormattedReturnValue(value) {
    var _this$props6 = this.props,
        hasStringFormat = _this$props6.hasStringFormat,
        hasTimestampFormat = _this$props6.hasTimestampFormat,
        hasMomentFormat = _this$props6.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();
  }
  /**
   * @param {Moment} newValue
   * */
  ;

  _proto.getDateTimeMomentValue = function getDateTimeMomentValue() {
    var value = this.getDateTimeValue();

    if (!value || !(0, _moment.default)(value).isValid()) {
      value = (0, _moment.default)();
    } else {
      value = (0, _moment.default)(value);
    }

    return value;
  };

  _proto.getTimeValue = function getTimeValue() {
    var value = this.getDateTimeMomentValue();
    return {
      hours: value.hour(),
      minutes: value.minute()
    };
  };

  /* eslint-enable react/prop-types */
  _proto.renderSingleDatePicker = function renderSingleDatePicker() {
    var _this2 = this;

    var _this$props7 = this.props,
        numberOfMonths = _this$props7.numberOfMonths,
        singleDatePickerProps = _this$props7.singleDatePickerProps,
        isValidDate = _this$props7.isValidDate;
    var _this$state = this.state,
        isDayPickerFocused = _this$state.isDayPickerFocused,
        isDayPickerVisible = _this$state.isDayPickerVisible;
    var value = this.getDateTimeValue();
    var renderMonthElement = this.state.isMonthYearEditing ? this.renderMonthsDropDown : this.renderMonthYearText;
    return isDayPickerVisible ? _react.default.createElement(_reactDates.DayPickerSingleDateController, (0, _extends2.default)({
      renderCalendarInfo: this.renderTimePicker,
      calendarInfoPosition: 'after',
      isFocused: isDayPickerFocused,
      focused: true,
      initialVisibleMonth: this.initialVisibleMonth,
      onBlur: this.closeDayPicker,
      numberOfMonths: numberOfMonths,
      onDateChange: function onDateChange(newValue) {
        return _this2.onDateChange(newValue);
      },
      renderMonthElement: renderMonthElement,
      date: (0, _moment.default)(value),
      isOutsideRange: function isOutsideRange(day) {
        return !isValidDate('days', day);
      },
      hideKeyboardShortcutsPanel: true,
      phrases: this.getPhrases(),
      renderDayContents: _renderCalendarDayContent.renderCalendarDayContent
    }, singleDatePickerProps)) : null;
  };

  _proto.getDatePickerContainerClassName = function getDatePickerContainerClassName() {
    return 'rc-datepicker-single';
  };

  return SingleDatePickerInputControl;
}(_DatePickerBase2.DatePickerBase);

exports.SingleDatePickerInputControl = SingleDatePickerInputControl;
(0, _defineProperty2.default)(SingleDatePickerInputControl, "propTypes", Object.assign({}, _TextInputControl.TextInputControl.propTypes, _DatePickerBase2.DatePickerBase.propTypes, {
  dateFormat: _propTypes.default.string,
  timeFormat: _propTypes.default.string,
  utc: _propTypes.default.bool,
  hasOnlyDate: _propTypes.default.bool,
  hasOnlyTime: _propTypes.default.bool,
  hasStringFormat: _propTypes.default.bool,
  hasMomentFormat: _propTypes.default.bool,
  hasTimestampFormat: _propTypes.default.bool,
  onChange: _propTypes.default.func,
  numberOfMonths: _propTypes.default.number,
  isValidDate: _propTypes.default.func,
  singleDatePickerProps: _propTypes.default.object,
  inputProps: _propTypes.default.object,
  monthYearFormat: _propTypes.default.string,
  ariaTextDatePickerDescribe: _propTypes.default.string,
  ariaTextSelectedDateValue: _propTypes.default.string,
  ariaTextNextMonth: _propTypes.default.string,
  ariaTextPrevMonth: _propTypes.default.string,
  timePickerAriaDescription: _propTypes.default.string,
  timePickerCurrentTimeSettingAriaPrompt: _propTypes.default.string,
  timePickerHourIncreaseBtnAriaLabel: _propTypes.default.string.isRequired,
  timePickerHourDecreaseBtnAriaLabel: _propTypes.default.string.isRequired,
  timePickerMinuteIncreaseBtnAriaLabel: _propTypes.default.string.isRequired,
  timePickerMinuteDecreaseBtnAriaLabel: _propTypes.default.string.isRequired,
  timeAmAriaLabel: _propTypes.default.string,
  timePmAriaLabel: _propTypes.default.string,
  amPmSwitcherLabel: _propTypes.default.string,
  enablePMTitle: _propTypes.default.string,
  enableAMTitle: _propTypes.default.string,
  monthYearBtnAriaLabel: _propTypes.default.string,
  monthAriaLabel: _propTypes.default.string,
  yearAriaLabel: _propTypes.default.string,
  yearsBeforeCurrent: _propTypes.default.number,
  yearsAfterCurrent: _propTypes.default.number
}));
(0, _defineProperty2.default)(SingleDatePickerInputControl, "defaultProps", Object.assign({}, _DatePickerBase2.DatePickerBase.defaultProps, _TextInputControl.TextInputControl.defaultProps, {
  testAutomationId: 'dateTimePicker',
  dateFormat: 'L',
  timeFormat: 'h:mm A',
  utc: false,
  hasOnlyTime: false,
  hasOnlyDate: false,
  numberOfMonths: 1,
  isValidDate: function isValidDate(mode, currentDay) {
    return true;
  },
  singleDatePickerProps: {},
  inputProps: {},
  monthYearFormat: 'MMMM YYYY',
  timePickerCurrentTimeSettingAriaPrompt: 'TRANSLATE:TIME_PICKER_ARIA_CURRENT_TIME',
  timePickerAriaDescription: 'TRANSLATE:TIME_PICKER_ARIA_DESCRIPTION',
  timePickerHourIncreaseBtnAriaLabel: 'TRANSLATE:TIME_PICKER_HOUR_INCREASE_ARIA_LABEL',
  timePickerHourDecreaseBtnAriaLabel: 'TRANSLATE:TIME_PICKER_HOUR_DECREASE_ARIA_LABEL',
  timePickerMinuteIncreaseBtnAriaLabel: 'TRANSLATE:TIME_PICKER_MINUTE_INCREASE_ARIA_LABEL',
  timePickerMinuteDecreaseBtnAriaLabel: 'TRANSLATE:TIME_PICKER_MINUTE_DECREASE_ARIA_LABEL',
  timeAmAriaLabel: 'TRANSLATE:TIME_PICKER_AM',
  timePmAriaLabel: 'TRANSLATE:TIME_PICKER_PM',
  monthYearBtnAriaLabel: 'TRANSLATE:DATE_PICKER_MONTH_BUTTON_ARIA_LABEL',
  monthAriaLabel: 'TRANSLATE:DATE_PICKER_MONTH_DROPDOWN_ARIA_LABEL',
  yearAriaLabel: 'TRANSLATE:DATE_PICKER_YEAR_DROPDOWN_ARIA_LABEL',
  amPmSwitcherLabel: 'TRANSLATE:AM_PM_SWITCHER',
  enablePMTitle: 'TRANSLATE:CLICK_TO_ENABLE_PM',
  enableAMTitle: 'TRANSLATE:CLICK_TO_ENABLE_AM',
  yearsBeforeCurrent: 100,
  yearsAfterCurrent: 100,
  hasStringFormat: false,
  hasMomentFormat: false,
  hasTimestampFormat: false
}));