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

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

import React, { Component } from 'react';
import { Box } from 'grommet/es6/components/Box';
import { Calendar } from 'grommet/es6/components/Calendar';
import { Calendar as CalendarIcon } from 'grommet-icons/es6/icons/Calendar';

import { MaskedInput } from '../MaskedInput';
import { smallDate } from '../../utils/moment';

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

  function DateInput() {
    var _temp, _this, _ret;

    _classCallCheck(this, DateInput);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.onSelect = function (isoDate) {
      var date = new Date(isoDate);
      _this.upDateValue(smallDate(date));
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

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

    var _props = this.props,
        value = _props.value,
        bounds = _props.bounds,
        dates = _props.dates,
        disabledDates = _props.disabledDates,
        autocorrect = _props.autocorrect,
        firstDayOfWeek = _props.firstDayOfWeek,
        locale = _props.locale,
        size = _props.size,
        userMask = _props.mask,
        rest = _objectWithoutProperties(_props, ['value', 'bounds', 'dates', 'disabledDates', 'autocorrect', 'firstDayOfWeek', 'locale', 'size', 'mask']);

    var mask = void 0;
    if (userMask) {
      mask = { mask: userMask };
    } else if (autocorrect) {
      mask = {
        mask: [/\d/, /\d/, '/', /\d/, /\d/, '/', /\d/, /\d/, /\d/, /\d/],
        pipe: MaskedInput.createAutoCorrectedDatePipe(),
        keepCharPositions: true
      };
    } else {
      mask = { mask: [/\d/, /\d/, '/', /\d/, /\d/, '/', /\d/, /\d/, /\d/, /\d/] };
    }
    return React.createElement(MaskedInput, _extends({
      update: function update(_update) {
        _this2.upDateValue = _update;
      },
      value: value,
      dropContent: React.createElement(
        Box,
        { pad: 'small' },
        React.createElement(Calendar, {
          date: value,
          onSelect: this.onSelect,
          bounds: bounds,
          dates: dates,
          disabled: disabledDates,
          firstDayOfWeek: firstDayOfWeek,
          locale: locale,
          size: size
        })
      )
    }, _extends({}, rest, mask)));
  };

  return DateInput;
}(Component);

DateInput.defaultProps = {
  dropIcon: React.createElement(CalendarIcon, null),
  size: 'small',
  a11yDropTitle: 'Open calendar'
};


var DateInputDoc = void 0;
if (process.env.NODE_ENV !== 'production') {
  DateInputDoc = require('./doc').default(DateInput); // eslint-disable-line global-require
}

var DateInputWrapper = DateInputDoc || DateInput;

export { DateInputWrapper as DateInput };