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 _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, { Component } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import uuid from 'uuid';
import Radio from './Radio';
import { asOptionObject, getValue } from './utils/options';
import Icon from '../Icon';
import MarkdownLabel from './MarkdownLabel';

class RadioGroup extends Component {
  constructor(props) {
    super(props);

    _defineProperty(this, "onChange", index => {
      const {
        input: {
          onChange
        },
        options
      } = this.props;
      return onChange(getValue(options[index]));
    });

    _defineProperty(this, "renderOption", (option, index) => {
      const {
        input: {
          value
        },
        optionComponent: OptionComponent
      } = this.props;
      const {
        value: optionValue,
        label: optionLabel,
        ...optionRest
      } = asOptionObject(option);
      const selected = optionValue === value;
      return /*#__PURE__*/React.createElement(OptionComponent, _extends({
        key: index,
        input: {
          value: index,
          checked: selected,
          onChange: () => this.onChange(index)
        },
        label: optionLabel // eslint-disable-next-line react/jsx-props-no-spreading

      }, optionRest));
    });

    this.id = uuid();
  }

  render() {
    const {
      options,
      input: {
        name
      },
      className,
      label,
      fieldLabel,
      meta: {
        error,
        invalid,
        touched
      }
    } = this.props;
    const containerClassNames = cx('form-field-container', {
      'form-field-radio-group--has-error': invalid && touched && error
    });
    const classNames = cx('form-field', 'form-field-radio-group', className);
    const anyLabel = fieldLabel || label;
    return /*#__PURE__*/React.createElement("div", {
      className: containerClassNames
    }, anyLabel && /*#__PURE__*/React.createElement(MarkdownLabel, {
      label: anyLabel
    }), /*#__PURE__*/React.createElement("div", {
      className: classNames,
      name: name
    }, options.map(this.renderOption)), invalid && touched && /*#__PURE__*/React.createElement("div", {
      className: "form-field-radio-group__error"
    }, /*#__PURE__*/React.createElement(Icon, {
      name: "warning"
    }), error));
  }

}

RadioGroup.propTypes = {
  options: PropTypes.array,
  label: PropTypes.string,
  input: PropTypes.object.isRequired,
  className: PropTypes.string,
  fieldLabel: PropTypes.string,
  meta: PropTypes.object,
  optionComponent: PropTypes.func
};
RadioGroup.defaultProps = {
  label: null,
  fieldLabel: null,
  className: null,
  optionComponent: Radio,
  options: [],
  meta: {}
};
export { RadioGroup };
export default RadioGroup;