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

import classNames from 'classnames';
import { CLS_PREFIX } from '../../constants';
import Tooltip from '../Tooltip';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
/**
 * `Switch` component is used for changing the state of an object or system preferences.
 * It provides immediate results that is why it should not be used in Form.
 * @since 0.0.42
 */
const Switch = _ref => {
  let {
    className,
    children,
    onChange,
    tooltip,
    intent,
    checked,
    defaultChecked,
    baseClassName = `${CLS_PREFIX}switch`,
    disabled = false,
    loading = false,
    ...props
  } = _ref;
  const handleChange = event => {
    if (!disabled && onChange) {
      onChange(event.target.checked);
    }
  };
  const result = /*#__PURE__*/_jsxs("label", {
    className: classNames(baseClassName, {
      [`${baseClassName}--${intent}`]: intent,
      [`${baseClassName}--loading`]: loading
    }, className),
    ...props,
    children: [children ? /*#__PURE__*/_jsx("span", {
      className: `${baseClassName}__label`,
      children: children
    }) : null, /*#__PURE__*/_jsx("input", {
      type: "checkbox",
      onChange: handleChange,
      checked: checked,
      defaultChecked: checked === null || checked === undefined ? defaultChecked : undefined,
      disabled: disabled || loading
    }), /*#__PURE__*/_jsx("span", {
      className: `${baseClassName}__indicator`
    })]
  });
  if (tooltip) {
    return /*#__PURE__*/_jsx(Tooltip, {
      title: tooltip,
      children: result
    });
  }
  return result;
};
export default Switch;