"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _classnames = _interopRequireDefault(require("classnames"));
var _constants = require("../../constants");
var _Tooltip = _interopRequireDefault(require("../Tooltip"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

/**
 * `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 = `${_constants.CLS_PREFIX}switch`,
    disabled = false,
    loading = false,
    ...props
  } = _ref;
  const handleChange = event => {
    if (!disabled && onChange) {
      onChange(event.target.checked);
    }
  };
  const result = /*#__PURE__*/(0, _jsxRuntime.jsxs)("label", {
    className: (0, _classnames.default)(baseClassName, {
      [`${baseClassName}--${intent}`]: intent,
      [`${baseClassName}--loading`]: loading
    }, className),
    ...props,
    children: [children ? /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: `${baseClassName}__label`,
      children: children
    }) : null, /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
      type: "checkbox",
      onChange: handleChange,
      checked: checked,
      defaultChecked: checked === null || checked === undefined ? defaultChecked : undefined,
      disabled: disabled || loading
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: `${baseClassName}__indicator`
    })]
  });
  if (tooltip) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Tooltip.default, {
      title: tooltip,
      children: result
    });
  }
  return result;
};
var _default = exports.default = Switch;