'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _DatePicker = require('../DatePicker/DatePicker');

var _DatePicker2 = _interopRequireDefault(_DatePicker);

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

var _TimePicker2 = _interopRequireDefault(_TimePicker);

var _DateTimeRangePickerContainer = require('./styles/DateTimeRangePickerContainer');

var _DateTimeRangePickerContainer2 = _interopRequireDefault(_DateTimeRangePickerContainer);

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

var _lodash = require('lodash');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * **DateTimeRangePicker** combines the functionality of DatePicker.Range and TimePicker
 * into a single interface. It passes all of its props into DatePicker.Range.
 */
var DateTimeRangePicker = function (_React$PureComponent) {
  (0, _inherits3.default)(DateTimeRangePicker, _React$PureComponent);

  function DateTimeRangePicker() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, DateTimeRangePicker);

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = DateTimeRangePicker.__proto__ || (0, _getPrototypeOf2.default)(DateTimeRangePicker)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      internalValue: { start: undefined, end: undefined }
    }, _this.startDateDisabled = function () {
      return [true, 'startDate'].includes(_this.props.disabled);
    }, _this.endDateDisabled = function () {
      return [true, 'endDate'].includes(_this.props.disabled);
    }, _this.triggerOnChange = function (_ref2) {
      var start = _ref2.start,
          end = _ref2.end;
      return _this.props.onChange({
        start: _this.startDateDisabled() ? null : start,
        end: _this.endDateDisabled() ? null : end
      });
    }, _this.handleStartTimeChange = function (time) {
      var start = _this.momentStartValue.hours(time.hours()).minutes(time.minutes());
      var newState = {
        start: start,
        end: _this.momentEndValue
      };
      _this.triggerOnChange(newState);
      _this.setState({ internalValue: newState });
    }, _this.handleEndTimeChange = function (time) {
      var end = _this.momentEndValue.hours(time.hours()).minutes(time.minutes());
      var newState = {
        start: _this.momentStartValue,
        end: end
      };
      _this.triggerOnChange(newState);
      _this.setState({ internalValue: newState });
    }, _this.handleDatesChange = function (_ref3) {
      var startDate = _ref3.startDate,
          endDate = _ref3.endDate;

      var newState = { start: _this.momentStartValue, end: _this.momentEndValue };
      if (startDate) {
        newState.start = _this.moment(startDate).hours(_this.momentStartValue.hours()).minutes(_this.momentStartValue.minutes());
      }
      if (endDate) {
        newState.end = _this.moment(endDate).hours(_this.momentEndValue.hours()).minutes(_this.momentEndValue.minutes());
      }
      _this.triggerOnChange(newState);
      _this.setState({ internalValue: newState });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(DateTimeRangePicker, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          onChange = _props.onChange,
          disabledStartText = _props.disabledStartText,
          disabledEndText = _props.disabledEndText,
          rest = (0, _objectWithoutProperties3.default)(_props, ['onChange', 'disabledStartText', 'disabledEndText']);


      return _react2.default.createElement(_DatePicker2.default.Range, (0, _extends3.default)({
        startDatePlaceholderText: this.startDateDisabled() ? disabledStartText : null,
        endDatePlaceholderText: this.endDateDisabled() ? disabledEndText : null,
        startDate: this.startDateDisabled() ? null : this.momentStartValue,
        endDate: this.endDateDisabled() ? null : this.momentEndValue,
        onDatesChange: this.handleDatesChange,
        calendarInfoPosition: 'top',
        renderCalendarInfo: function renderCalendarInfo() {
          return _react2.default.createElement(
            _DateTimeRangePickerContainer2.default,
            null,
            _this2.startDateDisabled() || _react2.default.createElement(_TimePicker2.default, {
              name: 'start-time-picker',
              timeFormat: _this2.props.timeDisplayFormat,
              value: _this2.momentStartValue,
              onChange: _this2.handleStartTimeChange
            }),
            _this2.endDateDisabled() || _react2.default.createElement(_TimePicker2.default, {
              name: 'end-time-picker',
              timeFormat: _this2.props.timeDisplayFormat,
              value: _this2.momentEndValue,
              onChange: _this2.handleEndTimeChange
            })
          );
        }
      }, rest));
    }
  }, {
    key: 'moment',
    get: function get() {
      return this.props.utc ? _moment2.default.utc : _moment2.default;
    }
  }, {
    key: 'value',
    get: function get() {
      return this.props.value ? this.props.value : this.state.internalValue;
    }
  }, {
    key: 'startValue',
    get: function get() {
      return this.value && this.value.start;
    }
  }, {
    key: 'endValue',
    get: function get() {
      return this.value && this.value.end;
    }
  }, {
    key: 'momentStartValue',
    get: function get() {
      return this.moment(this.startValue);
    }
  }, {
    key: 'momentEndValue',
    get: function get() {
      return this.moment(this.endValue);
    }

    // Trigger onChange prop

  }]);
  return DateTimeRangePicker;
}(_react2.default.PureComponent);

DateTimeRangePicker.propTypes = {
  /**
   * Callback invoked when the datetime is changed. Includes an object with the signature `{startDatetime, endDatetime}`.
   * The values are provided as moment objects.
   */
  onChange: _propTypes2.default.func.isRequired,
  /**
   * Moment date time format to display in the input
   */
  displayFormat: _propTypes2.default.string,
  /**
   * Moment date time format to display in the time picker
   */
  timeDisplayFormat: _propTypes2.default.string,
  /**
   * Use UTC datetime.
   */
  utc: _propTypes2.default.bool,
  /**
   * Text to use when the start date is disabled.
   */
  disabledStartText: _propTypes2.default.string,
  /**
   * Text to use when the end date is disabled.
   */
  disabledEndText: _propTypes2.default.string,
  /**
   * The current datetimes being displayed. If unset, the component will track its own internal state.
   * Datetime should be provided as an object `{start: <moment>, end: <moment>}`, where <moment>
   * is a moment object.
   */
  value: _propTypes2.default.object,
  /**
   * Set to true if the component's value is invalid
   */
  invalid: _propTypes2.default.bool,
  /**
   * Set to true if the component should be disabled. Alternatively, set to either 'startDate' or 'endDate'
   * to disable a particular part of the component.
   */
  disabled: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.string])
};
DateTimeRangePicker.defaultProps = {
  onChange: _lodash.noop,
  displayFormat: 'MMM DD YYYY [at] hh:mm A',
  timeDisplayFormat: 'hh:mm',
  utc: false,
  disabledStartText: 'Now',
  disabledEndText: 'Forever',
  value: undefined,
  invalid: false,
  disabled: false
};
exports.default = DateTimeRangePicker;