"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 _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

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

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

var _bem = require("@zarm-design/bem");

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

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

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

var _list = _interopRequireDefault(require("../list"));

var _context = require("./context");

var getValue = function getValue(props, defaultValue) {
  var _ref, _props$value;

  return (_ref = (_props$value = props.value) !== null && _props$value !== void 0 ? _props$value : props.defaultValue) !== null && _ref !== void 0 ? _ref : defaultValue;
};

var CheckboxGroup = function CheckboxGroup(props) {
  var _ref2;

  var _useState = (0, _react.useState)(getValue(props, [])),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      value = _useState2[0],
      setValue = _useState2[1];

  var type = props.type,
      block = props.block,
      disabled = props.disabled,
      iconAlign = props.iconAlign,
      className = props.className,
      style = props.style;

  var _useContext = (0, _react.useContext)(_configProvider.ConfigContext),
      prefixCls = _useContext.prefixCls;

  var bem = (0, _bem.createBEM)('checkbox-group', {
    prefixCls: prefixCls
  });
  var cls = bem([(_ref2 = {}, (0, _defineProperty2.default)(_ref2, "".concat(type), !!type), (0, _defineProperty2.default)(_ref2, "block", block), (0, _defineProperty2.default)(_ref2, "disabled", disabled), _ref2), className]);
  (0, _react.useEffect)(function () {
    if (props.value === undefined) return;
    if ((0, _isEqual.default)(props.value, value)) return;
    setValue(getValue(props, []));
  }, [props.value]);
  return /*#__PURE__*/_react.default.createElement(_context.CheckboxGroupContext.Provider, {
    value: {
      value: value,
      type: type,
      block: block,
      disabled: disabled,
      iconAlign: iconAlign,
      check: function check(v) {
        var _props$onChange;

        var values = [].concat((0, _toConsumableArray2.default)(value), [v]);
        setValue(values);
        (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, values);
      },
      uncheck: function uncheck(v) {
        var _props$onChange2;

        var values = value.filter(function (item) {
          return item !== v;
        });
        setValue(values);
        (_props$onChange2 = props.onChange) === null || _props$onChange2 === void 0 ? void 0 : _props$onChange2.call(props, values);
      }
    }
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: cls,
    style: style
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('inner')
  }, type === 'list' ? /*#__PURE__*/_react.default.createElement(_list.default, null, props.children) : props.children)));
};

CheckboxGroup.displayName = 'CheckboxGroup';
CheckboxGroup.defaultProps = {
  block: false,
  disabled: false,
  iconAlign: 'before'
};
var _default = CheckboxGroup;
exports.default = _default;