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

/**
 * `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
 */
class Switch extends Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "handleChange", event => {
      const {
        disabled,
        onChange
      } = this.props;

      if (!disabled && onChange) {
        onChange(event.target.checked);
      }
    });
  }

  render() {
    const {
      baseClassName,
      className,
      children,
      onChange,
      tooltip,
      intent,
      checked,
      defaultChecked,
      disabled,
      loading,
      ...props
    } = this.props;
    let result = /*#__PURE__*/React.createElement("label", _extends({
      className: classNames(baseClassName, {
        ["".concat(baseClassName, "--").concat(intent)]: intent,
        ["".concat(baseClassName, "--loading")]: loading
      }, className)
    }, props), children ? /*#__PURE__*/React.createElement("span", {
      className: "".concat(baseClassName, "__label")
    }, children) : null, /*#__PURE__*/React.createElement("input", {
      type: "checkbox",
      onChange: this.handleChange,
      checked: checked,
      defaultChecked: checked === null || checked === undefined ? defaultChecked : undefined,
      disabled: disabled || loading
    }), /*#__PURE__*/React.createElement("span", {
      className: "".concat(baseClassName, "__indicator")
    }));

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

    return result;
  }

}

Switch.propTypes = {
  /**
   * Determine whether the `Switch` is checked.
   * @since 0.0.42
   */
  checked: PropTypes.bool,

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

  /**
   * A callback function, can be executed when the checked state is changing.
   * @since 0.0.42
   */
  onChange: PropTypes.func,

  /**
   * Tooltip for component
   * @since 0.0.42
   */
  tooltip: PropTypes.any,

  /**
   * Visual intent color to apply to component.
   * @since 0.3.0
   */
  intent: PropTypes.oneOf(['warning', 'danger']),

  /**
   * Is `Switch` disabled?
   * @since 0.0.42
   */
  disabled: PropTypes.bool,

  /**
   * Loading indication
   * @since 0.3.0
   */
  loading: PropTypes.bool,

  /**
   * Label of the `Switch`
   * @since 0.0.42
   */
  children: PropTypes.node,

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

  /**
   * @ignore
   */
  baseClassName: PropTypes.string
};
Switch.defaultProps = {
  checked: undefined,
  defaultChecked: undefined,
  onChange: undefined,
  tooltip: undefined,
  intent: undefined,
  disabled: false,
  loading: false,
  children: undefined,
  className: undefined,
  baseClassName: "".concat(CLS_PREFIX, "switch")
};
export default Switch;