"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _constants = require("../../constants");

var _Checkbox = _interopRequireDefault(require("../Checkbox"));

var _FormField = _interopRequireDefault(require("../FormField"));

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-2020. Plesk International GmbH. All rights reserved.

/**
 * `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.default.createElement(_FormField.default, (0, _extends2.default)({
  className: (0, _classnames.default)(baseClassName, className),
  label: null,
  required: required
}, props), ({
  getId,
  getName,
  getValue,
  setValue,
  getRequiredMark,
  getFullDescription,
  isDisabled
}) => /*#__PURE__*/_react.default.createElement(_react.Fragment, null, /*#__PURE__*/_react.default.createElement("input", {
  type: "hidden",
  name: getName(),
  value: uncheckedValue
}), /*#__PURE__*/_react.default.createElement(_Checkbox.default, {
  checked: getValue() === checkedValue,
  onChange: checked => setValue(checked ? checkedValue : uncheckedValue),
  inputProps: {
    id: getId(),
    name: getName(),
    value: checkedValue,
    autoFocus
  },
  disabled: isDisabled(),
  rightAddon: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, getFullDescription(), required ? getRequiredMark() : null)
}, label)));

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

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

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

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

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

  /**
   * @ignore
   */
  label: _propTypes.default.any,

  /**
   * @ignore
   */
  required: _propTypes.default.bool,

  /**
   * @ignore
   */
  className: _propTypes.default.string,

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