text: 'use strict';
  
  var _extends = require('babel-runtime/helpers/extends')['default'];
  
  var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default')['default'];
  
  exports.__esModule = true;
  
  var _react = require('react');
  
  var _react2 = _interopRequireDefault(_react);
  
  var _classnames = require('classnames');
  
  var _classnames2 = _interopRequireDefault(_classnames);
  
  var _BootstrapMixin = require('./BootstrapMixin');
  
  var _BootstrapMixin2 = _interopRequireDefault(_BootstrapMixin);
  
  var _Collapse = require('./Collapse');
  
  var _Collapse2 = _interopRequireDefault(_Collapse);
  
  var Panel = _react2['default'].createClass({
    displayName: 'Panel',
  
    mixins: [_BootstrapMixin2['default']],
  
    propTypes: {
      collapsible: _react2['default'].PropTypes.bool,
      onSelect: _react2['default'].PropTypes.func,
      header: _react2['default'].PropTypes.node,
      id: _react2['default'].PropTypes.string,
      footer: _react2['default'].PropTypes.node,
      defaultExpanded: _react2['default'].PropTypes.bool,
      expanded: _react2['default'].PropTypes.bool,
      eventKey: _react2['default'].PropTypes.any
    },
  
    getDefaultProps: function getDefaultProps() {
      return {
        bsClass: 'panel',
        bsStyle: 'default',
        defaultExpanded: false
      };
    },
  
    getInitialState: function getInitialState() {
      return {
        expanded: this.props.defaultExpanded
      };
    },
  
    handleSelect: function handleSelect(e) {
      e.selected = true;
  
      if (this.props.onSelect) {
        this.props.onSelect(e, this.props.eventKey);
      } else {
        e.preventDefault();
      }
  
      if (e.selected) {
        this.handleToggle();
      }
    },
  
    handleToggle: function handleToggle() {
      this.setState({ expanded: !this.state.expanded });
    },
  
    isExpanded: function isExpanded() {
      return this.props.expanded != null ? this.props.expanded : this.state.expanded;
    },
  
    render: function render() {
      return _react2['default'].createElement(
        'div',
        _extends({}, this.props, {
          className: _classnames2['default'](this.props.className, this.getBsClassSet()),
          id: this.props.collapsible ? null : this.props.id, onSelect: null }),
        this.renderHeading(),
        this.props.collapsible ? this.renderCollapsibleBody() : this.renderBody(),
        this.renderFooter()
      );
    },
  
    renderCollapsibleBody: function renderCollapsibleBody() {
      var collapseClass = this.prefixClass('collapse');
  
      return _react2['default'].createElement(
        _Collapse2['default'],
        { 'in': this.isExpanded() },
        _react2['default'].createElement(
          'div',
          {
            className: collapseClass,
            id: this.props.id,
            ref: 'panel' },
          this.renderBody()
        )
      );
    },
  
    renderBody: function renderBody() {
      var allChildren = this.props.children;
      var bodyElements = [];
      var panelBodyChildren = [];
      var bodyClass = this.prefixClass('body');
  
      function getProps() {
        return { key: bodyElements.length };
      }
  
      function addPanelChild(child) {
        bodyElements.push(_react.cloneElement(child, getProps()));
      }
  
      function addPanelBody(children) {
        bodyElements.push(_react2['default'].createElement(
          'div',
          _extends({ className: bodyClass }, getProps()),
          children
        ));
      }
  
      function maybeRenderPanelBody() {
        if (panelBodyChildren.length === 0) {
          return;
        }
  
        addPanelBody(panelBodyChildren);
        panelBodyChildren = [];
      }
  
      // Handle edge cases where we should not iterate through children.
      if (!Array.isArray(allChildren) || allChildren.length === 0) {
        if (this.shouldRenderFill(allChildren)) {
          addPanelChild(allChildren);
        } else {
          addPanelBody(allChildren);
        }
      } else {
  
        allChildren.forEach((function (child) {
          if (this.shouldRenderFill(child)) {
            maybeRenderPanelBody();
  
            // Separately add the filled element.
            addPanelChild(child);
          } else {
            panelBodyChildren.push(child);
          }
        }).bind(this));
  
        maybeRenderPanelBody();
      }
  
      return bodyElements;
    },
  
    shouldRenderFill: function shouldRenderFill(child) {
      return _react2['default'].isValidElement(child) && child.props.fill != null;
    },
  
    renderHeading: function renderHeading() {
      var header = this.props.header;
  
      if (!header) {
        return null;
      }
  
      if (!_react2['default'].isValidElement(header) || Array.isArray(header)) {
        header = this.props.collapsible ? this.renderCollapsibleTitle(header) : header;
      } else {
        var className = _classnames2['default'](this.prefixClass('title'), header.props.className);
  
        if (this.props.collapsible) {
          header = _react.cloneElement(header, {
            className: className,
            children: this.renderAnchor(header.props.children)
          });
        } else {
          header = _react.cloneElement(header, { className: className });
        }
      }
  
      return _react2['default'].createElement(
        'div',
        { className: this.prefixClass('heading') },
        header
      );
    },
  
    renderAnchor: function renderAnchor(header) {
      return _react2['default'].createElement(
        'a',
        {
          href: '#' + (this.props.id || ''),
          'aria-controls': this.props.collapsible ? this.props.id : null,
          className: this.isExpanded() ? null : 'collapsed',
          'aria-expanded': this.isExpanded(),
          onClick: this.handleSelect },
        header
      );
    },
  
    renderCollapsibleTitle: function renderCollapsibleTitle(header) {
      return _react2['default'].createElement(
        'h4',
        { className: this.prefixClass('title') },
        this.renderAnchor(header)
      );
    },
  
    renderFooter: function renderFooter() {
      if (!this.props.footer) {
        return null;
      }
  
      return _react2['default'].createElement(
        'div',
        { className: this.prefixClass('footer') },
        this.props.footer
      );
    }
  });
  
  exports['default'] = Panel;
  module.exports = exports['default'];