'use strict';

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.CollapsibleList = void 0;

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Collapse = _interopRequireDefault(require("react-bootstrap/lib/Collapse"));

var _lodash = _interopRequireDefault(require("lodash"));

var CollapsibleList =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(CollapsibleList, _React$Component);

  function CollapsibleList(props, context) {
    var _this;

    _this = _React$Component.call(this, props, context) || this;
    _this.state = {
      isCollapsed: true
    };
    return _this;
  }

  var _proto = CollapsibleList.prototype;

  _proto.handleToggleClick = function handleToggleClick() {
    this.setState({
      isCollapsed: !this.state.isCollapsed
    });
  };

  _proto.render = function render() {
    var head = _lodash.default.take(this.props.children, this.props.visibleItems);

    var tail = _lodash.default.takeRight(this.props.children, this.props.children.length - this.props.visibleItems);

    var showToggle = tail.length > 0;
    var toggle;

    if (showToggle) {
      toggle = _react.default.createElement("span", {
        className: "toggle-list",
        onClick: this.handleToggleClick.bind(this)
      }, "...");
    }

    return _react.default.createElement("div", {
      className: "collapsible-list",
      "data-test-automation-id": this.props.testAutomationId
    }, _react.default.createElement("div", {
      className: "head-wrapper"
    }, head), toggle, _react.default.createElement(_Collapse.default, {
      in: !this.state.isCollapsed
    }, _react.default.createElement("div", null, tail)));
  };

  return CollapsibleList;
}(_react.default.Component);

exports.CollapsibleList = CollapsibleList;
(0, _defineProperty2.default)(CollapsibleList, "propTypes", {
  testAutomationId: _propTypes.default.string,
  visibleItems: _propTypes.default.number,
  children: _propTypes.default.node
});
(0, _defineProperty2.default)(CollapsibleList, "defaultProps", {
  testAutomationId: 'collapsibleList',
  visibleItems: 1,
  children: null
});