function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

import _pt from "prop-types";

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

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

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React from 'react';
import TextField from '../TextField/TextField';
import classNames from 'classnames';
import { t } from '../i18n';
export var DateInput = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(DateInput, _React$PureComponent);

  var _super = _createSuper(DateInput);

  function DateInput(props) {
    var _this;

    _classCallCheck(this, DateInput);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "monthInput", void 0);

    _defineProperty(_assertThisInitialized(_this), "dayInput", void 0);

    _defineProperty(_assertThisInitialized(_this), "yearInput", void 0);

    _this.handleBlur = _this.handleBlur.bind(_assertThisInitialized(_this));
    _this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(DateInput, [{
    key: "formatDate",
    value: function formatDate() {
      if (this.props.dateFormatter && this.monthInput && this.dayInput && this.yearInput) {
        var values = {
          month: this.monthInput.value,
          day: this.dayInput.value,
          year: this.yearInput.value
        };
        return this.props.dateFormatter(values);
      }
    }
  }, {
    key: "handleBlur",
    value: function handleBlur(evt) {
      if (this.props.onBlur) {
        this.props.onBlur(evt, this.formatDate());
      }

      if (this.props.onComponentBlur) {
        this.handleComponentBlur(evt);
      }
    }
  }, {
    key: "handleChange",
    value: function handleChange(evt) {
      this.props.onChange(evt, this.formatDate());
    }
  }, {
    key: "handleComponentBlur",
    value: function handleComponentBlur(evt) {
      var _this2 = this;

      // The active element is always the document body during a focus
      // transition, so in order to check if the newly focused element
      // is one of our other date inputs, we're going to have to wait
      // a bit.
      setTimeout(function () {
        if (document.activeElement !== _this2.dayInput && document.activeElement !== _this2.monthInput && document.activeElement !== _this2.yearInput) {
          _this2.props.onComponentBlur(evt, _this2.formatDate());
        }
      }, 20);
    }
  }, {
    key: "renderField",
    value: function renderField(type, maxLength) {
      var _this$props$,
          _this3 = this;

      var sharedTextFieldProps = {
        className: 'ds-l-col--auto',
        labelClassName: 'ds-c-datefield__label',
        disabled: this.props.disabled,
        inversed: this.props.inversed,
        onBlur: (this.props.onBlur || this.props.onComponentBlur) && this.handleBlur,
        onChange: this.props.onChange && this.handleChange,
        numeric: true
      };
      return /*#__PURE__*/React.createElement(TextField, _extends({}, sharedTextFieldProps, {
        defaultValue: this.props["".concat(type, "DefaultValue")],
        value: this.props["".concat(type, "Value")],
        label: (_this$props$ = this.props["".concat(type, "Label")]) !== null && _this$props$ !== void 0 ? _this$props$ : t("dateField.".concat(type, "Label")),
        name: this.props["".concat(type, "Name")],
        maxLength: maxLength,
        fieldClassName: classNames("ds-c-field--".concat(type), {
          'ds-c-field--error': this.props["".concat(type, "Invalid")]
        }),
        inputRef: function inputRef(el) {
          _this3["".concat(type, "Input")] = el;
          if (_this3.props["".concat(type, "FieldRef")]) _this3.props["".concat(type, "FieldRef")](el);
        },
        autoComplete: this.props.autoComplete && "bday-".concat(type),
        "aria-describedby": this.props.labelId,
        "aria-invalid": this.props["".concat(type, "Invalid")]
      }));
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement("div", {
        className: "ds-c-datefield__container ds-l-form-row"
      }, this.renderField('month', 2), /*#__PURE__*/React.createElement("span", {
        className: "ds-c-datefield__separator"
      }, "/"), this.renderField('day', 2), /*#__PURE__*/React.createElement("span", {
        className: "ds-c-datefield__separator"
      }, "/"), this.renderField('year', 4));
    }
  }]);

  return DateInput;
}(React.PureComponent);

_defineProperty(DateInput, "propTypes", {
  /**
     * Adds `autocomplete` attributes `bday-day`, `bday-month` and `bday-year` to the corresponding `<DateInput>` inputs
     */
  autoComplete: _pt.bool,

  /**
     * Additional classes to be added to the root fieldset element
     */
  className: _pt.string,

  /**
     * Optional method to format the `input` field values. If this
     * method is provided, the returned value will be passed as a second argument
     * to the `onBlur` and `onChange` callbacks. This method receives an object as
     * its only argument, in the shape of: `{ day, month, year }`
     * By default `dateFormatter` will be set to the `defaultDateFormatter` function, which prevents days/months more than 2 digits & years more than 4 digits.
     */
  dateFormatter: _pt.func,

  /**
     * Disables all three input fields.
     */
  disabled: _pt.bool,

  /**
     * Applies the "inverse" UI theme
     */
  inversed: _pt.bool,

  /**
     * A unique ID applied to the DateField label.
     */
  labelId: _pt.string.isRequired,

  /**
     * Called anytime any date input is blurred
     */
  onBlur: _pt.func,

  /**
     * Called when any date input is blurred and the focus does not land on one
     * of the other date inputs inside this component (i.e., when the whole
     * component loses focus)
     */
  onComponentBlur: _pt.func,

  /**
     * Called anytime any date input is changed
     */
  onChange: _pt.func,

  /**
     * Label for the day field
     */
  dayLabel: _pt.node,

  /**
     * `name` for the day `input` field
     */
  dayName: _pt.string.isRequired,

  /**
     * Initial value for the day `input` field. Use this for an uncontrolled
     * component; otherwise, use the `dayValue` property.
     */
  dayDefaultValue: _pt.oneOfType([_pt.string, _pt.number]),

  /**
     * Access a reference to the day `input`
     */
  dayFieldRef: _pt.func,

  /**
     * Apply error styling to the day `input`
     */
  dayInvalid: _pt.bool,

  /**
     * Sets the day input's `value`. Use this in combination with `onChange`
     * for a controlled component; otherwise, set `dayDefaultValue`.
     */
  dayValue: _pt.oneOfType([_pt.string, _pt.number]),

  /**
     * Label for the month field
     */
  monthLabel: _pt.node,

  /**
     * `name` for the month `input` field
     */
  monthName: _pt.string.isRequired,

  /**
     * Initial value for the month `input` field. Use this for an uncontrolled
     * component; otherwise, use the `monthValue` property.
     */
  monthDefaultValue: _pt.oneOfType([_pt.string, _pt.number]),

  /**
     * Access a reference to the month `input`
     */
  monthFieldRef: _pt.func,

  /**
     * Apply error styling to the month `input`
     */
  monthInvalid: _pt.bool,

  /**
     * Sets the month input's `value`. Use this in combination with `onChange`
     * for a controlled component; otherwise, set `monthDefaultValue`.
     */
  monthValue: _pt.oneOfType([_pt.string, _pt.number]),

  /**
     * Initial value for the year `input` field. Use this for an uncontrolled
     * component; otherwise, use the `yearValue` property.
     */
  yearDefaultValue: _pt.oneOfType([_pt.string, _pt.number]),

  /**
     * Access a reference to the year `input`
     */
  yearFieldRef: _pt.func,

  /**
     * Apply error styling to the year `input`
     */
  yearInvalid: _pt.bool,

  /**
     * Label for the year `input` field
     */
  yearLabel: _pt.node
});

export default DateInput;