'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
/* eslint-disable import/imports-first, import/no-unresolved */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDatePicker = require('react-date-picker');

var _DropdownInput = require('./DropdownInput');

var _DropdownInput2 = _interopRequireDefault(_DropdownInput);

var _ReadOnlyInput = require('./ReadOnlyInput');

var _ReadOnlyInput2 = _interopRequireDefault(_ReadOnlyInput);

var _changeable = require('../enhancers/changeable');

var _changeable2 = _interopRequireDefault(_changeable);

var _committable = require('../enhancers/committable');

var _committable2 = _interopRequireDefault(_committable);

var _dateHelpers = require('../helpers/dateHelpers');

var _pathHelpers = require('../helpers/pathHelpers');

var _DateInput = require('../../styles/cspace-input/DateInput.css');

var _DateInput2 = _interopRequireDefault(_DateInput);

require('!style-loader!css-loader!react-date-picker/base.css');

require('!style-loader!css-loader!../../styles/react-date-picker/cspace.css');

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

/* eslint-enable import/imports-first, import/no-unresolved */

var DropdownInput = (0, _committable2.default)((0, _changeable2.default)(_DropdownInput2.default));

var propTypes = _extends({}, _DropdownInput2.default.propTypes, {
  locale: _react.PropTypes.string,
  onCommit: _react.PropTypes.func,
  todayButtonLabel: _react.PropTypes.string,
  clearButtonLabel: _react.PropTypes.string,
  okButtonLabel: _react.PropTypes.string,
  cancelButtonLabel: _react.PropTypes.string,
  readOnly: _react.PropTypes.bool
});

var defaultProps = {
  locale: 'en',
  todayButtonLabel: 'Today',
  clearButtonLabel: 'Clear',
  okButtonLabel: 'OK',
  cancelButtonLabel: 'Cancel'
};

