'use strict';

exports.__esModule = true;

var _isUndefined2 = require('lodash/isUndefined');

var _isUndefined3 = _interopRequireDefault(_isUndefined2);

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 _react = require('react');

var React = _interopRequireWildcard(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _setDisplayName = require('recompose/setDisplayName');

var _setDisplayName2 = _interopRequireDefault(_setDisplayName);

var _utils = require('./utils');

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

var _propTypes2 = _interopRequireDefault(_propTypes);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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

function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return 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) : _defaults(subClass, superClass); }

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

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

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.handleChange = function (event) {
      var _this$props = _this.props,
          onChange = _this$props.onChange,
          disabled = _this$props.disabled,
          value = _this$props.value;

      var checked = !_this.isChecked();

      if (disabled) {
        return;
      }

      _this.setState({ checked: checked }, function () {
        onChange && onChange(value, checked, event);
      });
    };

    _this.state = {
      checked: props.defaultChecked
    };
    return _this;
  }

  Checkbox.prototype.isChecked = function isChecked() {
    var checked = this.props.checked;

    return (0, _isUndefined3.default)(checked) ? this.state.checked : checked;
  };

  Checkbox.prototype.render = function render() {
    var _classNames;

    var _props = this.props,
        inline = _props.inline,
        disabled = _props.disabled,
        className = _props.className,
        onChange = _props.onChange,
        children = _props.children,
        title = _props.title,
        inputRef = _props.inputRef,
        defaultChecked = _props.defaultChecked,
        indeterminate = _props.indeterminate,
        tabIndex = _props.tabIndex,
        classPrefix = _props.classPrefix,
        props = _objectWithoutProperties(_props, ['inline', 'disabled', 'className', 'onChange', 'children', 'title', 'inputRef', 'defaultChecked', 'indeterminate', 'tabIndex', 'classPrefix']);

    var checked = this.isChecked();
    var addPrefix = (0, _utils.prefix)(classPrefix);
    var classes = (0, _classnames2.default)(classPrefix, className, (_classNames = {}, _classNames[addPrefix('inline')] = inline, _classNames[addPrefix('indeterminate')] = indeterminate, _classNames[addPrefix('disabled')] = disabled, _classNames[addPrefix('checked')] = checked, _classNames));

    var unhandled = (0, _utils.getUnhandledProps)(Checkbox, props);

    var _partitionHTMLProps = (0, _utils.partitionHTMLProps)(unhandled),
        htmlInputProps = _partitionHTMLProps[0],
        rest = _partitionHTMLProps[1];

    var input = React.createElement(
      'span',
      { tabIndex: disabled ? -1 : tabIndex, className: addPrefix('wrapper') },
      React.createElement('input', _extends({}, htmlInputProps, {
        defaultChecked: defaultChecked,
        type: 'checkbox',
        ref: inputRef,
        disabled: disabled,
        onChange: this.handleChange
      })),
      React.createElement('span', { className: addPrefix('inner') })
    );

    return React.createElement(
      'div',
      _extends({}, rest, { className: classes }),
      React.createElement(
        'div',
        { className: addPrefix('checker') },
        React.createElement(
          'label',
          { title: title },
          input,
          children
        )
      )
    );
  };

  return Checkbox;
}(React.Component);

Checkbox.defaultProps = {
  tabIndex: 0
};
Checkbox.handledProps = ['checked', 'children', 'className', 'classPrefix', 'defaultChecked', 'disabled', 'indeterminate', 'inline', 'inputRef', 'onChange', 'tabIndex', 'title', 'value'];
Checkbox.propTypes = {
  title: _propTypes2.default.string,
  className: _propTypes2.default.string,
  inline: _propTypes2.default.bool,
  disabled: _propTypes2.default.bool,
  checked: _propTypes2.default.bool,
  defaultChecked: _propTypes2.default.bool,
  indeterminate: _propTypes2.default.bool,
  onChange: _propTypes2.default.func,
  inputRef: function inputRef() {
    return (typeof (React.ElementRef == null ? {} : React.ElementRef) === 'function' ? _propTypes2.default.instanceOf(React.ElementRef == null ? {} : React.ElementRef) : _propTypes2.default.any).apply(this, arguments);
  },
  value: _propTypes2.default.any,
  children: _propTypes2.default.node,
  classPrefix: _propTypes2.default.string,
  tabIndex: _propTypes2.default.number
};
exports.default = (0, _setDisplayName2.default)('Checkbox')((0, _utils.defaultProps)({
  classPrefix: 'checkbox'
})(Checkbox));