"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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 _lodash = require("lodash");

var _reactBootstrap = require("react-bootstrap");

var _functions = require("../../utils/functions");

var _Block = require("../Block");

var _Icon = require("../Icon");

var AccordionItem =
/*#__PURE__*/
function (_PureComponent) {
  (0, _inheritsLoose2.default)(AccordionItem, _PureComponent);

  function AccordionItem() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _PureComponent.call.apply(_PureComponent, [this].concat(args)) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", {
      isShown: _this.props.isExpanded
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleClick", function () {
      var _this$props = _this.props,
          onToggle = _this$props.onToggle,
          name = _this$props.name;

      if ((0, _lodash.isFunction)(onToggle)) {
        onToggle(name);
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleKeyDown", (0, _functions.filterKeyCodes)(_this.handleClick));
    return _this;
  }

  AccordionItem.getDerivedStateFromProps = function getDerivedStateFromProps(props, state) {
    var isExpanded = props.isExpanded;
    var isShown = state.isShown;

    if (!isShown && isExpanded) {
      return {
        isShown: true
      };
    }

    return state;
  };

  var _proto = AccordionItem.prototype;

  _proto.renderChildren = function renderChildren() {
    var _this$props2 = this.props,
        lazy = _this$props2.lazy,
        children = _this$props2.children;
    var isShown = this.state.isShown;

    if (!isShown && lazy) {
      return null;
    }

    return children;
  };

  _proto.render = function render() {
    var _this$props3 = this.props,
        title = _this$props3.title,
        isExpanded = _this$props3.isExpanded,
        testAutomationId = _this$props3.testAutomationId;
    var arrow = isExpanded ? 'chevron-up-big' : 'chevron-down-big';
    var className = (0, _classnames.default)('rc-accordion-item', {
      'rc-accordion-item-expanded': isExpanded
    });
    return _react.default.createElement("div", {
      className: className,
      "data-test-automation-id": testAutomationId
    }, _react.default.createElement("div", {
      className: "rc-accordion-item-header",
      onClick: this.handleClick,
      onKeyDown: this.handleKeyDown,
      tabIndex: "0",
      role: "button",
      "aria-expanded": isExpanded,
      "aria-label": title,
      "data-test-automation-id": "accordionItemHead"
    }, _react.default.createElement(_Block.Block, {
      addHorizontalOffset: true,
      cancelBottomOffset: true
    }, _react.default.createElement(_Block.Block, {
      inline: true,
      className: "pull-left"
    }, _react.default.createElement(_Icon.Icon, {
      name: arrow
    })), _react.default.createElement("h3", {
      role: "presentation",
      "data-test-automation-id": "assignToggleText"
    }, title))), _react.default.createElement(_reactBootstrap.Collapse, {
      in: isExpanded,
      "data-test-automation-id": "accordionItemContent"
    }, _react.default.createElement("div", null, this.renderChildren())));
  };

  return AccordionItem;
}(_react.PureComponent);

exports.AccordionItem = AccordionItem;
(0, _defineProperty2.default)(AccordionItem, "propTypes", {
  title: _propTypes.default.node.isRequired,
  name: _propTypes.default.string,
  isExpanded: _propTypes.default.bool,
  onToggle: _propTypes.default.func,
  children: _propTypes.default.node,
  lazy: _propTypes.default.bool,
  testAutomationId: _propTypes.default.string
});
(0, _defineProperty2.default)(AccordionItem, "defaultProps", {
  name: null,
  onToggle: null,
  isExpanded: false,
  children: null,
  lazy: false,
  testAutomationId: 'accordionItem'
});