var DateInput = function (_Component) {
  _inherits(DateInput, _Component);

  function DateInput(props) {
    _classCallCheck(this, DateInput);

    var _this = _possibleConstructorReturn(this, (DateInput.__proto__ || Object.getPrototypeOf(DateInput)).call(this, props));

    _this.focusCalendar = _this.focusCalendar.bind(_this);
    _this.handleCalendarActiveDateChange = _this.handleCalendarActiveDateChange.bind(_this);
    _this.handleCalendarChange = _this.handleCalendarChange.bind(_this);
    _this.handleCalendarRef = _this.handleCalendarRef.bind(_this);
    _this.handleCalendarViewDateChange = _this.handleCalendarViewDateChange.bind(_this);
    _this.handleDropdownInputChange = _this.handleDropdownInputChange.bind(_this);
    _this.handleDropdownInputClose = _this.handleDropdownInputClose.bind(_this);
    _this.handleDropdownInputCommit = _this.handleDropdownInputCommit.bind(_this);
    _this.handleDropdownInputKeyPress = _this.handleDropdownInputKeyPress.bind(_this);
    _this.handleDropdownInputOpen = _this.handleDropdownInputOpen.bind(_this);
    _this.renderHistoryView = _this.renderHistoryView.bind(_this);

    _this.state = {
      date: (0, _dateHelpers.normalizeISO8601DateString)(props.value),
      open: false
    };
    return _this;
  }

  _createClass(DateInput, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      this.setState({
        date: (0, _dateHelpers.normalizeISO8601DateString)(nextProps.value)
      });
    }
  }, {
    key: 'commit',
    value: function commit(value) {
      var onCommit = this.props.onCommit;
      var initialValue = this.props.value;


      initialValue = (0, _dateHelpers.normalizeISO8601DateString)(initialValue);

      if (onCommit && (value || initialValue) && value !== initialValue) {
        onCommit((0, _pathHelpers.getPath)(this.props), value);
      }
    }
  }, {
    key: 'focusCalendar',
    value: function focusCalendar() {
      if (this.calendar) {
        this.calendar.focus();
      }
    }
  }, {
    key: 'handleCalendarActiveDateChange',
    value: function handleCalendarActiveDateChange(dateString) {
      this.setState({
        activeDate: dateString || Date.now(),
        provisionalDate: dateString
      });
    }
  }, {
    key: 'handleCalendarChange',
    value: function handleCalendarChange(dateString) {
      this.setState({
        date: dateString,
        open: false,
        provisionalDate: undefined
      });

      this.commit(dateString);
    }
  }, {
    key: 'handleCalendarRef',
    value: function handleCalendarRef(ref) {
      this.calendar = ref;
    }
  }, {
    key: 'handleCalendarViewDateChange',
    value: function handleCalendarViewDateChange(dateString) {
      this.setState({
        viewDate: dateString || Date.now()
      });
    }
  }, {
    key: 'handleDropdownInputChange',
    value: function handleDropdownInputChange(value) {
      var dateString = (0, _dateHelpers.normalizeDateString)(value, this.props.locale);

      var newState = {
        activeDate: dateString,
        provisionalDate: value,
        open: true
      };

      if (dateString !== null) {
        newState.viewDate = dateString;
      }

      this.setState(newState);
    }
  }, {
    key: 'handleDropdownInputClose',
    value: function handleDropdownInputClose() {
      this.setState({
        open: false,
        provisionalDate: undefined
      });
    }
  }, {
    key: 'handleDropdownInputCommit',
    value: function handleDropdownInputCommit() {
      var provisionalDate = this.state.provisionalDate;


      if (typeof provisionalDate !== 'undefined') {
        var dateString = void 0;

        if (provisionalDate === '') {
          dateString = '';
        } else {
          dateString = (0, _dateHelpers.normalizeDateString)(provisionalDate, this.props.locale);
        }

        if (dateString !== null) {
          this.setState({
            date: dateString,
            open: false,
            provisionalDate: undefined
          });

          this.commit(dateString);
        }
      }
    }
  }, {
    key: 'handleDropdownInputKeyPress',
    value: function handleDropdownInputKeyPress(event) {
      if (event.key === 'Enter') {
        var provisionalDate = this.state.provisionalDate;


        if (typeof provisionalDate !== 'undefined') {
          event.preventDefault();
        }
      }
    }
  }, {
    key: 'handleDropdownInputOpen',
    value: function handleDropdownInputOpen() {
      var _state = this.state,
          date = _state.date,
          provisionalDate = _state.provisionalDate;


      var activeDate = provisionalDate || date || Date.now();

      this.setState({
        activeDate: activeDate,
        viewDate: activeDate,
        open: true
      });
    }
  }, {
    key: 'renderHistoryView',
    value: function renderHistoryView(historyViewProps) {
      var _props = this.props,
          okButtonLabel = _props.okButtonLabel,
          cancelButtonLabel = _props.cancelButtonLabel;


      return _react2.default.createElement(
        _reactDatePicker.HistoryView,
        historyViewProps,
        _react2.default.createElement(_reactDatePicker.Footer, {
          todayButton: false,
          clearButton: false,
          okButtonText: okButtonLabel,
          cancelButtonText: cancelButtonLabel
        })
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          locale = _props2.locale,
          todayButtonLabel = _props2.todayButtonLabel,
          clearButtonLabel = _props2.clearButtonLabel,
          readOnly = _props2.readOnly,
          okButtonLabel = _props2.okButtonLabel,
          cancelButtonLabel = _props2.cancelButtonLabel,
          remainingProps = _objectWithoutProperties(_props2, ['locale', 'todayButtonLabel', 'clearButtonLabel', 'readOnly', 'okButtonLabel', 'cancelButtonLabel']);

      var _state2 = this.state,
          open = _state2.open,
          date = _state2.date,
          activeDate = _state2.activeDate,
          provisionalDate = _state2.provisionalDate,
          viewDate = _state2.viewDate;


      if (readOnly) {
        return _react2.default.createElement(_ReadOnlyInput2.default, { value: date });
      }

      var value = typeof provisionalDate === 'undefined' ? date : provisionalDate;
      var className = typeof provisionalDate === 'undefined' ? _DateInput2.default.normal : _DateInput2.default.provisional;

      return _react2.default.createElement(
        DropdownInput,
        _extends({}, remainingProps, {
          className: className,
          commitUnchanged: true,
          focusPopup: this.focusCalendar,
          open: open,
          onChange: this.handleDropdownInputChange,
          onClose: this.handleDropdownInputClose,
          onCommit: this.handleDropdownInputCommit,
          onKeyPress: this.handleDropdownInputKeyPress,
          onOpen: this.handleDropdownInputOpen,
          value: value
        }),
        _react2.default.createElement(
          _reactDatePicker.MonthView,
          {
            activeDate: activeDate,
            date: date,
            footer: true,
            highlightToday: false,
            highlightWeekends: false,
            locale: locale,
            ref: this.handleCalendarRef,
            theme: 'cspace',
            viewDate: viewDate,
            weekNumbers: false,
            onChange: this.handleCalendarChange,
            onViewDateChange: this.handleCalendarViewDateChange,
            onActiveDateChange: this.handleCalendarActiveDateChange
          },
          _react2.default.createElement(_reactDatePicker.NavBar, {
            renderHistoryView: this.renderHistoryView,
            secondary: true
          }),
          _react2.default.createElement(_reactDatePicker.Footer, {
            todayButtonText: todayButtonLabel,
            clearButtonText: clearButtonLabel,
            theme: 'cspace'
          })
        )
      );
    }
  }]);

  return DateInput;
}(_react.Component);

exports.default = DateInput;


DateInput.propTypes = propTypes;
DateInput.defaultProps = defaultProps;