'use strict';

exports.__esModule = true;

var _isUndefined2 = require('lodash/isUndefined');

var _isUndefined3 = _interopRequireDefault(_isUndefined2);

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 React = _interopRequireWildcard(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _setDisplayName = require('recompose/setDisplayName');

var _setDisplayName2 = _interopRequireDefault(_setDisplayName);

var _utils = require('./utils');

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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

function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return 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) : _defaults(subClass, superClass); }

var Radio = function (_React$Component) {
  _inherits(Radio, _React$Component);

  function Radio(props) {
    _classCallCheck(this, Radio);

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.handleChange = function (event) {
      var _this$props = _this.props,
          value = _this$props.value,
          disabled = _this$props.disabled,
          onChange = _this$props.onChange;

      var checked = true;
      if (disabled) {
        return;
      }

      _this.setState({ checked: checked }, function () {
        onChange && onChange(value, checked, event);
      });
    };

    _this.state = {
      checked: props.defaultChecked
    };
    return _this;
  }

  Radio.prototype.isChecked = function isChecked() {
    var checked = this.props.checked;

    return (0, _isUndefined3.default)(checked) ? this.state.checked : checked;
  };

  Radio.prototype.updateCheckedState = function updateCheckedState(checked, callback) {
    this.setState({ checked: checked }, callback);
  };

  Radio.prototype.render = function render() {
    var _classNames;

    var _props = this.props,
        inline = _props.inline,
        title = _props.title,
        name = _props.name,
        className = _props.className,
        children = _props.children,
        onChange = _props.onChange,
        disabled = _props.disabled,
        checked = _props.checked,
        defaultChecked = _props.defaultChecked,
        classPrefix = _props.classPrefix,
        tabIndex = _props.tabIndex,
        inputRef = _props.inputRef,
        props = _objectWithoutProperties(_props, ['inline', 'title', 'name', 'className', 'children', 'onChange', 'disabled', 'checked', 'defaultChecked', 'classPrefix', 'tabIndex', 'inputRef']);

    var nextChecked = this.isChecked();
    var addPrefix = (0, _utils.prefix)(classPrefix);
    var classes = (0, _classnames2.default)(classPrefix, className, (_classNames = {}, _classNames[addPrefix('inline')] = inline, _classNames[addPrefix('disabled')] = disabled, _classNames[addPrefix('checked')] = nextChecked, _classNames));

    var unhandled = (0, _utils.getUnhandledProps)(Radio, props);

    var _partitionHTMLProps = (0, _utils.partitionHTMLProps)(unhandled),
        htmlInputProps = _partitionHTMLProps[0],
        rest = _partitionHTMLProps[1];

    var input = React.createElement(
      'span',
      { className: addPrefix('wrapper'), tabIndex: disabled ? -1 : tabIndex },
      React.createElement('input', _extends({}, htmlInputProps, {
        type: 'radio',
        checked: checked,
        defaultChecked: defaultChecked,
        ref: inputRef,
        name: name,
        disabled: disabled,
        onChange: this.handleChange
      })),
      React.createElement('span', { className: addPrefix('inner') })
    );

    return React.createElement(
      'div',
      _extends({}, rest, { className: classes }),
      React.createElement(
        'div',
        { className: addPrefix('checker'), role: 'button' },
        React.createElement(
          'label',
          { title: title },
          input,
          children
        )
      )
    );
  };

  return Radio;
}(React.Component);

Radio.defaultProps = {
  tabIndex: 0
};
Radio.handledProps = ['checked', 'children', 'className', 'classPrefix', 'defaultChecked', 'disabled', 'id', 'inline', 'inputRef', 'name', 'onChange', 'tabIndex', 'title', 'value'];
Radio.propTypes = {
  id: _propTypes2.default.string,
  name: _propTypes2.default.string,
  inline: _propTypes2.default.bool,
  title: _propTypes2.default.string,
  disabled: _propTypes2.default.bool,
  checked: _propTypes2.default.bool,
  defaultChecked: _propTypes2.default.bool,
  inputRef: function inputRef() {
    return (typeof (React.ElementRef == null ? {} : React.ElementRef) === 'function' ? _propTypes2.default.instanceOf(React.ElementRef == null ? {} : React.ElementRef) : _propTypes2.default.any).apply(this, arguments);
  },
  children: _propTypes2.default.node,
  className: _propTypes2.default.string,
  classPrefix: _propTypes2.default.string,
  value: _propTypes2.default.any,
  onChange: _propTypes2.default.func,
  tabIndex: _propTypes2.default.number
};
exports.default = (0, _setDisplayName2.default)('Radio')((0, _utils.defaultProps)({
  classPrefix: 'radio'
})(Radio));