'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _pikaday = require('pikaday');

var _pikaday2 = _interopRequireDefault(_pikaday);

require('./style.css');

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

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; }

var InputDate = function (_React$Component) {
  _inherits(InputDate, _React$Component);

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

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.picker = null;
    _this.startPikaday = _this.startPikaday.bind(_this);
    _this.handleChange = _this.handleChange.bind(_this);
    return _this;
  }

  InputDate.prototype.componentDidMount = function componentDidMount() {
    this.startPikaday();
    this.picker.setDate(this.props.defaultDate, true);
  };

  InputDate.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    this.picker.setDate(nextProps.defaultDate, true);
  };

  InputDate.prototype.handleChange = function handleChange(date) {
    if (typeof this.props.onChangeCallback === 'function') {
      this.props.onChangeCallback({ date: date });
    }
  };

  InputDate.prototype.startPikaday = function startPikaday() {
    var restrict = this.props.restrict;
    var pickerOptions = {
      field: this.dateInput,
      format: this.props.format,
      formatStrict: true,
      onSelect: this.handleChange,
      setDefaultDate: false
    };
    if (this.props.defaultDate !== null) {
      pickerOptions.defaultDate = this.props.defaultDate;
      pickerOptions.setDefaultDate = true;
    }
    this.picker = new _pikaday2.default(pickerOptions);
    this.dateInput.setAttribute('type', 'text');
    if (restrict === 'max') {
      this.picker.setMaxDate(new Date());
    } else if (restrict === 'min') {
      this.picker.setMinDate(new Date());
    }
  };

  InputDate.prototype.render = function render() {
    var _this2 = this;

    var classNames = this.props.required ? 'ma__input-date js-input-date js-is-required' : 'ma__input-date js-input-date ';
    return _react2.default.createElement(
      _react2.default.Fragment,
      null,
      _react2.default.createElement(
        'label',
        { htmlFor: this.props.id },
        this.props.labelText
      ),
      _react2.default.createElement('input', {
        className: classNames,
        name: this.props.name,
        id: this.props.id,
        type: 'date',
        placeholder: this.props.placeholder,
        'data-type': 'date',
        'data-restrict': this.props.restrict,
        ref: function ref(input) {
          _this2.dateInput = input;
        },
        required: this.props.required,
        format: this.props.format
      })
    );
  };

  return InputDate;
}(_react2.default.Component);

InputDate.propTypes = process.env.NODE_ENV !== "production" ? {
  /** The label text above the input field */
  labelText: _propTypes2.default.string.isRequired,
  /** Whether an input date is required or not */
  required: _propTypes2.default.bool,
  /** The id on the input date element */
  id: _propTypes2.default.string.isRequired,
  /** The name of the input date element */
  name: _propTypes2.default.string.isRequired,
  /** The placeholder text in the input box, prompting users for a value */
  placeholder: _propTypes2.default.string,
  /** Controls whether the user can pick any date (''), today and prior ('max') or today and future ('min') */
  restrict: _propTypes2.default.oneOf(['', 'max', 'min']),
  /** Controls the date format of input date . The current option are: 'M/DD/YYYY’, 'MM/DD/YYYY’', 'MMM D YYYY', or 'dddd, MMMM Do YYYY' */
  format: _propTypes2.default.oneOf(['M/DD/YYYY', 'MM/DD/YYYY', 'MM-DD-YYYY', 'YYYYMMDD']),
  /** Custom onChange function that receives the selected date input */
  onChangeCallback: _propTypes2.default.func,
  /** The date to set by default */
  defaultDate: _propTypes2.default.instanceOf(Date)
} : {};

// Only set defaults for the configuration variables which need to be opted in to activate.
InputDate.defaultProps = {
  required: false,
  restrict: '',
  defaultDate: null,
  format: 'M/DD/YYYY'
};

exports.default = InputDate;
module.exports = exports['default'];