'use strict';

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

var _keys = require('babel-runtime/core-js/object/keys');

var _keys2 = _interopRequireDefault(_keys);

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

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 _recompose = require('recompose');

var _RadioButton = require('./RadioButton');

var _RadioButton2 = _interopRequireDefault(_RadioButton);

var _RadioGroupContainer = require('./styles/RadioGroupContainer');

var _RadioGroupContainer2 = _interopRequireDefault(_RadioGroupContainer);

var _generateId = require('../../extensions/generateId');

var _generateId2 = _interopRequireDefault(_generateId);

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

var RadioGroup = function (_React$Component) {
  (0, _inherits3.default)(RadioGroup, _React$Component);

  function RadioGroup() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = RadioGroup.__proto__ || (0, _getPrototypeOf2.default)(RadioGroup)).call.apply(_ref, [this].concat(args))), _this), _this.getOptions = function () {
      return _this.props.options || _this.props.choices;
    }, _this.getName = function () {
      return _this.props.name || (0, _generateId2.default)('radioGroup');
    }, _this.optionsToButtons = function () {
      var _this$props = _this.props,
          value = _this$props.value,
          _onChange = _this$props.onChange,
          disabled = _this$props.disabled,
          required = _this$props.required,
          Button = _this$props.Button;

      var options = _this.getOptions();
      var name = _this.getName();
      if (options instanceof Array) {
        return options.map(function (choice) {
          return _react2.default.createElement(Button, {
            checked: choice === value,
            name: name,
            label: choice,
            value: choice,
            key: choice,
            disabled: disabled,
            required: required,
            onChange: function onChange() {
              return _onChange(choice);
            }
          });
        });
      } else if ((typeof options === 'undefined' ? 'undefined' : (0, _typeof3.default)(options)) === 'object') {
        return (0, _keys2.default)(options).map(function (key) {
          var choice = options[key];

          return _react2.default.createElement(Button, {
            checked: key === value,
            name: name,
            value: key,
            label: key,
            children: choice,
            key: key,
            disabled: disabled,
            required: required,
            onChange: function onChange() {
              return _onChange(key);
            }
          });
        });
      } else {
        return 'invalid options';
      }
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(RadioGroup, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          className = _props.className,
          id = _props.id,
          Container = _props.Container;

      return _react2.default.createElement(
        Container,
        { className: className, id: id },
        this.optionsToButtons()
      );
    }
  }]);
  return RadioGroup;
}(_react2.default.Component);

RadioGroup.propTypes = {
  /**
   * An array of choice objects or strings to create buttons from.
   */
  options: _propTypes2.default.oneOfType([_propTypes2.default.arrayOf(_propTypes2.default.string), _propTypes2.default.objectOf(_propTypes2.default.node)]).isRequired,
  /**
   * A field name for this input.
   */
  name: _propTypes2.default.string,
  /**
   * The currently selected value.
   */
  value: _propTypes2.default.string.isRequired,
  /**
   * Called when the input value changes.
   */
  onChange: _propTypes2.default.func.isRequired,
  /**
   * Indicates whether the user can change the input.
   */
  disabled: _propTypes2.default.bool,
  /**
   * Indicates whether the value is required for form input.
   */
  required: _propTypes2.default.bool,
  /**
   * Adds a class name to the group container element.
   */
  className: _propTypes2.default.string,
  /**
   * Adds an id to the group container element.
   */
  id: _propTypes2.default.string,
  /**
   * A component for rendering a container around the buttons
   */
  Container: _propTypes2.default.func,
  /**
   * A component for rendering a button, defaults RadioGroup.Button
   */
  Button: _propTypes2.default.func
};
RadioGroup.defaultProps = {
  disabled: false,
  required: false,
  className: null,
  id: null,
  choices: null,
  Container: _RadioGroupContainer2.default,
  Button: _RadioButton2.default,
  name: undefined
};


RadioGroup.Vertical = (0, _recompose.withProps)({
  Container: _RadioGroupContainer2.default.Vertical
})(RadioGroup);
RadioGroup.Small = (0, _recompose.withProps)({
  Button: _RadioButton2.default.Small
})(RadioGroup);
RadioGroup.Large = (0, _recompose.withProps)({
  Button: _RadioButton2.default.Large
})(RadioGroup);

RadioGroup.Vertical.Small = RadioGroup.Small.Vertical = (0, _recompose.withProps)({
  Container: _RadioGroupContainer2.default.Vertical,
  Button: _RadioButton2.default.Small
})(RadioGroup);
RadioGroup.Vertical.Large = RadioGroup.Large.Vertical = (0, _recompose.withProps)({
  Container: _RadioGroupContainer2.default.Vertical,
  Button: _RadioButton2.default.Large
})(RadioGroup);

RadioGroup.Button = _RadioButton2.default;

exports.default = RadioGroup;