import _extends from "@babel/runtime/helpers/extends";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import { TextInputControl } from '../TextInputControl';
import { DayPickerSingleDateController } from 'react-dates';
import moment from 'moment';
import { TimePicker } from './TimePicker';
import { is24HoursMode } from './datePickerHelpers';
import { DatePickerBase } from './DatePickerBase';
import { MonthYearDropDown } from './MonthYearDropDown';
import { Button } from '../../Button';
import { translate } from '../../../../utils';
import { playMessage } from '../../../utils/playMessage';
import { renderCalendarDayContent } from './renderCalendarDayContent';
import { wait } from '../../..';
var WAIT_CALENDAR_READY_TIMEOUT = 1;
export var SingleDatePickerInputControl =
/*#__PURE__*/
function (_DatePickerBase) {
  _inheritsLoose(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;

    _defineProperty(_assertThisInitialized(_this), "state", Object.assign({}, _DatePickerBase.prototype.state, {
      isMonthYearEditing: false,
      value: _this.getDateTimeValue()
    }));

    _defineProperty(_assertThisInitialized(_this), "notifyDateTimeChange", function (momentValue) {
      var value = moment(momentValue);

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

      onChange(_this.getFormattedReturnValue(value));
    });

    _defineProperty(_assertThisInitialized(_this), "onDateChange", function (newValue) {
      var oldValue = _this.getDateTimeValue();

      var oldMomentValue = moment(oldValue);
      var value = moment(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();
    });

    _defineProperty(_assertThisInitialized(_this), "onTimeChange", function (_ref) {
      var hours = _ref.hours,
          minutes = _ref.minutes;
      var newValue = moment(_this.getDateTimeMomentValue()).hour(hours).minute(minutes);

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

        playMessage(_this.getTimePickerPromptMessage(newValue));
      });
    });

    _defineProperty(_assertThisInitialized(_this), "initialVisibleMonth", function () {
      var value = _this.getDateTimeValue();

      return value ? moment(value) : moment();
    });

    _defineProperty(_assertThisInitialized(_this), "handleSwitchToMonthYearEditing", function () {
      return _this.blurDateCell().then(function () {
        return _this.setState({
          isMonthYearEditing: true
        });
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onSwitchToMonthYearText", function () {
      return _this.setState({
        isMonthYearEditing: false
      });
    });

    _defineProperty(_assertThisInitialized(_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.createElement(MonthYearDropDown, {
        month: month,
        onMonthSelect: onMonthSelectHandler,
        onYearSelect: onYearSelectHandler,
        monthAriaLabel: monthAriaLabel,
        yearAriaLabel: yearAriaLabel,
        yearsBeforeCurrent: yearsBeforeCurrent,
        yearsAfterCurrent: yearsAfterCurrent
      });
    });

    _defineProperty(_assertThisInitialized(_this), "renderMonthYearText", function (_ref3) {
      var month = _ref3.month;
      var _this$props2 = _this.props,
          monthYearFormat = _this$props2.monthYearFormat,
          monthYearBtnAriaLabel = _this$props2.monthYearBtnAriaLabel;
      var ariaLabel = translate(monthYearBtnAriaLabel, {
        month: month.format('MMMM'),
        year: month.year()
      });
      return React.createElement(Button, {
        testAutomationId: "editYearMonthButton",
        bsStyle: "link",
        onClick: _this.handleSwitchToMonthYearEditing,
        "aria-label": ariaLabel
      }, React.createElement("strong", null, month.format(monthYearFormat)));
    });

    _defineProperty(_assertThisInitialized(_this), "handleBackFromTimePicker", function () {
      return wait(WAIT_CALENDAR_READY_TIMEOUT).then(_this.focusOnDateCell);
    });

    _defineProperty(_assertThisInitialized(_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 = translate(timePickerAriaDescription, {
        time: moment(value).format(timeFormat)
      });
      return React.createElement("div", {
        onMouseUp: _this.blurDateCell
      }, React.createElement(TimePicker, {
        onChange: _this.onTimeChange,
        value: value,
        is24hour: 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
      }));
    });

    _defineProperty(_assertThisInitialized(_this), "getTimePickerPromptMessage", function (timeValue) {
      var _this$props4 = _this.props,
          timeFormat = _this$props4.timeFormat,
          timePickerCurrentTimeSettingAriaPrompt = _this$props4.timePickerCurrentTimeSettingAriaPrompt;
      return translate(timePickerCurrentTimeSettingAriaPrompt, {
        time: moment(timeValue).format(timeFormat)
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleMouseUpDayPickerContainer", function () {
      _this.onSwitchToMonthYearText();
    });

    _defineProperty(_assertThisInitialized(_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 (!moment(value).isValid()) {
        return value;
      }

      var formatArr = [];

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

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

      return moment(value).format(formatArr.join(' '));
    });

    _defineProperty(_assertThisInitialized(_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.utc(value) : moment(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 || !moment(value).isValid()) {
      value = moment();
    } else {
      value = moment(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.createElement(DayPickerSingleDateController, _extends({
      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: moment(value),
      isOutsideRange: function isOutsideRange(day) {
        return !isValidDate('days', day);
      },
      hideKeyboardShortcutsPanel: true,
      phrases: this.getPhrases(),
      renderDayContents: renderCalendarDayContent
    }, singleDatePickerProps)) : null;
  };

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

  return SingleDatePickerInputControl;
}(DatePickerBase);

_defineProperty(SingleDatePickerInputControl, "propTypes", Object.assign({}, TextInputControl.propTypes, DatePickerBase.propTypes, {
  dateFormat: PropTypes.string,
  timeFormat: PropTypes.string,
  utc: PropTypes.bool,
  hasOnlyDate: PropTypes.bool,
  hasOnlyTime: PropTypes.bool,
  hasStringFormat: PropTypes.bool,
  hasMomentFormat: PropTypes.bool,
  hasTimestampFormat: PropTypes.bool,
  onChange: PropTypes.func,
  numberOfMonths: PropTypes.number,
  isValidDate: PropTypes.func,
  singleDatePickerProps: PropTypes.object,
  inputProps: PropTypes.object,
  monthYearFormat: PropTypes.string,
  ariaTextDatePickerDescribe: PropTypes.string,
  ariaTextSelectedDateValue: PropTypes.string,
  ariaTextNextMonth: PropTypes.string,
  ariaTextPrevMonth: PropTypes.string,
  timePickerAriaDescription: PropTypes.string,
  timePickerCurrentTimeSettingAriaPrompt: PropTypes.string,
  timePickerHourIncreaseBtnAriaLabel: PropTypes.string.isRequired,
  timePickerHourDecreaseBtnAriaLabel: PropTypes.string.isRequired,
  timePickerMinuteIncreaseBtnAriaLabel: PropTypes.string.isRequired,
  timePickerMinuteDecreaseBtnAriaLabel: PropTypes.string.isRequired,
  timeAmAriaLabel: PropTypes.string,
  timePmAriaLabel: PropTypes.string,
  amPmSwitcherLabel: PropTypes.string,
  enablePMTitle: PropTypes.string,
  enableAMTitle: PropTypes.string,
  monthYearBtnAriaLabel: PropTypes.string,
  monthAriaLabel: PropTypes.string,
  yearAriaLabel: PropTypes.string,
  yearsBeforeCurrent: PropTypes.number,
  yearsAfterCurrent: PropTypes.number
}));

_defineProperty(SingleDatePickerInputControl, "defaultProps", Object.assign({}, DatePickerBase.defaultProps, 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
}));