"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 _reactMeasure = _interopRequireDefault(require("react-measure"));

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

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

var _InPlaceEdit = _interopRequireDefault(require("../InPlaceEdit"));

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

var _VerticalContext = _interopRequireWildcard(require("../VerticalContext"));

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

/* eslint-disable react/no-deprecated */
const getVertical = (props, verticalContext) => {
  if (props.vertical !== null && props.vertical !== undefined) {
    return props.vertical;
  }

  if (verticalContext !== undefined) {
    return verticalContext;
  }

  return undefined;
};
/**
 * `Section` component is used for dividing of complex content into groups for better readability
 * and comprehension of the presented information.
 * @since 0.0.54
 */


class Section extends _react.Component {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2.default)(this, "state", {
      vertical: false
    });
    (0, _defineProperty2.default)(this, "handleResize", ({
      bounds
    }) => {
      const vertical = getVertical(this.props, this.context);

      if (vertical !== undefined) {
        return;
      }

      this.setState({
        vertical: bounds.width < _VerticalContext.VERTICAL_BREAKPOINT
      });
    });
    (0, _defineProperty2.default)(this, "handleCollapse", e => {
      if ((0, _utils.isClickable)(e)) {
        return;
      }

      this.setState(({
        collapsed
      }) => ({
        collapsed: !collapsed
      }));
    });
  }

  static getDerivedStateFromProps(props, state) {
    const {
      collapsed
    } = props;
    const {
      prevCollapsed
    } = state;
    const nextState = {};

    if (collapsed !== prevCollapsed) {
      nextState.collapsed = collapsed;
    }

    nextState.prevCollapsed = collapsed;
    return nextState;
  }

  renderCollapseButton({
    baseClassName,
    collapsible
  }) {
    const {
      collapsed
    } = this.state;
    return collapsible && /*#__PURE__*/_react.default.createElement(_Button.default, {
      ghost: true,
      className: (0, _classnames.default)("".concat(baseClassName, "__control-button"), "".concat(baseClassName, "__control-button--collapse")),
      icon: {
        name: 'chevron-up',
        flipVertical: collapsed
      },
      onClick: this.handleCollapse
    });
  }

  renderCloseButton({
    baseClassName,
    onClose,
    buttons
  }) {
    return (onClose || buttons) && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__header-buttons")
    }, buttons, onClose && /*#__PURE__*/_react.default.createElement(_Button.default, {
      ghost: true,
      className: (0, _classnames.default)("".concat(baseClassName, "__control-button"), "".concat(baseClassName, "__control-button--close")),
      onClick: onClose,
      icon: "cross-mark"
    }));
  }

  renderTitle({
    baseClassName,
    title,
    onTitleChange
  }) {
    return title && /*#__PURE__*/_react.default.createElement(_InPlaceEdit.default, {
      className: "".concat(baseClassName, "__title"),
      value: title,
      onChange: onTitleChange,
      onClick: e => {
        var _e$target$closest$que;

        (_e$target$closest$que = e.target.closest(".".concat(baseClassName, "__header")).querySelector(".".concat(baseClassName, "__control-button"))) === null || _e$target$closest$que === void 0 ? void 0 : _e$target$closest$que.click();
      }
    });
  }

  renderContent({
    collapsible,
    children
  }) {
    const {
      collapsed
    } = this.state;

    if (collapsible && collapsed) {
      return null;
    }

    return children;
  }

  render() {
    const {
      baseClassName,
      className,
      intent,
      collapsible,
      title,
      collapsed,
      onTitleChange,
      onClose,
      buttons,
      vertical: verticalProp,
      children,
      ...props
    } = this.props;
    let vertical = getVertical(this.props, this.context);
    vertical = vertical === undefined ? this.state.vertical : vertical;
    return /*#__PURE__*/_react.default.createElement(_VerticalContext.default.Provider, {
      value: vertical
    }, /*#__PURE__*/_react.default.createElement(_reactMeasure.default, {
      onResize: this.handleResize,
      bounds: true
    }, ({
      measureRef
    }) => /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
      ref: measureRef,
      className: (0, _classnames.default)(baseClassName, {
        ["".concat(baseClassName, "--").concat(intent)]: intent,
        ["".concat(baseClassName, "--collapsible")]: collapsible,
        ["".concat(baseClassName, "--collapsed")]: this.state.collapsed,
        ["".concat(baseClassName, "--vertical")]: vertical
      }, className)
    }, props), title && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__header")
    }, this.renderCollapseButton({
      baseClassName,
      collapsible
    }), this.renderTitle({
      baseClassName,
      title,
      onTitleChange
    }), this.renderCloseButton({
      baseClassName,
      onClose,
      buttons
    })), this.renderContent({
      collapsible,
      children
    }))));
  }

}

Section.propTypes = {
  /**
   * Title of the section.
   * @since 0.0.54
   */
  title: _propTypes.default.node,

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

  /**
   * Is section collapsible.
   * @since 0.0.54
   */
  collapsible: _propTypes.default.bool,

  /**
   * Is section collapsed now.
   * @since 0.0.54
   */
  collapsed: _propTypes.default.bool,

  /**
   * Event handler of title change with inline editing form.
   * @since 0.0.54
   */
  onTitleChange: _propTypes.default.func,

  /**
   * Event handler of closing section with X-sign at the right.
   * @since 0.0.54
   */
  onClose: _propTypes.default.func,

  /**
   * Array of custom section action button at the right.
   * @since 0.0.54
   */
  buttons: _propTypes.default.arrayOf(_propTypes.default.element),

  /**
   * Vertical or horizontal section view.
   * Section view will be automatically switched if this parameter is not specified explicitly.
   * @since 0.3.0
   */
  vertical: _propTypes.default.bool,

  /**
   * Content of the `Section`.
   * @since 0.0.54
   */
  children: _propTypes.default.node,

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

  /**
   * @ignore
   */
  baseClassName: _propTypes.default.string
};
Section.defaultProps = {
  title: null,
  intent: null,
  collapsible: false,
  collapsed: false,
  onTitleChange: null,
  onClose: null,
  buttons: null,
  vertical: null,
  children: null,
  className: null,
  baseClassName: "".concat(_constants.CLS_PREFIX, "section")
};
Section.contextType = _VerticalContext.default;
var _default = Section;
exports.default = _default;