import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["checked", "color", "children", "className", "description", "disabled", "id", "invalid", "labelSize", "name", "onChange", "required", "style", "value", "aria-labelledby", "aria-describedby"];
// eslint-disable-next-line import/no-duplicates
import * as React from 'react';
import { useRef, useMemo // eslint-disable-next-line import/no-duplicates
} from 'react';
import classNames from 'classnames';
import Text from '../../text/Text';
import generateRandomString from '../../../js/generateRandomString';
import useRadioContext from './useRadioContext';
import { useFirstPaint } from '../../utils/useFirstPaint';

var Radio = function Radio(_ref) {
  var _classNames;

  var checked = _ref.checked,
      _ref$color = _ref.color,
      color = _ref$color === void 0 ? 'dark' : _ref$color,
      children = _ref.children,
      className = _ref.className,
      description = _ref.description,
      disabled = _ref.disabled,
      id = _ref.id,
      invalid = _ref.invalid,
      _ref$labelSize = _ref.labelSize,
      labelSize = _ref$labelSize === void 0 ? 'medium' : _ref$labelSize,
      name = _ref.name,
      onChange = _ref.onChange,
      _ref$required = _ref.required,
      required = _ref$required === void 0 ? false : _ref$required,
      style = _ref.style,
      value = _ref.value,
      ariaLabelledBy = _ref['aria-labelledby'],
      ariaDescribedBy = _ref['aria-describedby'],
      props = _objectWithoutProperties(_ref, _excluded);

  var circleRef = React.useRef();

  var _useRef = useRef(id === undefined || id === '' ? generateRandomString() : id),
      radioId = _useRef.current;

  var radioGroupContext = useRadioContext();
  var isWithinRadioGroup = Boolean(radioGroupContext && Object.keys(radioGroupContext).length);
  var shouldAnimateRef = React.useRef(false);
  var isControlled = checked !== undefined || isWithinRadioGroup;
  var isChecked = undefined;
  useFirstPaint(function () {
    shouldAnimateRef.current = true;
  });

  if (isControlled) {
    // Radio can either be directly set as checked, or be controlled by a RadioGroup
    isChecked = checked !== undefined ? checked : Boolean(radioGroupContext.selectedValue) && radioGroupContext.selectedValue === value;

    if (shouldAnimateRef.current && circleRef.current) {
      circleRef.current.classList.add('sg-radio__circle--with-animation');
    }
  }

  var colorName = radioGroupContext.color || color;
  var isDisabled = disabled !== undefined ? disabled : radioGroupContext.disabled;
  var hasLabel = children !== undefined && children !== null;
  var isInputOnly = !hasLabel && !description;
  var descriptionId = useMemo(function () {
    if (ariaDescribedBy) return ariaDescribedBy;
    if (description) return "".concat(radioId, "-description");
    return null;
  }, [radioId, ariaDescribedBy, description]);
  var radioClass = classNames('sg-radio', className, (_classNames = {}, _defineProperty(_classNames, "sg-radio--".concat(String(colorName)), colorName), _defineProperty(_classNames, 'sg-radio--disabled', isDisabled), _defineProperty(_classNames, 'sg-radio--with-label', !!hasLabel), _defineProperty(_classNames, 'sg-radio--with-description', !!descriptionId), _defineProperty(_classNames, 'sg-radio--with-padding', !isInputOnly), _classNames));
  var labelClass = classNames('sg-radio__label', _defineProperty({
    'sg-radio__label--with-padding-bottom': description
  }, "sg-radio__label--".concat(String(labelSize)), labelSize));
  var circleClass = classNames('sg-radio__circle');
  var labelId = ariaLabelledBy || "".concat(radioId, "-label");
  var isInvalid = invalid !== undefined ? invalid : radioGroupContext.invalid;

  var onInputChange = function onInputChange(e) {
    if (isWithinRadioGroup) {
      radioGroupContext.setLastFocusedValue(value);
      radioGroupContext.setSelectedValue(e, value);
    }

    if (onChange) {
      onChange(e);
    }

    if (circleRef.current && shouldAnimateRef.current) {
      circleRef.current.classList.add('sg-radio__circle--with-animation');
    }
  };

  return /*#__PURE__*/React.createElement("div", {
    className: radioClass,
    style: style
  }, /*#__PURE__*/React.createElement("div", {
    className: "sg-radio__wrapper"
  }, /*#__PURE__*/React.createElement("div", {
    className: "sg-radio__element"
  }, /*#__PURE__*/React.createElement("input", _extends({}, props, {
    className: "sg-radio__input",
    type: "radio",
    id: radioId,
    checked: isChecked,
    disabled: isDisabled,
    name: name || radioGroupContext.name,
    onChange: onInputChange,
    required: required,
    value: value,
    "aria-labelledby": labelId,
    "aria-describedby": descriptionId,
    "aria-invalid": isInvalid ? true : undefined
  })), /*#__PURE__*/React.createElement("span", {
    ref: circleRef,
    className: circleClass // This element is purely decorative so
    // we hide it for screen readers
    ,
    "aria-hidden": "true"
  })), hasLabel && /*#__PURE__*/React.createElement(Text, {
    id: labelId,
    htmlFor: radioId,
    as: "label",
    size: labelSize,
    weight: "bold",
    className: labelClass
  }, children)), description && /*#__PURE__*/React.createElement(Text, {
    id: descriptionId,
    className: "sg-radio__description",
    size: "small",
    as: "span",
    breakWords: true
  }, description));
};

export default Radio;