import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Copyright 1999-2020. Plesk International GmbH. All rights reserved.

/* eslint-disable react/no-deprecated */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Measure from 'react-measure';
import { CLS_PREFIX } from '../../constants';
import { isClickable } from '../utils';
import InPlaceEdit from '../InPlaceEdit';
import Button from '../Button';
import Vertical, { VERTICAL_BREAKPOINT } from '../VerticalContext';

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 Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "state", {
      vertical: false
    });

    _defineProperty(this, "handleResize", ({
      bounds
    }) => {
      const vertical = getVertical(this.props, this.context);

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

      this.setState({
        vertical: bounds.width < VERTICAL_BREAKPOINT
      });
    });

    _defineProperty(this, "handleCollapse", e => {
      if (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.createElement(Button, {
      ghost: true,
      className: classNames("".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.createElement("div", {
      className: "".concat(baseClassName, "__header-buttons")
    }, buttons, onClose && /*#__PURE__*/React.createElement(Button, {
      ghost: true,
      className: classNames("".concat(baseClassName, "__control-button"), "".concat(baseClassName, "__control-button--close")),
      onClick: onClose,
      icon: "cross-mark"
    }));
  }

  renderTitle({
    baseClassName,
    title,
    onTitleChange
  }) {
    return title && /*#__PURE__*/React.createElement(InPlaceEdit, {
      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.createElement(Vertical.Provider, {
      value: vertical
    }, /*#__PURE__*/React.createElement(Measure, {
      onResize: this.handleResize,
      bounds: true
    }, ({
      measureRef
    }) => /*#__PURE__*/React.createElement("div", _extends({
      ref: measureRef,
      className: classNames(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.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.node,

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

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

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

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

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

  /**
   * Array of custom section action button at the right.
   * @since 0.0.54
   */
  buttons: PropTypes.arrayOf(PropTypes.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.bool,

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

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

  /**
   * @ignore
   */
  baseClassName: PropTypes.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(CLS_PREFIX, "section")
};
Section.contextType = Vertical;
export default Section;