"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var React = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _components = require("../components");

var _checkboxGroup = require("./checkbox-group");

var _context = require("../config-provider/context");

var InternalCheckbox = function InternalCheckbox(_ref, ref) {
  var prefixCls = _ref.prefixCls,
      className = _ref.className,
      children = _ref.children,
      _ref$indeterminate = _ref.indeterminate,
      indeterminate = _ref$indeterminate === void 0 ? false : _ref$indeterminate,
      style = _ref.style,
      onMouseEnter = _ref.onMouseEnter,
      onMouseLeave = _ref.onMouseLeave,
      _ref$skipGroup = _ref.skipGroup,
      skipGroup = _ref$skipGroup === void 0 ? false : _ref$skipGroup,
      restProps = (0, _objectWithoutProperties2.default)(_ref, ["prefixCls", "className", "children", "indeterminate", "style", "onMouseEnter", "onMouseLeave", "skipGroup"]);

  var _React$useContext = React.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var checkboxGroup = React.useContext(_checkboxGroup.GroupContext);
  var prevValue = React.useRef(restProps.value);
  React.useEffect(function () {
    checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.registerValue(restProps.value);
  }, []);
  React.useEffect(function () {
    if (skipGroup) {
      return;
    }

    if (restProps.value !== prevValue.current) {
      checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.cancelValue(prevValue.current);
      checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.registerValue(restProps.value);
    }

    return function () {
      return checkboxGroup === null || checkboxGroup === void 0 ? void 0 : checkboxGroup.cancelValue(restProps.value);
    };
  }, [restProps.value]);
  var checkboxCls = getPrefixClassName('checkbox', prefixCls);
  var checkboxProps = (0, _objectSpread2.default)({}, restProps);

  if (checkboxGroup && !skipGroup) {
    checkboxProps.onChange = function () {
      if (restProps.onChange) {
        restProps.onChange.apply(restProps, arguments);
      }

      if (checkboxGroup.toggleOption) {
        checkboxGroup.toggleOption({
          label: children,
          value: restProps.value
        });
      }
    };

    checkboxProps.name = checkboxGroup.name;
    checkboxProps.checked = checkboxGroup.value.indexOf(restProps.value) !== -1;
    checkboxProps.disabled = restProps.disabled || checkboxGroup.disabled;
  }

  return /*#__PURE__*/React.createElement("label", {
    className: (0, _classnames.default)("".concat(checkboxCls, "-wrapper"), checkboxProps.checked && "".concat(checkboxCls, "-wrapper-checked"), checkboxProps.disabled && "".concat(checkboxCls, "-wrapper-disabled"), className),
    style: style,
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave
  }, /*#__PURE__*/React.createElement(_components.RcCheckbox, Object.assign({}, checkboxProps, {
    prefixCls: checkboxCls,
    className: (0, _classnames.default)(indeterminate && "".concat(checkboxCls, "-indeterminate")),
    ref: ref
  })), children !== undefined && /*#__PURE__*/React.createElement("span", null, children));
};

var Checkbox = /*#__PURE__*/React.forwardRef(InternalCheckbox);
Checkbox.displayName = 'Checkbox';
var _default = Checkbox;
exports.default = _default;