var _excluded = ["parent"];

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React, { forwardRef, useContext, useImperativeHandle, useMemo } from 'react';
import Checker from './Checker';
import CheckBoxContext from './CheckboxContext';
import useMergedState from '../hooks/use-merged-state';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
var CheckBox = /*#__PURE__*/forwardRef(function (props, ref) {
  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('checkbox', prefixCls),
      bem = _createNamespace[0];

  var _useContext2 = useContext(CheckBoxContext),
      parent = _useContext2.parent,
      context = _objectWithoutPropertiesLoose(_useContext2, _excluded);

  var _useMergedState = useMergedState({
    value: props.checked,
    defaultValue: props.defaultChecked
  }),
      checked = _useMergedState[0],
      setChecked = _useMergedState[1];

  var setParentValue = function setParentValue(isChecked) {
    var name = props.name;
    var max = parent.props.max;
    var value = context.checked.slice();

    if (isChecked) {
      var overlimit = max && value.length >= max;

      if (!overlimit && value.indexOf(name) === -1) {
        value.push(name);

        if (props.bindGroup) {
          context.toggle(value);
        }
      }
    } else {
      var index = value.indexOf(name);

      if (index !== -1) {
        value.splice(index, 1);

        if (props.bindGroup) {
          context.toggle(value);
        }
      }
    }
  };

  var isChecked = useMemo(function () {
    if (parent && props.bindGroup) {
      return context.checked.indexOf(props.name) !== -1;
    }

    return checked;
  }, [context.checked, checked]);

  var toggle = function toggle(newValue) {
    if (newValue === void 0) {
      newValue = !isChecked;
    }

    if (parent && props.bindGroup) {
      setParentValue(newValue);
    } else {
      setChecked(newValue);
      props.onChange == null ? void 0 : props.onChange(newValue);
    }
  };

  useImperativeHandle(ref, function () {
    return {
      toggle: toggle,
      checked: isChecked,
      props: props
    };
  });
  return /*#__PURE__*/React.createElement(Checker, _extends({}, props, {
    bem: bem,
    role: "checkbox",
    parent: parent,
    checked: isChecked,
    className: props.className,
    bindGroup: props.bindGroup,
    onToggle: toggle
  }));
});
CheckBox.displayName = 'Checkbox';
CheckBox.defaultProps = {
  bindGroup: true
};
export default CheckBox;