'use strict';

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

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

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 }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

/**
 * @deprecated RadioGroup has been deprecated in favor of using RadioButton directly
 */
var RadioGroup = function (_React$Component) {
  _inherits(RadioGroup, _React$Component);

  function RadioGroup() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, RadioGroup);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = RadioGroup.__proto__ || Object.getPrototypeOf(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' : _typeof(options)) === 'object') {
        return Object.keys(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), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(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;