"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.RadioButton = void 0;
var _react = _interopRequireWildcard(require("react"));
var _get = _interopRequireDefault(require("lodash-es/get"));
var _classnames = _interopRequireDefault(require("classnames"));
var _tooltip = require("../tooltip");
var _types = require("./types");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
// eslint-disable-next-line you-dont-need-lodash-underscore/get

/** Allows the user to select one option from a set
 * [CAPITALIZATION] Use sentence case
 * @midnight-gray-supported
 * @alpine-light-supported
 */
const RadioButton = exports.RadioButton = /*#__PURE__*/(0, _react.forwardRef)(({
  option,
  checked,
  size,
  onSelect,
  groupIdentifier = '',
  ariaLabel = '',
  radioButtonRole,
  onKeyDown
}, ref) => {
  const getId = prefix => {
    return [prefix, groupIdentifier, option.id].filter(Boolean).join('-');
  };
  const wrapperRef = (0, _react.useRef)(null);
  const itemId = getId('radiogroup-item');
  const labelId = getId('radiogroup-label');
  const helperId = getId('radiogroup-helper');
  const uniqueLabelId = `radiobutton-label-${option.id}`;
  const uniqueDescriptionId = `radiobutton-description-${option.id}`;
  const ariaAttr = {
    'aria-checked': checked,
    ...(option?.helperText && {
      'aria-describedby': uniqueDescriptionId
    }),
    ...(ariaLabel && {
      'aria-label': ariaLabel
    }),
    ...(!ariaLabel && (option.children || option.label) && {
      'aria-labelledby': uniqueLabelId
    }),
    role: radioButtonRole
  };
  const handleFocus = () => {
    // transition to the focused element when it was hidden by scroll
    wrapperRef?.current?.scrollIntoView?.({
      behavior: 'smooth',
      block: 'nearest'
    });
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "radiobutton-wrapper",
    ref: wrapperRef,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
      ref: ref,
      className: "radiobutton",
      id: itemId,
      "test-id": itemId,
      type: "radio",
      disabled: option.disabled,
      value: option.id,
      checked: checked,
      onChange: e => onSelect?.(e.target.value),
      onFocus: handleFocus,
      name: groupIdentifier,
      onKeyDown: onKeyDown,
      ...ariaAttr
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
      placement: (0, _get.default)(option, 'tooltipParams.placement'),
      content: (0, _get.default)(option, 'tooltipParams.content'),
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("label", {
        "test-id": labelId,
        htmlFor: itemId,
        className: (0, _classnames.default)('radiobutton-label', {
          lg: size === _types.RADIO_BUTTON_SIZE.LG,
          'with-helper-text': option?.helperText
        }),
        id: uniqueLabelId,
        children: option.children || option.label
      })
    }), option?.helperText && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "radiobutton-helper-text",
      "test-id": helperId,
      id: uniqueDescriptionId,
      children: option?.helperText
    })]
  });
});
RadioButton.displayName = 'RadioButton';