import _extends from "@babel/runtime/helpers/extends";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.
import React from 'react';
import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Tooltip from '../Tooltip';

/**
 * `Checkbox` component is used for turning an option on or off and selecting or deselecting an item.
 * @since 0.0.55
 */
const Checkbox = ({
  baseClassName = `${CLS_PREFIX}checkbox`,
  className,
  children,
  onChange,
  tooltip,
  checked,
  defaultChecked,
  disabled = false,
  name,
  indeterminate = false,
  inputProps,
  rightAddon,
  ...props
}) => {
  const handleChange = event => {
    if (!disabled && onChange) {
      onChange(event.target.checked);
    }
  };

  let result = /*#__PURE__*/React.createElement("div", _extends({
    className: classNames(baseClassName, {
      [`${baseClassName}--disabled`]: disabled,
      [`${baseClassName}--indeterminate`]: indeterminate
    }, className)
  }, props), /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("label", {
    className: `${baseClassName}__label`
  }, /*#__PURE__*/React.createElement("input", _extends({
    className: `${baseClassName}__input`,
    type: "checkbox",
    onChange: handleChange,
    checked: !indeterminate && checked,
    defaultChecked: defaultChecked,
    disabled: disabled,
    name: name
  }, inputProps)), /*#__PURE__*/React.createElement("span", {
    className: `${baseClassName}__indicator`
  }), children ? /*#__PURE__*/React.createElement("span", {
    className: `${baseClassName}__text`
  }, children) : null), rightAddon ? rightAddon : null));

  if (tooltip) {
    result = /*#__PURE__*/React.createElement(Tooltip, {
      title: tooltip
    }, result);
  }

  return result;
};

export default Checkbox;