'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _style = require('./style.scss');

var _style2 = _interopRequireDefault(_style);

var _bind = require('classnames/bind');

var _bind2 = _interopRequireDefault(_bind);

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var PanelGroup = function (_React$Component) {
  _inherits(PanelGroup, _React$Component);

  function PanelGroup(props) {
    _classCallCheck(this, PanelGroup);

    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(PanelGroup).call(this, props));

    _this.state = {
      panels: []
    };

    _this.componentWillMount = function () {
      _this.setInitialState();
    };

    _this.activatePanels = function (activePanels) {
      var panels = _this.getPanels();
      var initialPanels = [];

      panels.forEach(function (panel, index) {
        if (activePanels) {
          initialPanels = [].concat(_toConsumableArray(initialPanels), [{ active: !!(activePanels.indexOf(index) !== -1) }]);
        } else {
          initialPanels = [].concat(_toConsumableArray(initialPanels), [{ active: false }]);
        }
      });

      _this.setState({ panels: initialPanels });
    };

    _this.setInitialState = function () {
      _this.activatePanels(_this.props.activePanels);
    };

    _this.componentWillReceiveProps = function (nextProps) {
      _this.activatePanels(nextProps.activePanels);
    };

    _this.collapsePanels = function () {
      var panels = _this.getPanels();
      var collapsedPanels = [];

      panels.forEach(function (panel, index) {
        collapsedPanels = [].concat(_toConsumableArray(collapsedPanels), [{ active: false }]);
      });

      return collapsedPanels;
    };

    _this.getPanels = function () {
      var panels = [];
      _react2.default.Children.forEach(_this.props.children, function (panel) {
        if (_react2.default.isValidElement(panel)) {
          panels.push(panel);
        }
      });
      return panels;
    };

    _this.handlePanelClick = function (panel) {
      var panelIndex = panel.props.panelIndex;
      var state = _this.state.panels;

      if (!_this.props.accordion) {
        state[panelIndex] = { active: !_this.state.panels[panelIndex].active };
        _this.setState({ panels: state });
      } else {
        var resetState = _this.collapsePanels();
        resetState[panelIndex] = { active: !_this.state.panels[panelIndex].active };
        _this.setState({ panels: resetState });
      }
    };

    return _this;
  }

  _createClass(PanelGroup, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      var cx = _bind2.default.bind(_style2.default);
      var panelGroupClass = cx(_style2.default['panel-group'], this.props.optClass);

      var panels = this.getPanels().map(function (panel, index) {
        return _react2.default.cloneElement(panel, {
          key: index,
          panelIndex: index,
          active: _this2.state.panels[index].active,
          onPanelClick: _this2.handlePanelClick
        });
      });

      return _react2.default.createElement(
        'div',
        { className: panelGroupClass },
        panels
      );
    }
  }]);

  return PanelGroup;
}(_react2.default.Component);

PanelGroup.propTypes = {
  /**
   * The panel(s) to be open by default
   */
  activePanels: _react2.default.PropTypes.array,
  /**
   * Whether the panelGroup should allow only one panel to be open at a time
   * Note: if accordion is set to true, the activePanels array will respect
   * only the first item.
   */
  accordion: _react2.default.PropTypes.bool,
  /**
   * An optional CSS class to be used to local style
   */
  optClass: _react2.default.PropTypes.string
};
PanelGroup.defaultProps = {
  activePanels: []
};
exports.default = PanelGroup;