'use strict';

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

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _Checkbox = require('../../../components/Checkbox');

var _Checkbox2 = _interopRequireDefault(_Checkbox);

var _Toggle = require('../../../components/Toggle');

var _Toggle2 = _interopRequireDefault(_Toggle);

var _TextArea = require('../../../components/TextArea');

var _TextArea2 = _interopRequireDefault(_TextArea);

var _RadioGroup = require('../../../components/RadioGroup');

var _RadioGroup2 = _interopRequireDefault(_RadioGroup);

var _Label = require('../../../components/Label');

var _Label2 = _interopRequireDefault(_Label);

var _FlowItem = require('../FlowItem');

var _FlowItem2 = _interopRequireDefault(_FlowItem);

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

/**
 * Creates a wrapper which renders a redux-form Field component as a FlowItem
 */
exports.default = function (FieldComponent) {
  var _class, _temp2;

  return _temp2 = _class = function (_React$Component) {
    (0, _inherits3.default)(FlowField, _React$Component);

    function FlowField() {
      var _ref;

      var _temp, _this, _ret;

      (0, _classCallCheck3.default)(this, FlowField);

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

      return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = FlowField.__proto__ || (0, _getPrototypeOf2.default)(FlowField)).call.apply(_ref, [this].concat(args))), _this), _this.hasFlexibleContent = FieldComponent === _TextArea2.default, _this.alignment = [_RadioGroup2.default, _Toggle2.default, _Checkbox2.default].includes(FieldComponent) ? 'left' : 'stretch', _this.renderLabel = function () {
        var _this$props = _this.props,
            label = _this$props.label,
            required = _this$props.required;

        return _react2.default.createElement(
          _Label2.default,
          { className: 'FlowItem-label', required: required },
          label
        );
      }, _this.renderInput = function () {
        var _this$props2 = _this.props,
            input = _this$props2.input,
            meta = _this$props2.meta,
            required = _this$props2.required,
            disabled = _this$props2.disabled,
            id = _this$props2.id,
            className = _this$props2.className,
            options = _this$props2.options,
            choices = _this$props2.choices,
            description = _this$props2.description,
            type = _this$props2.type,
            extraProps = _this$props2.extraProps;


        var passedProps = (0, _extends3.default)({}, extraProps, input, meta, {
          error: !!meta.error,
          required: required,
          disabled: disabled,
          id: id,
          className: className,
          options: options,
          choices: choices,
          description: description,
          type: type
        });

        return _react2.default.createElement(FieldComponent, passedProps);
      }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
    }

    /**
     * Renders the label with required state.
     */


    /**
     * Renders the input component with desired passed props.
     */


    (0, _createClass3.default)(FlowField, [{
      key: 'render',
      value: function render() {
        var _props = this.props,
            moreContent = _props.moreContent,
            meta = _props.meta,
            helpText = _props.helpText;


        return _react2.default.createElement(
          _FlowItem2.default,
          {
            label: this.renderLabel(),
            helpText: helpText,
            moreContent: moreContent,
            flexibleContent: this.hasFlexibleContent,
            error: meta.error,
            alignment: this.alignment
          },
          this.renderInput()
        );
      }
    }]);
    return FlowField;
  }(_react2.default.Component), _class.propTypes = (0, _extends3.default)({}, _FlowItem2.default.propTypes, {
    required: _propTypes2.default.bool,
    disabled: _propTypes2.default.bool,
    options: _propTypes2.default.array,
    choices: _propTypes2.default.array,
    description: _propTypes2.default.string,
    type: _propTypes2.default.string,
    input: _propTypes2.default.shape({
      checked: _propTypes2.default.bool,
      name: _propTypes2.default.string,
      onBlur: _propTypes2.default.func,
      onChange: _propTypes2.default.func,
      onDragStart: _propTypes2.default.func,
      onDrop: _propTypes2.default.func,
      onFocus: _propTypes2.default.func,
      value: _propTypes2.default.any
    }).isRequired,
    meta: _propTypes2.default.shape({
      error: _propTypes2.default.string,
      active: _propTypes2.default.bool,
      autofilled: _propTypes2.default.bool,
      asyncValidating: _propTypes2.default.bool,
      dirty: _propTypes2.default.bool,
      dispatch: _propTypes2.default.func,
      form: _propTypes2.default.string,
      initial: _propTypes2.default.any,
      invalid: _propTypes2.default.bool,
      pristine: _propTypes2.default.bool,
      submitting: _propTypes2.default.bool,
      submitFailed: _propTypes2.default.bool,
      touched: _propTypes2.default.bool,
      valid: _propTypes2.default.bool,
      visited: _propTypes2.default.bool,
      warning: _propTypes2.default.string
    }),
    extraProps: _propTypes2.default.object
  }), _class.defaultProps = (0, _extends3.default)({}, _FlowItem2.default.defaultProps, {
    meta: {},
    required: false,
    disabled: false,
    options: null,
    choices: null,
    description: null,
    type: null,
    extraProps: {}
  }), _temp2;
};