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 = ["checked", "disabled", "className", "size", "label", "children", "onMouseEnter", "onMouseLeave"];

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 cx from 'classnames';
import omit from 'lodash.omit';
import PropTypes from 'prop-types';
import React, { createRef, PureComponent } from 'react';
import Box, { omitBoxProps, pickBoxProps } from '../box';
import { TextBodyCompact, TextDisplay, TextSmall } from '../typography';
import theme from './theme.css';
/** @type {React.ComponentType<any>} */

var RadioButton = /*#__PURE__*/function (_PureComponent) {
  _inherits(RadioButton, _PureComponent);

  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), "inputNode", /*#__PURE__*/createRef());

    _defineProperty(_assertThisInitialized(_this), "handleToggle", function (event) {
      var _this$props = _this.props,
          disabled = _this$props.disabled,
          checked = _this$props.checked,
          onChange = _this$props.onChange;

      if (event.pageX !== 0 && event.pageY !== 0) {
        _this.blur();
      }

      if (!disabled && onChange) {
        onChange(!checked, event);
      }
    });

    return _this;
  }

  _createClass(RadioButton, [{
    key: "blur",
    value: function blur() {
      if (this.inputNode.current) {
        this.inputNode.current.blur();
      }
    }
  }, {
    key: "focus",
    value: function focus() {
      if (this.inputNode.current) {
        this.inputNode.current.focus();
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _cx;

      var _this$props2 = this.props,
          checked = _this$props2.checked,
          disabled = _this$props2.disabled,
          className = _this$props2.className,
          size = _this$props2.size,
          label = _this$props2.label,
          children = _this$props2.children,
          onMouseEnter = _this$props2.onMouseEnter,
          onMouseLeave = _this$props2.onMouseLeave,
          others = _objectWithoutProperties(_this$props2, _excluded);

      var TextElement = size === 'small' ? TextSmall : size === 'medium' ? TextBodyCompact : TextDisplay;
      var restProps = omit(others, ['onChange']);
      var boxProps = pickBoxProps(restProps);
      var inputProps = omitBoxProps(restProps);
      var classNames = cx(theme['radiobutton'], theme["is-".concat(size)], (_cx = {}, _defineProperty(_cx, theme['is-checked'], checked), _defineProperty(_cx, theme['is-disabled'], disabled), _cx), className);
      return /*#__PURE__*/React.createElement(Box, _extends({
        element: "label",
        "data-teamleader-ui": "radiobutton",
        className: classNames,
        onMouseEnter: onMouseEnter,
        onMouseLeave: onMouseLeave
      }, boxProps), /*#__PURE__*/React.createElement("input", _extends({
        className: theme['input'],
        type: "radio",
        checked: checked,
        disabled: disabled,
        onClick: this.handleToggle,
        readOnly: true,
        ref: this.inputNode
      }, inputProps)), /*#__PURE__*/React.createElement("span", {
        className: theme['shape']
      }), (label || children) && /*#__PURE__*/React.createElement("span", {
        className: theme['label']
      }, label && /*#__PURE__*/React.createElement(TextElement, {
        element: "span",
        color: disabled ? 'neutral' : 'teal'
      }, label), children));
    }
  }]);

  return RadioButton;
}(PureComponent);

RadioButton.defaultProps = {
  checked: false,
  disabled: false,
  size: 'medium'
};
export default RadioButton;