'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 _DateTimePickerContainer = require('./styles/DateTimePickerContainer');

var _DateTimePickerContainer2 = _interopRequireDefault(_DateTimePickerContainer);

var _DateTimeRangePicker = require('./DateTimeRangePicker');

var _DateTimeRangePicker2 = _interopRequireDefault(_DateTimeRangePicker);

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

var _lodash = require('lodash');

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

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

  function DateTimePicker() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = DateTimePicker.__proto__ || (0, _getPrototypeOf2.default)(DateTimePicker)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      internalValue: undefined
    }, _this.handleTimeChange = function (time) {
      // Set time on existing datetime
      var newDatetime = _this.momentValue.hours(time.hours()).minutes(time.minutes());
      _this.props.onChange(newDatetime);
      _this.setState({ internalValue: newDatetime });
    }, _this.handleDateChange = function (date) {
      // Set time from existing datetime on incoming date, since it's easier to set hours/minutes than year/month/day
      var newDatetime = _this.moment(date).hours(_this.momentValue.hours()).minutes(_this.momentValue.minutes());
      _this.props.onChange(newDatetime);
      _this.setState({ internalValue: newDatetime });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

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

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

      return _react2.default.createElement(_DatePicker2.default, (0, _extends3.default)({
        date: this.momentValue,
        onDateChange: this.handleDateChange,
        calendarInfoPosition: 'top',
        renderCalendarInfo: function renderCalendarInfo() {
          return _react2.default.createElement(
            _DateTimePickerContainer2.default,
            null,
            _react2.default.createElement(_TimePicker2.default, {
              name: 'time-picker',
              value: _this2.momentValue,
              onChange: _this2.handleTimeChange
            })
          );
        }
      }, 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: 'momentValue',
    get: function get() {
      return this.moment(this.value);
    }
  }]);
  return DateTimePicker;
}(_react2.default.PureComponent);

DateTimePicker.propTypes = {
  /**
   * A function to handle the datetime when it changes.
   */
  onChange: _propTypes2.default.func.isRequired,
  /**
   * Moment date time format to display in the input
   */
  displayFormat: _propTypes2.default.string,
  /**
   * Use UTC datetime.
   */
  utc: _propTypes2.default.bool,
  /**
   * The current datetime being displayed. If unset, the component will track its own internal state.
   * Datetime should be provided as 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.
   */
  disabled: _propTypes2.default.bool
};
DateTimePicker.defaultProps = {
  onChange: _lodash.noop,
  displayFormat: 'MMM DD YYYY [at] hh:mm A',
  utc: false,
  value: undefined,
  invalid: false,
  disabled: false
};


DateTimePicker.Range = _DateTimeRangePicker2.default;

exports.default = DateTimePicker;