import _extends from "@babel/runtime/helpers/extends";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Checkbox from '../Checkbox';
import FormField from '../FormField';

/**
 * `FormFieldCheckbox` component component is used for turning an option on or off
 * and selecting or deselecting an item.
 * @since 0.0.58
 */
const FormFieldCheckbox = ({
  baseClassName,
  className,
  label,
  required,
  checkedValue,
  uncheckedValue,
  autoFocus,
  multi,
  ...props
}) => /*#__PURE__*/React.createElement(FormField, _extends({
  className: classNames(baseClassName, className),
  label: null,
  required: required
}, props), ({
  getId,
  getName,
  getValue,
  setValue,
  getRequiredMark,
  getFullDescription,
  isDisabled
}) => /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement("input", {
  type: "hidden",
  name: getName(),
  value: uncheckedValue
}), /*#__PURE__*/React.createElement(Checkbox, {
  checked: getValue() === checkedValue,
  onChange: checked => setValue(checked ? checkedValue : uncheckedValue),
  inputProps: {
    id: getId(),
    name: getName(),
    value: checkedValue,
    autoFocus
  },
  disabled: isDisabled(),
  rightAddon: /*#__PURE__*/React.createElement(React.Fragment, null, getFullDescription(), required ? getRequiredMark() : null)
}, label)));

FormFieldCheckbox.propTypes = {
  /**
   * Disabled state of field.
   * @since 0.1.0
   */
  disabled: PropTypes.bool,

  /**
   * Custom checked value. Default is boolean true.
   * @since 0.0.59
   */
  checkedValue: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),

  /**
   * Custom unchecked value. Default is boolean false.
   * @since 0.0.59
   */
  uncheckedValue: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),

  /**
   * The browser will automatically focus on the component upon rendering the screen
   * @since 1.9.0
   */
  autoFocus: PropTypes.bool,

  /**
   * @ignore Field cannot be multiple
   */
  multi: PropTypes.bool,

  /**
   * @ignore
   */
  label: PropTypes.any,

  /**
   * @ignore
   */
  required: PropTypes.bool,

  /**
   * @ignore
   */
  className: PropTypes.string,

  /**
   * @ignore
   */
  baseClassName: PropTypes.string,
  fullDescription: PropTypes.string
};
FormFieldCheckbox.defaultProps = {
  disabled: undefined,
  checkedValue: true,
  uncheckedValue: false,
  autoFocus: undefined,
  multi: undefined,
  label: undefined,
  required: false,
  className: undefined,
  baseClassName: `${CLS_PREFIX}form-field-checkbox`,
  fullDescription: undefined
};
export default FormFieldCheckbox;