import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { isFunction } from 'lodash';
import { Collapse } from 'react-bootstrap';
import { filterKeyCodes } from '../../utils/functions';
import { Block } from '../Block';
import { Icon } from '../Icon';
export var AccordionItem =
/*#__PURE__*/
function (_PureComponent) {
  _inheritsLoose(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;

    _defineProperty(_assertThisInitialized(_this), "state", {
      isShown: _this.props.isExpanded
    });

    _defineProperty(_assertThisInitialized(_this), "handleClick", function () {
      var _this$props = _this.props,
          onToggle = _this$props.onToggle,
          name = _this$props.name;

      if (isFunction(onToggle)) {
        onToggle(name);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleKeyDown", 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 = classnames('rc-accordion-item', {
      'rc-accordion-item-expanded': isExpanded
    });
    return React.createElement("div", {
      className: className,
      "data-test-automation-id": testAutomationId
    }, React.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.createElement(Block, {
      addHorizontalOffset: true,
      cancelBottomOffset: true
    }, React.createElement(Block, {
      inline: true,
      className: "pull-left"
    }, React.createElement(Icon, {
      name: arrow
    })), React.createElement("h3", {
      role: "presentation",
      "data-test-automation-id": "assignToggleText"
    }, title))), React.createElement(Collapse, {
      in: isExpanded,
      "data-test-automation-id": "accordionItemContent"
    }, React.createElement("div", null, this.renderChildren())));
  };

  return AccordionItem;
}(PureComponent);

_defineProperty(AccordionItem, "propTypes", {
  title: PropTypes.node.isRequired,
  name: PropTypes.string,
  isExpanded: PropTypes.bool,
  onToggle: PropTypes.func,
  children: PropTypes.node,
  lazy: PropTypes.bool,
  testAutomationId: PropTypes.string
});

_defineProperty(AccordionItem, "defaultProps", {
  name: null,
  onToggle: null,
  isExpanded: false,
  children: null,
  lazy: false,
  testAutomationId: 'accordionItem'
});