"use strict";

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

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

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

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

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

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

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

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

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

var _omit = _interopRequireDefault(require("../components/rc-util/omit"));

var _checkbox = _interopRequireDefault(require("./checkbox"));

var _grid = _interopRequireDefault(require("../grid"));

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

var Row = _grid.default.Row,
    Col = _grid.default.Col;
var GroupContext = /*#__PURE__*/React.createContext(null);
exports.GroupContext = GroupContext;

var CheckboxGroup = function CheckboxGroup(_ref) {
  var defaultValue = _ref.defaultValue,
      children = _ref.children,
      _ref$options = _ref.options,
      options = _ref$options === void 0 ? [] : _ref$options,
      customizePrefixCls = _ref.prefixCls,
      className = _ref.className,
      style = _ref.style,
      onChange = _ref.onChange,
      rowProps = _ref.rowProps,
      colProps = _ref.colProps,
      direction = _ref.direction,
      restProps = (0, _objectWithoutProperties2.default)(_ref, ["defaultValue", "children", "options", "prefixCls", "className", "style", "onChange", "rowProps", "colProps", "direction"]);

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

  var RestChildren = children;

  var _React$useState = React.useState(restProps.value || defaultValue || []),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      value = _React$useState2[0],
      setValue = _React$useState2[1];

  var _React$useState3 = React.useState([]),
      _React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
      registeredValues = _React$useState4[0],
      setRegisteredValues = _React$useState4[1];

  React.useEffect(function () {
    if ('value' in restProps) {
      setValue(restProps.value || []);
    }
  }, [restProps.value]);

  var getOptions = function getOptions() {
    return options.map(function (option) {
      if (typeof option === 'string') {
        return {
          label: option,
          value: option
        };
      }

      return option;
    });
  };

  var cancelValue = function cancelValue(val) {
    setRegisteredValues(function (prevValues) {
      return prevValues.filter(function (v) {
        return v !== val;
      });
    });
  };

  var registerValue = function registerValue(val) {
    setRegisteredValues(function (prevValues) {
      return [].concat((0, _toConsumableArray2.default)(prevValues), [val]);
    });
  };

  var toggleOption = function toggleOption(option) {
    var optionIndex = value.indexOf(option.value);
    var newValue = (0, _toConsumableArray2.default)(value);

    if (optionIndex === -1) {
      newValue.push(option.value);
    } else {
      newValue.splice(optionIndex, 1);
    }

    if (!('value' in restProps)) {
      setValue(newValue);
    }

    if (onChange) {
      var opts = getOptions();
      onChange(newValue.filter(function (val) {
        return registeredValues.indexOf(val) !== -1;
      }).sort(function (a, b) {
        var indexA = opts.findIndex(function (opt) {
          return opt.value === a;
        });
        var indexB = opts.findIndex(function (opt) {
          return opt.value === b;
        });
        return indexA - indexB;
      }));
    }
  };

  var prefixCls = getPrefixClassName('checkbox', customizePrefixCls);
  var groupPrefixCls = "".concat(prefixCls, "-group");
  var domProps = (0, _omit.default)(restProps, ['value', 'disabled']);

  if (options && options.length > 0) {
    RestChildren = getOptions().map(function (option, index) {
      return /*#__PURE__*/React.createElement(_checkbox.default, {
        prefixCls: prefixCls,
        key: option.value.toString(),
        disabled: 'disabled' in option ? option.disabled : restProps.disabled,
        value: option.value,
        checked: value.indexOf(option.value) !== -1,
        onChange: option.onChange,
        className: "".concat(groupPrefixCls, "-item"),
        style: option.style
      }, option.label);
    });
  }

  var context = {
    toggleOption: toggleOption,
    value: value,
    disabled: restProps.disabled,
    name: restProps.name,
    registerValue: registerValue,
    cancelValue: cancelValue
  };
  var classString = (0, _classnames.default)(groupPrefixCls, className);
  var rowCls = (0, _classnames.default)(rowProps === null || rowProps === void 0 ? void 0 : rowProps.className, (0, _defineProperty2.default)({}, "".concat(groupPrefixCls, "-row-vertical"), direction === 'vertical'));
  var rowGutter = direction === 'vertical' ? [0, 20] : [32];
  return /*#__PURE__*/React.createElement("div", Object.assign({
    className: classString,
    style: style
  }, domProps), /*#__PURE__*/React.createElement(GroupContext.Provider, {
    value: context
  }, /*#__PURE__*/React.createElement(Row, Object.assign({
    gutter: rowGutter,
    className: rowCls
  }, rowProps), React.Children.map(RestChildren, function (item, index) {
    return (
      /* eslint-disable react/no-array-index-key */
      item ? /*#__PURE__*/React.createElement(Col, Object.assign({
        key: index
      }, colProps), item) : null
    );
  }))));
};

exports.CheckboxGroup = CheckboxGroup;

var _default = /*#__PURE__*/React.memo(CheckboxGroup);

exports.default = _default;