'use strict';

exports.__esModule = true;

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _AvInputContainer = require('./AvInputContainer');

var _AvInputContainer2 = _interopRequireDefault(_AvInputContainer);

var _AvFeedback = require('./AvFeedback');

var _AvFeedback2 = _interopRequireDefault(_AvFeedback);

var _reactstrap = require('reactstrap');

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

var htmlValidationAttrs = ['required'];

var noop = function noop() {};

var AvRadioGroup = function (_InputContainer) {
  _inherits(AvRadioGroup, _InputContainer);

  function AvRadioGroup() {
    var _temp, _this, _ret;

    _classCallCheck(this, AvRadioGroup);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, _InputContainer.call.apply(_InputContainer, [this].concat(args))), _this), _this.state = {
      invalidInputs: {},
      dirtyInputs: {},
      touchedInputs: {},
      badInputs: {},
      validate: {},
      value: ''
    }, _this.value = '', _temp), _possibleConstructorReturn(_this, _ret);
  }

  AvRadioGroup.prototype.updateValidations = function updateValidations() {
    var _this2 = this;

    var props = arguments.length <= 0 || arguments[0] === undefined ? this.props : arguments[0];

    this.validations = Object.assign({}, props.validate);

    Object.keys(props).filter(function (val) {
      return htmlValidationAttrs.indexOf(val) > -1;
    }).forEach(function (attr) {
      if (props[attr]) {
        _this2.validations[attr] = _this2.validations[attr] || { value: props[attr] };
      } else {
        delete _this2.validations[attr];
      }
    });

    this.context.FormCtrl.register(this);
    this.validate();
  };

  AvRadioGroup.prototype.componentWillUpdate = function componentWillUpdate(nextProps) {
    if (nextProps !== this.props) {
      this.updateValidations(nextProps);
    }
  };

  AvRadioGroup.prototype.componentWillMount = function componentWillMount() {
    _InputContainer.prototype.componentWillMount.call(this);
    this.value = this.getDefaultValue().value;
    this.setState({ value: this.value });
    this.updateValidations();
  };

  AvRadioGroup.prototype.getValue = function getValue() {
    return this.value;
  };

  AvRadioGroup.prototype.componentWillUnmount = function componentWillUnmount() {
    this.context.FormCtrl.unregister(this);
  };

  AvRadioGroup.prototype.getInputState = function getInputState() {
    return this.context.FormCtrl.getInputState(this.props.name);
  };

  AvRadioGroup.prototype.validate = function validate() {
    this.context.FormCtrl.validate(this.props.name);
  };

  AvRadioGroup.prototype.getDefaultValue = function getDefaultValue() {
    var key = 'defaultValue';

    var value = this.props[key] || this.context.FormCtrl.getDefaultValue(this.props.name) || '';

    return { key: key, value: value };
  };

  AvRadioGroup.prototype.reset = function reset() {
    this.value = this.getDefaultValue().value;
    this.context.FormCtrl.setDirty(this.props.name, false);
    this.context.FormCtrl.setTouched(this.props.name, false);
    this.context.FormCtrl.setBad(this.props.name, false);
    this.setState({ value: this.value });
    this.validate();
    this.props.onReset && this.props.onReset(this.value);
  };

  AvRadioGroup.prototype.getChildContext = function getChildContext() {
    var _this3 = this;

    this.FormCtrl = _extends({}, this.context.FormCtrl);
    this.FormCtrl.register = noop;
    this.FormCtrl.validate = noop;

    var updateGroup = function updateGroup(value) {
      _this3.setState({ value: value });
      _this3.value = value;
      _this3.validate();
    };

    return {
      Group: {
        name: this.props.name,
        update: updateGroup,
        inline: this.props.inline,
        value: this.value,
        getInputState: this.getInputState.bind(this)
      },
      FormCtrl: this.FormCtrl
    };
  };

  AvRadioGroup.prototype.renderErrorMessage = function renderErrorMessage(validation) {
    if (validation.errorMessage) {
      return _react2.default.createElement(
        _AvFeedback2.default,
        null,
        validation.errorMessage
      );
    }

    return null;
  };

  AvRadioGroup.prototype.render = function render() {
    var legend = this.props.label ? _react2.default.createElement(
      'legend',
      null,
      this.props.label
    ) : '';
    var validation = this.getInputState();
    var errorMessage = this.renderErrorMessage(validation);
    var _props = this.props;
    var omit1 = _props.errorMessage;
    var omit2 = _props.validate;
    var omit3 = _props.validationEvent;
    var omit4 = _props.state;
    var omit5 = _props.label;
    var omit6 = _props.required;
    var inline = _props.inline;
    var children = _props.children;

    var attributes = _objectWithoutProperties(_props, ['errorMessage', 'validate', 'validationEvent', 'state', 'label', 'required', 'inline', 'children']);

    var radios = children;

    if (inline) {
      radios = _react2.default.createElement(
        _reactstrap.FormGroup,
        null,
        radios
      );
    }

    return _react2.default.createElement(
      _reactstrap.FormGroup,
      _extends({ tag: 'fieldset' }, attributes, { color: validation.color }),
      legend,
      radios,
      errorMessage
    );
  };

  return AvRadioGroup;
}(_AvInputContainer2.default);

AvRadioGroup.propTypes = Object.assign({}, _reactstrap.FormGroup.propTypes, {
  name: _react.PropTypes.string.isRequired
});
AvRadioGroup.contextTypes = {
  FormCtrl: _react.PropTypes.object.isRequired
};
AvRadioGroup.childContextTypes = {
  Group: _react.PropTypes.object.isRequired,
  FormCtrl: _react.PropTypes.object.isRequired
};
exports.default = AvRadioGroup;