'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _AccordionGroupContainer = require('./styles/AccordionGroupContainer');

var _AccordionGroupContainer2 = _interopRequireDefault(_AccordionGroupContainer);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * @visibleName Accordion.Group
 */
var AccordionGroup = function (_React$Component) {
  (0, _inherits3.default)(AccordionGroup, _React$Component);

  function AccordionGroup(props) {
    (0, _classCallCheck3.default)(this, AccordionGroup);

    var _this = (0, _possibleConstructorReturn3.default)(this, (AccordionGroup.__proto__ || (0, _getPrototypeOf2.default)(AccordionGroup)).call(this, props));

    _this.createToggleHandler = function (key) {
      return function (isAlreadyExpanded) {
        // current state is open, it will close
        if (!isAlreadyExpanded) {
          _this.setState({ expandedKey: key });
          // close already open accordion
        } else {
          _this.setState({ expandedKey: null });
        }
      };
    };

    _this.renderAccordion = function (accordion) {
      var expandedKey = _this.state.expandedKey;

      if (!accordion.key) {
        throw new Error('All AccordionGroup accordion children must have a key');
      }
      return _react2.default.cloneElement(accordion, {
        isExpanded: expandedKey === accordion.key,
        onToggle: _this.createToggleHandler(accordion.key)
      });
    };

    _this.renderAccordions = function () {
      var children = _this.props.children;

      return _react2.default.Children.map(children, _this.renderAccordion);
    };

    _this.state = {
      expandedKey: '' + props.startExpandedKey
    };
    return _this;
  }

  (0, _createClass3.default)(AccordionGroup, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          className = _props.className,
          id = _props.id,
          Container = _props.Container;


      return _react2.default.createElement(
        Container,
        { className: className, id: id },
        this.renderAccordions()
      );
    }
  }]);
  return AccordionGroup;
}(_react2.default.Component);

AccordionGroup.propTypes = {
  /**
   * Accordions to render in this group.
   */
  children: _propTypes2.default.node.isRequired,
  /**
   * The key of the accordion (if any) to start in the expanded state.
   */
  startExpandedKey: _propTypes2.default.any,
  /**
   * A class name to pass to the accordion group container.
   */
  className: _propTypes2.default.string,
  /**
   * An id to pass to the accordion group container
   */
  id: _propTypes2.default.string,
  /**
   * A component that wraps the accordions and defines layout and styling
   */
  Container: _propTypes2.default.func
};
AccordionGroup.defaultProps = {
  startExpandedKey: null,
  className: null,
  id: null,
  Container: _AccordionGroupContainer2.default
};
exports.default = AccordionGroup;