"use strict";

exports.__esModule = true;
exports.default = void 0;

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

var _clsx = _interopRequireDefault(require("clsx"));

var _useMergedState2 = _interopRequireDefault(require("../hooks/use-merged-state"));

var _CheckboxContext = _interopRequireDefault(require("./CheckboxContext"));

var _useRefs2 = _interopRequireDefault(require("../hooks/use-refs"));

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var CheckBoxGroup = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

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

  var _useRefs = (0, _useRefs2.default)(),
      childrenRefs = _useRefs[0],
      setChildrenRefs = _useRefs[1];

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

  var toggleAll = function toggleAll(options) {
    if (options === void 0) {
      options = {};
    }

    if (typeof options === 'boolean') {
      options = {
        checked: options
      };
    }

    var _options = options,
        isChecked = _options.checked,
        skipDisabled = _options.skipDisabled;
    var checkedChildren = childrenRefs.filter(function (item) {
      if (!item.props.bindGroup) {
        return false;
      }

      if (item.props.disabled && skipDisabled) {
        return item.checked;
      }

      return isChecked != null ? isChecked : !item.checked;
    });
    var names = checkedChildren.map(function (item) {
      return item.props.name;
    });
    setChecked(names);
    props.onChange(names);
  };

  var toggle = function toggle(name) {
    setChecked(name);
    props.onChange == null ? void 0 : props.onChange(name);
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      toggleAll: toggleAll
    };
  });
  return /*#__PURE__*/_react.default.createElement(_CheckboxContext.default.Provider, {
    value: {
      parent: {
        props: props
      },
      toggle: toggle,
      checked: checked
    }
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(props.className, bem([props.direction]))
  }, _react.default.Children.toArray(props.children).filter(Boolean).map(function (child, index) {
    var _child$type;

    if (((_child$type = child.type) == null ? void 0 : _child$type.displayName) !== 'Checkbox') return child;
    return /*#__PURE__*/_react.default.cloneElement(child, {
      ref: setChildrenRefs(index)
    });
  })));
});
CheckBoxGroup.defaultProps = {
  defaultValue: []
};
var _default = CheckBoxGroup;
exports.default = _default;