'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

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 _bind = require('classnames/bind');

var _bind2 = _interopRequireDefault(_bind);

var _ButtonToggle = require('./ButtonToggle');

var _ButtonToggle2 = _interopRequireDefault(_ButtonToggle);

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

var _style2 = _interopRequireDefault(_style);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

/**
 * The ButtonGroup component.
 */

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

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

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

    _this.state = {
      checkedOption: _this.props.defaultOption !== undefined ? _this.props.options[_this.props.defaultOption].value : ''
    };

    _this.handleChange = function (event, value) {
      event.persist();
      _this.setState({ checkedOption: value }, function () {
        if (typeof this.props.changeCallback === 'function') {
          this.props.changeCallback(event, value);
        }
      });
    };

    return _this;
  }

  _createClass(ButtonGroup, [{
    key: 'componentWillMount',
    value: function componentWillMount() {
      if (typeof this.props.defaultOption !== 'undefined') {
        this.props.options[this.props.defaultOption].checked = true;
      }
    }
  }, {
    key: 'getOptions',
    value: function getOptions() {
      var _this2 = this;

      var groupName = this.props.name;
      var buttonStyle = this.props.buttonStyle;
      var _props = this.props;
      var options = _props.options;
      var label = _props.label;
      var name = _props.name;
      var value = _props.value;
      var required = _props.required;
      var defaultOption = _props.defaultOption;
      var changeCallback = _props.changeCallback;

      var other = _objectWithoutProperties(_props, ['options', 'label', 'name', 'value', 'required', 'defaultOption', 'changeCallback']);

      return this.props.options.map(function (buttonToggle, index) {
        return _react2.default.createElement(_ButtonToggle2.default, _extends({
          key: buttonToggle.value,
          value: buttonToggle.value,
          label: buttonToggle.label,
          name: groupName,
          checked: _this2.state.checkedOption === buttonToggle.value,
          size: 'lg',
          optClass: _this2.state.checkedOption === buttonToggle.value ? buttonStyle : 'secondary',
          changeCallback: _this2.handleChange
        }, other));
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var cx = _bind2.default.bind(_style2.default);
      var buttonGroupClass = cx(_style2.default['button-group'], this.props.optClass);

      return _react2.default.createElement(
        'div',
        { className: buttonGroupClass },
        this.props.required ? _react2.default.createElement(
          'span',
          { className: _style2.default.asterisk },
          '*'
        ) : null,
        this.props.label ? _react2.default.createElement(
          'label',
          { className: _style2.default['button-group-label'] },
          this.props.label
        ) : null,
        _react2.default.createElement(
          'div',
          { className: _style2.default.options },
          this.getOptions()
        )
      );
    }
  }]);

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

ButtonGroup.defaultProps = {
  disabled: false,
  required: false
};
ButtonGroup.propTypes = {
  /**
   * Text shown above the button group.
   */
  label: _react2.default.PropTypes.string,
  /**
   * The name that will be applied to all radio buttons inside it.
   */
  name: _react2.default.PropTypes.string.isRequired,
  /**
   * Whether the button group is required.
   */
  required: _react2.default.PropTypes.bool,
  /**
   * Whether the button group is disabled.
   */
  disabled: _react2.default.PropTypes.bool,
  /**
   * A list of options for the button group.
   */
  options: _react2.default.PropTypes.array.isRequired,
  /**
   * Which option is checked by default.
   */
  defaultOption: _react2.default.PropTypes.number,
  /**
   * A callback function to be called when an option is changed.
   */
  changeCallback: _react2.default.PropTypes.func,
  /**
   * The style for the buttons in the group.
   */
  buttonStyle: _react2.default.PropTypes.string
};
exports.default = ButtonGroup;