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

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

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

var _Link = _interopRequireDefault(require("../Link"));

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

/* eslint-disable react/no-did-mount-set-state */

/**
 * `Cuttable` component is used for showing and hiding part of content.
 * @since 0.0.42
 */
class Cuttable extends _react.Component {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2.default)(this, "state", {
      collapsed: undefined,
      autofit: true
    });
    (0, _defineProperty2.default)(this, "contentRef", /*#__PURE__*/(0, _react.createRef)());
    (0, _defineProperty2.default)(this, "handleToggle", () => {
      this.setState(({
        collapsed
      }) => ({
        collapsed: !collapsed,
        autofit: false
      }));
    });
    (0, _defineProperty2.default)(this, "handleResize", () => {
      if (!this.state.autofit) {
        return;
      }

      const content = this.contentRef.current;

      if (content && content.scrollHeight > content.clientHeight) {
        this.setState({
          collapsed: this.props.collapsed
        });
      }
    });
  }

  render() {
    const {
      baseClassName,
      className,
      children,
      collapsed,
      maxHeight,
      locale,
      ...props
    } = this.props;
    const showMoreLessKey = this.state.collapsed ? 'showMore' : 'showLess';
    return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
      className: (0, _classnames.default)(baseClassName, {
        ["".concat(baseClassName, "--collapse")]: this.state.collapsed === true,
        ["".concat(baseClassName, "--expand")]: this.state.collapsed === false
      }, className)
    }, props), /*#__PURE__*/_react.default.createElement(_reactMeasure.default, {
      onResize: this.handleResize,
      innerRef: el => {
        this.contentRef.current = el;
      }
    }, ({
      measureRef
    }) => /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__content"),
      style: {
        maxHeight
      },
      ref: measureRef
    }, children)), this.state.collapsed !== undefined && /*#__PURE__*/_react.default.createElement("div", {
      className: "".concat(baseClassName, "__action")
    }, /*#__PURE__*/_react.default.createElement(_Link.default, {
      onClick: this.handleToggle
    }, /*#__PURE__*/_react.default.createElement(_Translate.default, {
      content: "Cuttable.".concat(showMoreLessKey),
      fallback: locale[showMoreLessKey]
    }))));
  }

}

(0, _defineProperty2.default)(Cuttable, "defaultProps", {
  collapsed: true,
  maxHeight: '7rem',
  locale: {
    showMore: 'Show more',
    showLess: 'Show less'
  },
  baseClassName: "".concat(_constants.CLS_PREFIX, "cuttable")
});
var _default = Cuttable;
exports.default = _default;