import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["labelText", "inputRef"];

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import PropTypes from 'prop-types';
import React from 'react';
import classNames from 'classnames';
import { settings } from '../../globals/js';
import uid from '../../tools/uniqueId';
var prefix = settings.prefix;
/** Radio buttons represent a group of mutually exclusive choices */

var RadioButton = /*#__PURE__*/function (_React$Component) {
  _inherits(RadioButton, _React$Component);

  var _super = _createSuper(RadioButton);

  function RadioButton() {
    var _this;

    _classCallCheck(this, RadioButton);

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

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "handleChange", function (evt) {
      _this.props.onChange(evt, _this.props.value, _this.props.name);
    });

    return _this;
  }

  _createClass(RadioButton, [{
    key: "UNSAFE_componentWillMount",
    value: function UNSAFE_componentWillMount() {
      this.uid = this.props.id || uid();
    }
  }, {
    key: "render",
    value: function render() {
      var wrapperClasses = classNames('radioButtonWrapper', this.props.className);

      var _this$props = this.props,
          labelText = _this$props.labelText,
          inputRef = _this$props.inputRef,
          other = _objectWithoutProperties(_this$props, _excluded);

      var handleChange = this.props.onChange ? {
        onChange: this.handleChange
      } : {};
      return /*#__PURE__*/React.createElement("div", {
        className: wrapperClasses
      }, /*#__PURE__*/React.createElement("input", _extends({}, other, {
        ref: inputRef,
        type: "radio",
        className: "".concat(prefix, "--radio-button")
      }, handleChange, {
        id: this.uid
      })), /*#__PURE__*/React.createElement("label", {
        htmlFor: this.uid,
        className: "".concat(prefix, "--radio-button__label")
      }, /*#__PURE__*/React.createElement("span", {
        className: "".concat(prefix, "--radio-button__appearance")
      }), labelText));
    }
  }]);

  return RadioButton;
}(React.Component);

_defineProperty(RadioButton, "propTypes", {
  /**
   * Specify whether the <RadioButton> is currently checked
   */
  checked: PropTypes.bool,

  /**
   * Provide an optional className to be applied to the containing node
   */
  className: PropTypes.string,

  /**
   * Specify whether the <RadioButton> should be checked by default
   */
  defaultChecked: PropTypes.bool,

  /**
   * Specify whether the control is disabled
   */
  disabled: PropTypes.bool,

  /**
   * Provide a unique id for the underlying `input` node
   */
  id: PropTypes.string,

  /**
   * Provide label text to be read by screen readers when interacting with the
   * control
   */
  labelText: PropTypes.node.isRequired,

  /**
   * Provide a name for the underlying `input` node
   */
  name: PropTypes.string,

  /**
   * Provide an optional `onChange` hook that is called each time the value of
   * the underlying `input` changes
   */
  onChange: PropTypes.func,

  /**
   * Specify the value of the <RadioButton>
   */
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired
});

export default (function () {
  var forwardRef = function forwardRef(props, ref) {
    return /*#__PURE__*/React.createElement(RadioButton, _extends({}, props, {
      inputRef: ref
    }));
  };

  forwardRef.displayName = 'RadioButton';
  return /*#__PURE__*/React.forwardRef(forwardRef);
})();