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 Switch from '../Switch';
import Action from '../Action';
import Popover from '../Popover';

/**
 * `SwitchesPanelItem` component is used for combining [Switch](#!/Switch) component with text title and description.
 * Used in [SwitchesPanel](#!/SwitchesPanel).
 * @since 0.3.0
 */
class SwitchesPanelItem extends Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "state", {
      checked: this.props.defaultChecked
    });

    _defineProperty(this, "handleChange", checked => {
      this.setState({
        checked
      });
      this.props.onChange(checked);
    });
  }

  render() {
    const {
      onChange,
      defaultChecked,
      intent,
      title,
      description,
      fullDescription,
      baseClassName,
      className,
      switchProps,
      children,
      ...props
    } = this.props;
    const {
      checked
    } = this.state;
    return /*#__PURE__*/React.createElement("div", _extends({
      className: classNames(baseClassName, {
        ["".concat(baseClassName, "--").concat(intent)]: intent && checked
      }, className)
    }, props), /*#__PURE__*/React.createElement("h5", {
      className: "".concat(baseClassName, "__title")
    }, title), description && /*#__PURE__*/React.createElement("p", {
      className: "".concat(baseClassName, "__description")
    }, description), /*#__PURE__*/React.createElement("div", {
      className: "".concat(baseClassName, "__actions")
    }, /*#__PURE__*/React.createElement(Switch, _extends({
      className: "".concat(baseClassName, "__switch"),
      intent: intent,
      onChange: this.handleChange,
      defaultChecked: defaultChecked
    }, switchProps)), (fullDescription || children) && /*#__PURE__*/React.createElement("div", {
      className: "".concat(baseClassName, "__controls")
    }, children, fullDescription && /*#__PURE__*/React.createElement(Popover, {
      intent: "info",
      target: /*#__PURE__*/React.createElement(Action, {
        icon: "question-mark-circle",
        outerProps: {
          className: "".concat(baseClassName, "__full-description-toggle")
        }
      }),
      placement: "bottom"
    }, fullDescription))));
  }

}

SwitchesPanelItem.propTypes = {
  /**
   * A callback function, can be executed when the checked state is changing.
   * @since 0.3.0
   */
  onChange: PropTypes.func.isRequired,

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

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

  /**
   * Title of the `SwitchesPanelItem`. Must be no longer than two lines.
   * @since 0.3.0
   */
  title: PropTypes.node.isRequired,

  /**
   * `SwitchesPanelItem` description. Must be no longer than three lines.
   * @since 0.3.0
   */
  description: PropTypes.node,

  /**
   * Rich full description of the `SwitchesPanelItem`.
   * Adds control button with question mark that shows `Popover` on click.
   * @since 0.3.0
   */
  fullDescription: PropTypes.node,

  /**
   * Props of underlying [Switch](#!/Switch) component.
   * @since 0.5.0
   */
  switchProps: PropTypes.object,

  /**
   * Content of the control actions.
   * Don't place here more than three buttons (with the one from 'fullDescription' property) or one link and one button.
   * @since 0.3.0
   */
  children: PropTypes.node,

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

  /**
   * @ignore
   */
  baseClassName: PropTypes.string
};
SwitchesPanelItem.defaultProps = {
  defaultChecked: false,
  intent: null,
  description: null,
  fullDescription: null,
  switchProps: {},
  children: null,
  className: null,
  baseClassName: "".concat(CLS_PREFIX, "switches-panel-item")
};
export default SwitchesPanelItem;