import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var _jsxFileName = 'src/checkbox/CheckboxGroup.jsx';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

/**
 * CheckboxGroup
 */

var CheckboxGroup = function (_Component) {
    _inherits(CheckboxGroup, _Component);

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

        var _this = _possibleConstructorReturn(this, (CheckboxGroup.__proto__ || Object.getPrototypeOf(CheckboxGroup)).call(this, props));

        _this.onItemChange = function (itemValue) {
            return function (e) {
                var value = _this.isControlled ? _this.props.value : _this.state.value;
                value = Array.isArray(value) ? [].concat(_toConsumableArray(value)) : [value];
                var itemIndex = value.indexOf(itemValue);
                if (itemIndex === -1) {
                    value.push(itemValue);
                } else {
                    value.splice(itemIndex, 1);
                }
                if (!_this.isControlled) {
                    _this.setState({ value: value });
                }
                if (_this.props.onChange) {
                    _this.props.onChange(e, value);
                }
            };
        };

        _this.isControlled = props.value != null;
        if (!_this.isControlled) {
            _this.state = {
                value: props.defaultValue
            };
        } else {
            _this.state = {};
        }
        return _this;
    }

    _createClass(CheckboxGroup, [{
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(newProps) {
            if (!this.isControlled && newProps.value != null) {
                this.isControlled = true;
                this.setState({
                    value: null
                });
            }
            if (!this.isControlled && newProps.defaultValue !== this.props.defaultValue) {
                this.setState({
                    value: newProps.defaultValue
                });
            }
        }
    }, {
        key: 'render',
        value: function render() {
            var _this2 = this;

            var _props = this.props,
                prefixCls = _props.prefixCls,
                className = _props.className,
                propsValue = _props.value,
                defaultValue = _props.defaultValue,
                disabled = _props.disabled,
                name = _props.name,
                onChange = _props.onChange,
                children = _props.children,
                error = _props.error,
                inputProps = _props.inputProps,
                size = _props.size,
                other = _objectWithoutProperties(_props, ['prefixCls', 'className', 'value', 'defaultValue', 'disabled', 'name', 'onChange', 'children', 'error', 'inputProps', 'size']);

            var value = this.isControlled ? propsValue : this.state.value;
            value = Array.isArray(value) ? [].concat(_toConsumableArray(value)) : [value];
            var items = React.Children.map(children, function (child, index) {
                if (!React.isValidElement(child)) {
                    return null;
                }
                var checked = value.indexOf(child.props.value) !== -1;
                return React.cloneElement(child, {
                    key: child.props.value || index,
                    checked: checked,
                    size: size,
                    name: name || child.props.name,
                    disabled: disabled || child.props.disabled,
                    onChange: _this2.onItemChange(child.props.value)
                });
            });
            var classes = classNames(prefixCls, _defineProperty({}, prefixCls + '-disabled', disabled), className);

            return React.createElement(
                'div',
                Object.assign({ className: classes }, other, {
                    __source: {
                        fileName: _jsxFileName,
                        lineNumber: 115
                    },
                    __self: this
                }),
                items
            );
        }
    }]);

    return CheckboxGroup;
}(Component);

CheckboxGroup.propTypes = {
    prefixCls: PropTypes.string,
    /** 子组件name */
    name: PropTypes.string,
    /** 是否禁用 */
    disabled: PropTypes.bool,
    /** 当前值（适用于受控组件） */
    value: PropTypes.array,
    /** 默认值 */
    defaultValue: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
    className: PropTypes.string,
    /** 选中状态改变时的回调 function(e, value) */
    onChange: PropTypes.func,
    /** 尺寸 */
    size: PropTypes.oneOf(['small', 'medium', 'large'])
};
CheckboxGroup.defaultProps = {
    defaultValue: [],
    prefixCls: 'ne-checkbox-group',
    disabled: false
};
export default CheckboxGroup;