'use strict';

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

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _index = require('./index');

var _index2 = _interopRequireDefault(_index);

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

// eslint-disable-next-line react/prefer-stateless-function
var OptionalLabel = function (_React$Component) {
    (0, _inherits3['default'])(OptionalLabel, _React$Component);

    function OptionalLabel(props) {
        (0, _classCallCheck3['default'])(this, OptionalLabel);

        var _this = (0, _possibleConstructorReturn3['default'])(this, (OptionalLabel.__proto__ || Object.getPrototypeOf(OptionalLabel)).call(this, props));

        _this.handleChange = function (e, v) {
            var onChange = _this.props.onChange;

            var value = [].concat((0, _toConsumableArray3['default'])(_this.state.value));
            var checked = e.target.checked;

            if (checked) {
                value.push(v.value);
            } else {
                value.splice(value.indexOf(v.value), 1);
            }
            _this.setState({ value: value });
            if (onChange) {
                onChange(value);
            }
        };
        _this.state = {
            value: props.value || props.defaultValue
        };
        return _this;
    }

    (0, _createClass3['default'])(OptionalLabel, [{
        key: 'render',
        value: function render() {
            var _this2 = this;

            var options = this.props.options;
            var value = this.state.value;

            return _react2['default'].createElement(
                'div',
                null,
                options.map(function (v) {
                    return _react2['default'].createElement(_index2['default'], { key: v.value, label: v.label, checked: Array.isArray(value) && value.length > 0 ? value.includes(v.value) : false, onChange: function onChange(e) {
                            return _this2.handleChange(e, v);
                        } });
                })
            );
        }
    }], [{
        key: 'getDerivedStateFromProps',
        value: function getDerivedStateFromProps(nextProps) {
            if ('value' in nextProps) {
                return {
                    value: nextProps.value || []
                };
            }
            return null;
        }
    }]);
    return OptionalLabel;
}(_react2['default'].Component);

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