'use strict';

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; }; }();

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 React = require('react');
var PropTypes = require('prop-types');
var _ = require('lodash');
var Button = require('react-bootstrap').Button;
var ButtonGroup = require('react-bootstrap').ButtonGroup;

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

  function ViewSwitcher() {
    _classCallCheck(this, ViewSwitcher);

    return _possibleConstructorReturn(this, (ViewSwitcher.__proto__ || Object.getPrototypeOf(ViewSwitcher)).apply(this, arguments));
  }

  _createClass(ViewSwitcher, [{
    key: 'buttonFactory',


    /**
     * return array of button components based on buttonLabels and activeButton
     *
     * @return {React.Fragment}  array of buttons
     */
    value: function buttonFactory() {
      var _this2 = this;

      // const onClick = _.get(this.props, 'onClick', () => {});
      return _.map(this.props.buttonLabels, function (label) {
        var active = _this2.props.activeButton === label;
        return React.createElement(
          Button,
          {
            key: label,
            active: active,
            bsSize: 'xsmall',
            onClick: _this2.props.onClick.bind(_this2, label),
            disabled: _this2.props.disabled
          },
          label
        );
      });
    }

    /**
     * Render view switcher component.
     *
     * @returns {React.Component} The component.
     */

  }, {
    key: 'render',
    value: function render() {
      var buttons = this.buttonFactory();
      return React.createElement(
        'div',
        { className: 'view-switcher action-bar' },
        React.createElement(
          'span',
          { className: 'view-switcher-label' },
          this.props.label
        ),
        React.createElement(
          ButtonGroup,
          null,
          buttons
        )
      );
    }
  }]);

  return ViewSwitcher;
}(React.Component);

ViewSwitcher.propTypes = {
  label: PropTypes.string,
  buttonLabels: PropTypes.arrayOf(PropTypes.string).isRequired,
  activeButton: PropTypes.string,
  onClick: PropTypes.func,
  disabled: PropTypes.bool
};

ViewSwitcher.defaultProps = {
  label: '',
  activeButton: '',
  onClick: function onClick() {},
  disabled: false
};

ViewSwitcher.displayName = 'ViewSwitcher';

module.exports = ViewSwitcher;