'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

require('./style.css');

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

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 ButtonToggle = function (_Component) {
  _inherits(ButtonToggle, _Component);

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

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

    _this.state = {
      selected: props.defaultValue
    };
    return _this;
  }

  ButtonToggle.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    this.setState({ selected: nextProps.defaultValue });
  };

  ButtonToggle.prototype.onToggleClick = function onToggleClick(event) {
    var selected = event.target.value;
    if (typeof this.props.onChangeCallback === 'function') {
      this.props.onChangeCallback(selected);
    }
    this.setState({ selected: selected });
  };

  ButtonToggle.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        id = _props.id,
        labelText = _props.labelText,
        option1 = _props.option1,
        option2 = _props.option2;
    var selected = this.state.selected;

    return _react2.default.createElement(
      'span',
      null,
      _react2.default.createElement(
        'label',
        { htmlFor: id, className: 'ma__label--inline ma__label--small' },
        labelText
      ),
      _react2.default.createElement(
        'button',
        { onClick: function onClick(e) {
            return _this2.onToggleClick(e);
          }, value: option1.value, type: 'button', className: 'ma__button-toggle ' + (option1.value === selected && 'ma__button-toggle--selected') },
        option1.text
      ),
      ' |',
      _react2.default.createElement(
        'button',
        { onClick: function onClick(e) {
            return _this2.onToggleClick(e);
          }, value: option2.value, type: 'button', className: 'ma__button-toggle ' + (option2.value === selected && 'ma__button-toggle--selected') },
        option2.text
      )
    );
  };

  return ButtonToggle;
}(_react.Component);

ButtonToggle.propTypes = process.env.NODE_ENV !== "production" ? {
  /** Option 1 for the buttonToggle */
  option1: _propTypes2.default.shape({
    value: _propTypes2.default.string.isRequired,
    text: _propTypes2.default.string.isRequired
  }),
  /** Option 2 for the buttonToggle */
  option2: _propTypes2.default.shape({
    value: _propTypes2.default.string.isRequired,
    text: _propTypes2.default.string.isRequired
  }),
  /** An id that specifies which element the label is bound to */
  id: _propTypes2.default.string.isRequired,
  /** A label text displayed for the buttonToggle */
  labelText: _propTypes2.default.string.isRequired,
  /** Custom onChange function that receives the selected value */
  onChangeCallback: _propTypes2.default.func,
  /** Default value selected */
  defaultValue: _propTypes2.default.string.isRequired
} : {};

exports.default = ButtonToggle;
module.exports = exports['default'];