"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

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

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

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

var _Tooltip = _interopRequireDefault(require("../Tooltip"));

// Copyright 1999-2020. 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
 */
class Switch extends _react.Component {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2.default)(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.default.createElement("label", (0, _extends2.default)({
      className: (0, _classnames.default)(baseClassName, {
        ["".concat(baseClassName, "--").concat(intent)]: intent,
        ["".concat(baseClassName, "--loading")]: loading
      }, className)
    }, props), children ? /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(baseClassName, "__label")
    }, children) : null, /*#__PURE__*/_react.default.createElement("input", {
      type: "checkbox",
      onChange: this.handleChange,
      checked: checked,
      defaultChecked: checked === null || checked === undefined ? defaultChecked : undefined,
      disabled: disabled || loading
    }), /*#__PURE__*/_react.default.createElement("span", {
      className: "".concat(baseClassName, "__indicator")
    }));

    if (tooltip) {
      result = /*#__PURE__*/_react.default.createElement(_Tooltip.default, {
        title: tooltip
      }, result);
    }

    return result;
  }

}

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

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

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

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

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

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

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

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

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

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