"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var React = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _Text = _interopRequireDefault(require("../../text/Text"));

var _generateRandomString = _interopRequireDefault(require("../../../js/generateRandomString"));

var _useRadioContext = _interopRequireDefault(require("./useRadioContext"));

var _useFirstPaint = require("../../utils/useFirstPaint");

var _excluded = ["checked", "color", "children", "className", "description", "disabled", "id", "invalid", "labelSize", "name", "onChange", "required", "style", "value", "aria-labelledby", "aria-describedby"];

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

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 = (0, React.useRef)(id === undefined || id === '' ? (0, _generateRandomString.default)() : id),
      radioId = _useRef.current;

  var radioGroupContext = (0, _useRadioContext.default)();
  var isWithinRadioGroup = Boolean(radioGroupContext && Object.keys(radioGroupContext).length);
  var shouldAnimateRef = React.useRef(false);
  var isControlled = checked !== undefined || isWithinRadioGroup;
  var isChecked = undefined;
  (0, _useFirstPaint.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 = (0, React.useMemo)(function () {
    if (ariaDescribedBy) return ariaDescribedBy;
    if (description) return "".concat(radioId, "-description");
    return null;
  }, [radioId, ariaDescribedBy, description]);
  var radioClass = (0, _classnames.default)('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 = (0, _classnames.default)('sg-radio__label', _defineProperty({
    'sg-radio__label--with-padding-bottom': description
  }, "sg-radio__label--".concat(String(labelSize)), labelSize));
  var circleClass = (0, _classnames.default)('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.default, {
    id: labelId,
    htmlFor: radioId,
    as: "label",
    size: labelSize,
    weight: "bold",
    className: labelClass
  }, children)), description && /*#__PURE__*/React.createElement(_Text.default, {
    id: descriptionId,
    className: "sg-radio__description",
    size: "small",
    as: "span",
    breakWords: true
  }, description));
};

var _default = Radio;
exports.default = _default;