"use strict";

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

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

exports.__esModule = true;
exports.Accordion = 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 _lodash = require("lodash");

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

var _AccordionItem = require("./AccordionItem");

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

  function Accordion() {
    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", {
      activeItem: _this.props.activeItem
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "createClickHandler", function (childOnToggle) {
      return function (activeItem) {
        var onToggle = _this.props.onToggle;

        if ((0, _lodash.isFunction)(onToggle)) {
          onToggle(activeItem);
        } else {
          _this.setState(function (state) {
            return {
              activeItem: state.activeItem !== activeItem ? activeItem : null
            };
          });
        }

        if ((0, _lodash.isFunction)(childOnToggle)) {
          childOnToggle(activeItem);
        }
      };
    });
    return _this;
  }

  var _proto = Accordion.prototype;

  // TODO: redo with getDerivedStateFromProps
  _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var activeItem = this.props.activeItem;

    if (activeItem !== nextProps.activeItem) {
      this.setState({
        activeItem: nextProps.activeItem
      });
    }
  };

  _proto.prepareChildren = function prepareChildren() {
    var _this2 = this;

    var _this$props = this.props,
        children = _this$props.children,
        lazy = _this$props.lazy;
    var activeItem = this.state.activeItem;
    return _react.default.Children.map(children, function (child) {
      if (!child) {
        return child;
      }

      if (child.type !== _AccordionItem.AccordionItem) {
        throw new Error('Accordion only supports children of type AccordionItem');
      }

      var _child$props = child.props,
          name = _child$props.name,
          onToggle = _child$props.onToggle,
          testAutomationId = _child$props.testAutomationId;

      if (!name) {
        throw new Error('AccordionItem in Accordion requires unique `name` prop');
      }

      return _react.default.cloneElement(child, {
        lazy: lazy,
        isExpanded: name === activeItem,
        onToggle: _this2.createClickHandler(onToggle),
        testAutomationId: testAutomationId === _AccordionItem.AccordionItem.defaultProps.testAutomationId ? "" + _AccordionItem.AccordionItem.defaultProps.testAutomationId + name : testAutomationId
      });
    });
  };

  _proto.render = function render() {
    var testAutomationId = this.props.testAutomationId;
    return _react.default.createElement("div", {
      "data-test-automation-id": testAutomationId
    }, this.prepareChildren());
  };

  return Accordion;
}(_react.PureComponent);

exports.Accordion = Accordion;
(0, _defineProperty2.default)(Accordion, "Item", _AccordionItem.AccordionItem);
(0, _defineProperty2.default)(Accordion, "propTypes", {
  activeItem: _propTypes.default.string,
  lazy: _propTypes.default.bool,
  children: _propTypes.default.node.isRequired,
  onToggle: _propTypes.default.func,
  testAutomationId: _propTypes.default.string
});
(0, _defineProperty2.default)(Accordion, "defaultProps", {
  activeItem: null,
  lazy: false,
  onToggle: null,
  testAutomationId: 'accordion'
});