"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 _classnames = _interopRequireDefault(require("classnames"));

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

var _Translate = require("../Translate");

var _Heading = _interopRequireDefault(require("../Heading"));

var _Button = _interopRequireDefault(require("../Button"));

var _utils = require("../utils");

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

/* eslint-disable react/no-deprecated */

/**
 * `Panel` component is used for visually grouping some content.
 * @since 0.0.35
 */
class Panel extends _react.Component {
  constructor(props) {
    super(props);
    (0, _defineProperty2.default)(this, "handleToggle", e => {
      if ((0, _utils.isClickable)(e)) {
        return;
      }

      this.setState(({
        collapsed
      }) => ({
        collapsed: !collapsed
      }), () => {
        if (this.props.onToggle) {
          this.props.onToggle(this.state.collapsed);
        }
      });
    });

    const _collapsed2 = this.props.collapsible && this.props.collapsed || false; // eslint-disable-next-line react/state-in-constructor


    this.state = {
      collapsed: _collapsed2,
      prevPropsCollapsed: _collapsed2
    };
  }

  static getDerivedStateFromProps(props, state) {
    if (props.collapsed !== state.prevPropsCollapsed) {
      return {
        collapsed: props.collapsed,
        prevPropsCollapsed: props.collapsed
      };
    }

    return null;
  }

  render() {
    const {
      baseClassName,
      className,
      title,
      collapsible,
      collapsed: _collapsed,
      onToggle,
      children,
      extra,
      ...props
    } = this.props;
    const {
      collapsed
    } = this.state;
    const handleToggle = collapsible ? this.handleToggle : undefined;
    return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
      className: (0, _classnames.default)(baseClassName, {
        ["".concat(baseClassName, "--collapsible")]: collapsible,
        ["".concat(baseClassName, "--collapsed")]: collapsed,
        ["".concat(baseClassName, "--expanded")]: !collapsed
      }, className)
    }, props), title && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__header"),
      onClick: handleToggle
    }, (0, _Translate.isLikeText)(title) ? /*#__PURE__*/_react.default.createElement(_Heading.default, {
      component: "div",
      level: 4,
      className: "".concat(baseClassName, "__title")
    }, title) : title, (extra || collapsible) && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__extra")
    }, extra, collapsible && /*#__PURE__*/_react.default.createElement(_Button.default, {
      ghost: true,
      className: "".concat(baseClassName, "__control-button"),
      icon: {
        name: 'chevron-up',
        flipVertical: collapsed
      },
      onClick: handleToggle
    }))), /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__content")
    }, children));
  }

}

(0, _defineProperty2.default)(Panel, "defaultProps", {
  collapsible: false,
  collapsed: false,
  baseClassName: "".concat(_constants.CLS_PREFIX, "panel")
});
var _default = Panel;
exports.default = _default;