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 { isFunction } from 'lodash';
import PropTypes from 'prop-types';
import { AccordionItem } from './AccordionItem';
export var Accordion =
/*#__PURE__*/
function (_PureComponent) {
  _inheritsLoose(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;

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

    _defineProperty(_assertThisInitialized(_this), "createClickHandler", function (childOnToggle) {
      return function (activeItem) {
        var onToggle = _this.props.onToggle;

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

        if (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.Children.map(children, function (child) {
      if (!child) {
        return child;
      }

      if (child.type !== 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.cloneElement(child, {
        lazy: lazy,
        isExpanded: name === activeItem,
        onToggle: _this2.createClickHandler(onToggle),
        testAutomationId: testAutomationId === AccordionItem.defaultProps.testAutomationId ? "" + AccordionItem.defaultProps.testAutomationId + name : testAutomationId
      });
    });
  };

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

  return Accordion;
}(PureComponent);

_defineProperty(Accordion, "Item", AccordionItem);

_defineProperty(Accordion, "propTypes", {
  activeItem: PropTypes.string,
  lazy: PropTypes.bool,
  children: PropTypes.node.isRequired,
  onToggle: PropTypes.func,
  testAutomationId: PropTypes.string
});

_defineProperty(Accordion, "defaultProps", {
  activeItem: null,
  lazy: false,
  onToggle: null,
  testAutomationId: 'accordion'
});