// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

import classNames from 'classnames';
import { cloneElement } from 'react';
import { CLS_PREFIX } from '../../constants';
import FormField, { BASE_FORM_FIELD_CLASS_NAME } from '../FormField';
import Hint from '../Hint';
import Radio from '../Radio';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
/**
 * `FormFieldRadioButtons` is used for making a choice among a set of mutually exclusive, related options. Only one option can be chosen.
 * @since 0.2.0
 */
const FormFieldRadioButtons = _ref => {
  let {
    baseClassName = `${CLS_PREFIX}form-field-radio-buttons`,
    className,
    options,
    autoFocus,
    ...props
  } = _ref;
  return /*#__PURE__*/_jsx(FormField, {
    className: classNames(baseClassName, className),
    ...props,
    children: _ref2 => {
      let {
        getId,
        getName,
        getValue,
        setValue,
        isDisabled
      } = _ref2;
      return options.map((_ref3, index) => {
        let {
          value,
          label,
          description,
          disabled,
          indentFields = []
        } = _ref3;
        return /*#__PURE__*/_jsxs("div", {
          className: `${baseClassName}__radio`,
          children: [/*#__PURE__*/_jsx(Radio, {
            name: getName(),
            checked: getValue() === value,
            onChange: isChecked => {
              if (isChecked) {
                setValue(value);
              }
            },
            disabled: isDisabled() || disabled,
            inputProps: {
              id: getId() ? `${getId()}-${value}` : undefined,
              name: getName(),
              value,
              autoFocus: index === 0 && autoFocus ? true : undefined
            },
            children: label || value
          }), description || indentFields ? /*#__PURE__*/_jsxs("div", {
            className: `${baseClassName}__content`,
            children: [description && /*#__PURE__*/_jsx(Hint, {
              component: "div",
              className: `${BASE_FORM_FIELD_CLASS_NAME}__description`,
              children: description
            }), indentFields.map(field => {
              if (isDisabled() || disabled || getValue() !== value) {
                return /*#__PURE__*/cloneElement(field, {
                  disabled: true
                });
              }
              return field;
            })]
          }) : null]
        }, value);
      });
    }
  });
};
export default FormFieldRadioButtons;