"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _classnames = _interopRequireDefault(require("classnames"));
var _react = require("react");
var _constants = require("../../constants");
var _FormField = _interopRequireWildcard(require("../FormField"));
var _Hint = _interopRequireDefault(require("../Hint"));
var _Radio = _interopRequireDefault(require("../Radio"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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; }
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

/**
 * `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 = `${_constants.CLS_PREFIX}form-field-radio-buttons`,
    className,
    options,
    autoFocus,
    ...props
  } = _ref;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_FormField.default, {
    className: (0, _classnames.default)(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__*/(0, _jsxRuntime.jsxs)("div", {
          className: `${baseClassName}__radio`,
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Radio.default, {
            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__*/(0, _jsxRuntime.jsxs)("div", {
            className: `${baseClassName}__content`,
            children: [description && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Hint.default, {
              component: "div",
              className: `${_FormField.BASE_FORM_FIELD_CLASS_NAME}__description`,
              children: description
            }), indentFields.map(field => {
              if (isDisabled() || disabled || getValue() !== value) {
                return /*#__PURE__*/(0, _react.cloneElement)(field, {
                  disabled: true
                });
              }
              return field;
            })]
          }) : null]
        }, value);
      });
    }
  });
};
var _default = exports.default = FormFieldRadioButtons;