"use strict";

var __assign = void 0 && (void 0).__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

var __spreadArray = void 0 && (void 0).__spreadArray || function (to, from) {
  for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) {
    to[j] = from[i];
  }

  return to;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

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

var jsx_runtime_1 = require("react/jsx-runtime");

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

var classnames_1 = __importDefault(require("classnames"));

var useStaticCallback_1 = __importDefault(require("../../hooks/useStaticCallback"));

var formateOptions_1 = __importDefault(require("../_utils/formateOptions"));

var Checkbox_1 = require("./Checkbox");

var prefixCls = 'dmc-checkbox-group';
var defaultProps = {
  options: [],
  valueKey: 'value',
  labelKey: 'label'
};
var CheckboxGroup = React.forwardRef(function (props, ref) {
  var _a = props,
      type = _a.type,
      size = _a.size,
      options = _a.options,
      valueKey = _a.valueKey,
      labelKey = _a.labelKey,
      onChange = _a.onChange,
      defaultValue = _a.defaultValue,
      value = _a.value,
      disabled = _a.disabled,
      className = _a.className,
      itemClassName = _a.itemClassName,
      renderItem = _a.renderItem,
      restProps = __rest(_a, ["type", "size", "options", "valueKey", "labelKey", "onChange", "defaultValue", "value", "disabled", "className", "itemClassName", "renderItem"]);

  var _b = React.useState(defaultValue || []),
      unControlVal = _b[0],
      setUnControlVal = _b[1];

  var opts = React.useMemo(function () {
    return formateOptions_1["default"](options, labelKey, valueKey);
  }, [options, labelKey, valueKey]);
  var isControl = ('value' in props);
  var val = isControl ? value !== null && value !== void 0 ? value : [] : unControlVal;
  var onCheckboxChange = useStaticCallback_1["default"](function (_checked, value) {
    var curr = __spreadArray([], val);

    var index = curr.indexOf(value);
    index === -1 ? curr.push(value) : curr.splice(index, 1);
    !isControl && setUnControlVal(curr);
    onChange && onChange(curr);
  });
  return jsx_runtime_1.jsx("div", __assign({}, restProps, {
    className: classnames_1["default"](prefixCls, className),
    ref: ref
  }, {
    children: opts.map(function (it) {
      var vk = it[valueKey];
      return jsx_runtime_1.jsx(Checkbox_1.CheckboxMemo, __assign({
        value: vk,
        type: type,
        size: size,
        disabled: it.disabled || disabled,
        checked: val.includes(vk),
        onChange: onCheckboxChange,
        className: itemClassName,
        title: it[labelKey]
      }, {
        children: renderItem && renderItem(it)
      }), vk);
    })
  }), void 0);
});
CheckboxGroup.defaultProps = defaultProps;
CheckboxGroup.displayName = 'CheckboxGroup';
exports["default"] = CheckboxGroup;
var CheckboxGroupMemo = React.memo(CheckboxGroup);
exports.CheckboxGroupMemo = CheckboxGroupMemo;
CheckboxGroupMemo.displayName = 'CheckboxGroupMemo';

