import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import React, { Component } from 'react';
import Pikaday from 'pikaday';

var DatePicker =
/*#__PURE__*/
function (_Component) {
  _inherits(DatePicker, _Component);

  function DatePicker(props, context) {
    var _this;

    _classCallCheck(this, DatePicker);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(DatePicker).call(this, props, context));

    _this.onSelect = function (date) {
      var input = _this.props.input;
      var newDate = new Date(date).toISOString(); // update redux-form

      input.onChange(newDate); // update display field

      _this.setState({
        displayText: newDate.slice(0, 10)
      });
    };

    _this.initPikaday = function () {
      var _this$props = _this.props,
          input = _this$props.input,
          minDate = _this$props.minDate;
      _this.picker = new Pikaday({
        field: _this.pickerRef.current,
        onSelect: _this.onSelect,
        minDate: minDate,
        yearRange: [new Date().getFullYear(), new Date().getFullYear() + 30],
        trigger: _this.displayRef.current
      });

      if (input.value) {
        _this.picker.setDate(input.value);
      }
    };

    _this.state = {
      displayText: ''
    };
    _this.pickerRef = React.createRef();
    _this.displayRef = React.createRef();
    _this.picker = null;
    return _this;
  }

  _createClass(DatePicker, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.initPikaday();
    }
  }, {
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      // handle form resets
      if (!nextProps.input.value) {
        this.setState({
          displayText: ''
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          input = _this$props2.input,
          minDate = _this$props2.minDate,
          disabled = _this$props2.disabled,
          props = _objectWithoutProperties(_this$props2, ["input", "minDate", "disabled"]);

      var displayText = this.state.displayText;
      return React.createElement("div", {
        className: "fieldDate has-feedback"
      }, React.createElement("input", {
        ref: this.displayRef,
        className: "form-control",
        disabled: disabled,
        value: displayText,
        onChange: function onChange() {}
      }), !disabled && React.createElement("span", {
        className: "ca-gov-icon-calendar form-control-feedback"
      }), React.createElement("input", Object.assign({
        ref: this.pickerRef
      }, props, {
        className: "hidden"
      })), React.createElement("input", {
        className: "hidden",
        id: input.name,
        onChange: function onChange(v) {
          console.log('changed: ', v);
        }
      }));
    }
  }]);

  return DatePicker;
}(Component);

export default DatePicker;