var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellCheckboxGroup/index.tsx
var pisellCheckboxGroup_exports = {};
__export(pisellCheckboxGroup_exports, {
  default: () => pisellCheckboxGroup_default
});
module.exports = __toCommonJS(pisellCheckboxGroup_exports);
var import_react = __toESM(require("react"));
var import_usePisellConfig = __toESM(require("../pisell-config-provider/hooks/usePisellConfig"));
var import_classnames = __toESM(require("classnames"));
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_utils = require("@pisell/utils");
var import_utils2 = require("@pisell/utils");
var import_index = require("./index.less");
var import_react2 = require("react");
var import_pisellRow = __toESM(require("../pisellRow"));
var import_mock = require("./mock");
var PisellCheckboxGroup = (props) => {
  const config = (0, import_usePisellConfig.default)();
  const {
    platform,
    gap = 10,
    style,
    options = import_mock.mockList,
    onChange,
    value,
    renderOption = (option) => {
      return option.label;
    },
    padding = 0,
    rowKey = "value",
    mode = "multiple",
    direction,
    fullWidth,
    iconPosition = "left",
    optionClassName,
    optionActiveClassName,
    onChangeDebounce,
    onItemClick,
    showActiveBackground = false,
    ...rest
  } = props;
  const [_value, set_value] = (0, import_react2.useState)([]);
  (0, import_react.useEffect)(() => {
    initValue();
  }, [value]);
  const initValue = () => {
    let _val = value;
    if (!value) {
      _val = [];
    } else {
      if (!Array.isArray(value)) {
        _val = [value];
      }
    }
    set_value(_val);
  };
  const _style = (0, import_react.useMemo)(() => {
    const gapVal = (0, import_utils.isNumber)(gap) ? gap + "px" : gap;
    const paddingVal = (0, import_utils.isNumber)(padding) ? padding + "px" : padding;
    let obj = {
      gap: gapVal,
      padding: paddingVal
    };
    if ("horizontal" === direction) {
      obj = {
        ...obj,
        flexDirection: "row",
        overflowX: "auto"
      };
    }
    return { ...obj, ...style };
  }, [style, gap, padding, direction]);
  const rowStyle = (0, import_react.useMemo)(() => {
    if ("horizontal" === direction) {
      if (false === fullWidth) {
        return {
          flexShrink: 0,
          width: "auto"
        };
      }
      return {
        flexShrink: 0
      };
    }
    return {};
  }, [direction, fullWidth]);
  const _onChange = (val, item) => {
    onChange == null ? void 0 : onChange(mode === "single" ? val[0] : val, item);
  };
  const { run } = (0, import_ahooks.useDebounceFn)(
    (key, item) => {
      handleSelectItem(key, item);
    },
    {
      wait: 400,
      leading: true,
      trailing: false
    }
  );
  const handleSelectItem = (key, item) => {
    let newVal = [key];
    if (mode === "multiple") {
      newVal = (0, import_utils2.changeArray)(_value, key, "this");
    }
    _onChange(newVal, item);
    set_value(newVal);
  };
  const CheckBoxItem = mode === "multiple" ? import_antd.Checkbox : import_antd.Radio;
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      ...rest,
      className: (0, import_classnames.default)("pisell-checkbox-group", rest.className),
      style: _style
    },
    options.map((option, index) => {
      const active = _value.includes(option[rowKey]);
      return /* @__PURE__ */ import_react.default.createElement(
        import_pisellRow.default,
        {
          className: (0, import_classnames.default)(
            "pisell-checkbox-group-item",
            optionClassName,
            {
              "pisell-checkbox-group-item-active": active,
              "pisell-checkbox-group-item-with-background": active && showActiveBackground,
              [optionActiveClassName]: active,
              "pisell-checkbox-group-item-error": option.error
            }
          ),
          key: option[rowKey],
          gap: 10,
          style: rowStyle,
          onClick: (e) => {
            onItemClick == null ? void 0 : onItemClick(e, option);
            if (option.disabled) return;
            if (onChangeDebounce) {
              run(option[rowKey], option);
              return;
            }
            handleSelectItem(option[rowKey], option);
          },
          leftContent: iconPosition === "left" && /* @__PURE__ */ import_react.default.createElement(
            CheckBoxItem,
            {
              checked: active,
              className: "pisell-checkbox-group-item-checkbox"
            }
          ),
          rightContent: iconPosition === "right" && /* @__PURE__ */ import_react.default.createElement(
            CheckBoxItem,
            {
              checked: active,
              className: "pisell-checkbox-group-item-checkbox"
            }
          ),
          centerContent: renderOption == null ? void 0 : renderOption(option, active, index),
          centerContentFill: true
        }
      );
    })
  );
};
var pisellCheckboxGroup_default = PisellCheckboxGroup;
