'use strict';

exports.__esModule = true;

var _react = require('react');

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

function validComponent(input) {
  var type = input && input.props ? input.props.type : undefined;
  var name = input && input.props ? input.props.name : undefined;

  if (!name) {
    throw new Error('Input ' + input + ' has no "name" prop');
  }

  return { type: type, name: name };
}

var InputContainer = function (_Component) {
  _inherits(InputContainer, _Component);

  function InputContainer() {
    _classCallCheck(this, InputContainer);

    return _possibleConstructorReturn(this, _Component.apply(this, arguments));
  }

  InputContainer.prototype.componentWillMount = function componentWillMount() {
    this._inputs = {};
  };

  InputContainer.prototype.registerInput = function registerInput(input) {
    var _validComponent = validComponent(input);

    var type = _validComponent.type;
    var name = _validComponent.name;


    if (type === 'radio') {
      this._inputs[name] = this._inputs[name] || [];
      if (this._inputs[name].indexOf(input) < 0) {
        this._inputs[name].push(input);
      }
    } else {
      this._inputs[name] = input;
    }
  };

  InputContainer.prototype.unregisterInput = function unregisterInput(input) {
    var _validComponent2 = validComponent(input);

    var type = _validComponent2.type;
    var name = _validComponent2.name;


    if (type === 'radio') {
      this._inputs[name] = this._inputs[name].filter(function (ipt) {
        return ipt !== input;
      });
    } else {
      delete this._inputs[name];
    }
  };

  return InputContainer;
}(_react.Component);

exports.default = InputContainer;