"use strict";

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 _reactTransitionGroup = require("react-transition-group");

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

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

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

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

// Copyright 1999-2021. 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;
    const rootRef = /*#__PURE__*/(0, _react.createRef)();
    return /*#__PURE__*/_react.default.createElement(_reactTransitionGroup.CSSTransition, {
      classNames: `${baseClassName}-`,
      in: !collapsed,
      timeout: 300,
      nodeRef: rootRef
    }, /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
      className: (0, _classnames.default)(baseClassName, {
        [`${baseClassName}--collapsible`]: collapsible,
        [`${baseClassName}--collapsed`]: collapsed,
        [`${baseClassName}--expanded`]: !collapsed
      }, className)
    }, props, {
      ref: rootRef
    }), title && /*#__PURE__*/_react.default.createElement("div", {
      className: `${baseClassName}__header`,
      onClick: handleToggle
    }, (0, _Translate.isLikeText)(title) ? /*#__PURE__*/_react.default.createElement(_Heading.default, {
      component: "div",
      level: 4,
      className: `${baseClassName}__title`
    }, title) : title, (extra || collapsible) && /*#__PURE__*/_react.default.createElement("div", {
      className: `${baseClassName}__extra`
    }, extra, collapsible && /*#__PURE__*/_react.default.createElement(_Button.default, {
      ghost: true,
      className: `${baseClassName}__control-button`,
      icon: {
        name: 'chevron-up',
        flipVertical: collapsed
      },
      onClick: handleToggle
    }))), /*#__PURE__*/_react.default.createElement("div", {
      className: `${baseClassName}__content`
    }, children)));
  }

}

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