"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 _Switch = _interopRequireDefault(require("../Switch"));

var _Action = _interopRequireDefault(require("../Action"));

var _Popover = _interopRequireDefault(require("../Popover"));

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

/**
 * `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 _react.Component {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2.default)(this, "state", {
      checked: this.props.defaultChecked
    });
    (0, _defineProperty2.default)(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.default.createElement("div", (0, _extends2.default)({
      className: (0, _classnames.default)(baseClassName, {
        ["".concat(baseClassName, "--").concat(intent)]: intent && checked
      }, className)
    }, props), /*#__PURE__*/_react.default.createElement("h5", {
      className: "".concat(baseClassName, "__title")
    }, title), description && /*#__PURE__*/_react.default.createElement("p", {
      className: "".concat(baseClassName, "__description")
    }, description), /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__actions")
    }, /*#__PURE__*/_react.default.createElement(_Switch.default, (0, _extends2.default)({
      className: "".concat(baseClassName, "__switch"),
      intent: intent,
      onChange: this.handleChange,
      defaultChecked: defaultChecked
    }, switchProps)), (fullDescription || children) && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__controls")
    }, children, fullDescription && /*#__PURE__*/_react.default.createElement(_Popover.default, {
      intent: "info",
      target: /*#__PURE__*/_react.default.createElement(_Action.default, {
        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.default.func.isRequired,

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

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

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

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

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

  /**
   * Props of underlying [Switch](#!/Switch) component.
   * @since 0.5.0
   */
  switchProps: _propTypes.default.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.default.node,

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

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