import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
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/switchBase/SwitchBase.jsx';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Icon from '../icon';

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

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

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

        _this.onInputChange = function (e) {
            var checked = e.target.checked;
            if (!_this.isControlled) {
                _this.setState({ checked: checked });
            }
            if (_this.props.onChange) {
                _this.props.onChange(e, checked);
            }
        };

        _this.getEl = function (el) {
            var inputRef = _this.props.inputRef;

            if (inputRef) {
                inputRef(el);
            }
        };

        _this.isControlled = props.checked != null;
        if (!_this.isControlled) {
            _this.state = {
                checked: props.defaultChecked
            };
        }
        return _this;
    }

    _createClass(SwitchBase, [{
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(newProps) {
            if (!this.isControlled && newProps.checked != null) {
                this.isControlled = true;
                this.setState({
                    checked: null
                });
            }
        }
    }, {
        key: 'render',
        value: function render() {
            var _classNames, _classNames2;

            var _props = this.props,
                prefixCls = _props.prefixCls,
                inputType = _props.inputType,
                inputClass = _props.inputClass,
                checkedIcon = _props.checkedIcon,
                uncheckedIcon = _props.uncheckedIcon,
                indeterminate = _props.indeterminate,
                indeterminateIcon = _props.indeterminateIcon,
                inputRef = _props.inputRef,
                className = _props.className,
                disabled = _props.disabled,
                propsChecked = _props.checked,
                defaultChecked = _props.defaultChecked,
                label = _props.label,
                onChange = _props.onChange,
                iconRender = _props.iconRender,
                error = _props.error,
                size = _props.size,
                inputProps = _props.inputProps,
                other = _objectWithoutProperties(_props, ['prefixCls', 'inputType', 'inputClass', 'checkedIcon', 'uncheckedIcon', 'indeterminate', 'indeterminateIcon', 'inputRef', 'className', 'disabled', 'checked', 'defaultChecked', 'label', 'onChange', 'iconRender', 'error', 'size', 'inputProps']);

            var checked = this.isControlled ? propsChecked : this.state.checked;
            var classes = classNames(prefixCls + '-container', (_classNames = {}, _defineProperty(_classNames, prefixCls + '-container-checked', checked), _defineProperty(_classNames, prefixCls + '-container-disabled', disabled), _classNames));
            var icon = indeterminate ? indeterminateIcon : checkedIcon;
            return React.createElement(
                'label',
                {
                    className: classNames(prefixCls + '-label', (_classNames2 = {}, _defineProperty(_classNames2, prefixCls + '-label-disabled', disabled), _defineProperty(_classNames2, prefixCls + '-' + size, size), _classNames2), className), __source: {
                        fileName: _jsxFileName,
                        lineNumber: 99
                    },
                    __self: this
                },
                React.createElement(
                    'span',
                    { className: classes, tabIndex: disabled ? -1 : null, __source: {
                            fileName: _jsxFileName,
                            lineNumber: 108
                        },
                        __self: this
                    },
                    React.createElement('input', Object.assign({
                        className: '' + prefixCls,
                        checked: checked,
                        onChange: this.onInputChange,
                        ref: this.getEl,
                        disabled: disabled
                    }, inputProps, other, {
                        __source: {
                            fileName: _jsxFileName,
                            lineNumber: 109
                        },
                        __self: this
                    })),
                    React.createElement(
                        'span',
                        { className: prefixCls + '-icon', __source: {
                                fileName: _jsxFileName,
                                lineNumber: 118
                            },
                            __self: this
                        },
                        iconRender ? iconRender(indeterminate ? 'indeterminate' : checked ? 'checked' : 'unchecked') : checked ? icon : uncheckedIcon
                    )
                ),
                React.createElement(
                    'span',
                    { className: prefixCls + '-label-text', __source: {
                            fileName: _jsxFileName,
                            lineNumber: 124
                        },
                        __self: this
                    },
                    typeof label === 'function' ? label(checked, disabled) : label
                )
            );
        }
    }]);

    return SwitchBase;
}(Component);

SwitchBase.propTypes = {
    inputType: PropTypes.string,
    prefixCls: PropTypes.string,
    label: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
    disabled: PropTypes.bool,
    checked: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
    defaultChecked: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
    checkedIcon: PropTypes.node,
    className: PropTypes.string,
    uncheckedIcon: PropTypes.node,
    indeterminate: PropTypes.bool,
    indeterminateIcon: PropTypes.node,
    iconRender: PropTypes.func,
    inputRef: PropTypes.func,
    onChange: PropTypes.func,
    size: PropTypes.oneOf(['small', 'medium', 'large'])
};
SwitchBase.defaultProps = {
    type: 'checkbox',
    defaultChecked: false,
    prefixCls: 'ne-switch-base',
    disabled: false,
    indeterminate: false,
    indeterminateIcon: React.createElement(Icon, { type: 'down-arrow', 'aria-hidden': 'true', __source: {
            fileName: _jsxFileName,
            lineNumber: 31
        },
        __self: this
    }),
    checkedIcon: React.createElement(Icon, { type: 'ok', 'aria-hidden': 'true', __source: {
            fileName: _jsxFileName,
            lineNumber: 32
        },
        __self: this
    }),
    size: 'medium'
};
export default SwitchBase;