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

import { Fragment } from 'react';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Checkbox from '../Checkbox';
import FormField from '../FormField';
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
/**
 * `FormFieldCheckbox` component is used for turning an option on or off
 * and selecting or deselecting an item.
 * @since 0.0.58
 */
const FormFieldCheckbox = _ref => {
  let {
    className,
    label,
    autoFocus,
    required = false,
    checkedValue = true,
    uncheckedValue = false,
    baseClassName = `${CLS_PREFIX}form-field-checkbox`,
    ...props
  } = _ref;
  return /*#__PURE__*/_jsx(FormField, {
    className: classNames(baseClassName, className),
    label: null,
    required: required,
    ...props,
    children: _ref2 => {
      let {
        getId,
        getName,
        getValue,
        setValue,
        getRequiredMark,
        getFullDescription,
        isDisabled
      } = _ref2;
      return /*#__PURE__*/_jsxs(Fragment, {
        children: [/*#__PURE__*/_jsx("input", {
          type: "hidden",
          name: getName(),
          value: uncheckedValue.toString()
        }), /*#__PURE__*/_jsx(Checkbox, {
          checked: getValue() === checkedValue,
          onChange: checked => setValue(checked ? checkedValue : uncheckedValue),
          inputProps: {
            id: getId(),
            name: getName(),
            value: checkedValue.toString(),
            autoFocus
          },
          disabled: isDisabled(),
          rightAddon: /*#__PURE__*/_jsxs(_Fragment, {
            children: [getFullDescription(), required ? getRequiredMark() : null]
          }),
          children: label
        })]
      });
    }
  });
};
export default FormFieldCheckbox;