function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["platform", "gap", "style", "options", "onChange", "value", "renderOption", "padding", "rowKey", "mode", "direction", "fullWidth", "iconPosition", "optionClassName", "optionActiveClassName", "onChangeDebounce", "onItemClick", "showActiveBackground"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useEffect, useMemo } from 'react';
import usePisellConfig from "../pisell-config-provider/hooks/usePisellConfig";
import classNames from 'classnames';
import { Checkbox, Radio } from 'antd';
import { useDebounceFn } from "ahooks";
import { isNumber } from '@pisell/utils';
import { changeArray } from '@pisell/utils';
import "./index.less";
import { useState } from 'react';
import PisellRow from "../pisellRow";
import { mockList } from "./mock";

// 列表选项

// 组件的Props

/**
 * @title: PisellCheckboxGroup
 * @description: 选中组件
 * @param {PisellCheckboxGroupProps} props
 * @return {*}
 * @Author: zhiwei.Wang
 * @Date: 2024-08-16 10:34
 */
var PisellCheckboxGroup = function PisellCheckboxGroup(props) {
  var config = usePisellConfig();
  var platform = props.platform,
    _props$gap = props.gap,
    gap = _props$gap === void 0 ? 10 : _props$gap,
    style = props.style,
    _props$options = props.options,
    options = _props$options === void 0 ? mockList : _props$options,
    onChange = props.onChange,
    value = props.value,
    _props$renderOption = props.renderOption,
    renderOption = _props$renderOption === void 0 ? function (option) {
      return option.label;
    } : _props$renderOption,
    _props$padding = props.padding,
    padding = _props$padding === void 0 ? 0 : _props$padding,
    _props$rowKey = props.rowKey,
    rowKey = _props$rowKey === void 0 ? 'value' : _props$rowKey,
    _props$mode = props.mode,
    mode = _props$mode === void 0 ? 'multiple' : _props$mode,
    direction = props.direction,
    fullWidth = props.fullWidth,
    _props$iconPosition = props.iconPosition,
    iconPosition = _props$iconPosition === void 0 ? 'left' : _props$iconPosition,
    optionClassName = props.optionClassName,
    optionActiveClassName = props.optionActiveClassName,
    onChangeDebounce = props.onChangeDebounce,
    onItemClick = props.onItemClick,
    _props$showActiveBack = props.showActiveBackground,
    showActiveBackground = _props$showActiveBack === void 0 ? false : _props$showActiveBack,
    rest = _objectWithoutProperties(props, _excluded);
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    _value = _useState2[0],
    set_value = _useState2[1];

  // 当value 发生变化时, 将外部value 处理成内部value
  useEffect(function () {
    initValue();
  }, [value]);

  /**
   * @title: 转换value到内部
   * @description:
   * @return {*}
   * @Author: zhiwei.Wang
   * @Date: 2024-01-30 16:45
   */
  var initValue = function initValue() {
    var _val = value;
    // 没有value 格式化空数组
    if (!value) {
      _val = [];
    } else {
      // 不是数组的话, 转换成数组
      if (!Array.isArray(value)) {
        _val = [value];
      }
    }
    set_value(_val);
  };

  // 样式
  var _style = useMemo(function () {
    // 间隔
    var gapVal = isNumber(gap) ? gap + 'px' : gap;

    // 内边距
    var paddingVal = isNumber(padding) ? padding + 'px' : padding;
    var obj = {
      gap: gapVal,
      padding: paddingVal
    };
    // 方向
    if ('horizontal' === direction) {
      obj = _objectSpread(_objectSpread({}, obj), {}, {
        flexDirection: 'row',
        overflowX: 'auto'
      });
    }
    return _objectSpread(_objectSpread({}, obj), style);
  }, [style, gap, padding, direction]);
  var rowStyle = useMemo(function () {
    if ('horizontal' === direction) {
      if (false === fullWidth) {
        return {
          flexShrink: 0,
          width: 'auto'
        };
      }
      return {
        flexShrink: 0
      };
    }
    return {};
  }, [direction, fullWidth]);

  /**
   * @title: 统一外部onChange
   * @description:
   * @return {*}
   * @Author: zhiwei.Wang
   * @Date: 2024-08-16 10:35
   */
  var _onChange = function _onChange(val, item) {
    // 单选模式, 只返回第一个值
    onChange === null || onChange === void 0 || onChange(mode === 'single' ? val[0] : val, item);
  };
  var _useDebounceFn = useDebounceFn(function (key, item) {
      handleSelectItem(key, item);
    }, {
      wait: 400,
      leading: true,
      trailing: false
    }),
    run = _useDebounceFn.run;

  /**
   * @title: 选择
   * @description:
   * @param {any} item
   * @return {*}
   * @Author: zhiwei.Wang
   * @Date: 2024-01-30 16:46
   */
  var handleSelectItem = function handleSelectItem(key, item) {
    var newVal = [key];

    // 多选进行合并
    if (mode === 'multiple') {
      newVal = changeArray(_value, key, 'this');
    }

    // 返回外部
    _onChange(newVal, item);

    // 设置内部值
    set_value(newVal);
  };

  // 根据mode 切换不同的组件
  var CheckBoxItem = mode === 'multiple' ? Checkbox : Radio;
  return /*#__PURE__*/React.createElement("div", _extends({}, rest, {
    className: classNames('pisell-checkbox-group', rest.className),
    style: _style
  }), options.map(function (option, index) {
    var active = _value.includes(option[rowKey]);
    return /*#__PURE__*/React.createElement(PisellRow, {
      className: classNames('pisell-checkbox-group-item', optionClassName, _defineProperty(_defineProperty({
        '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: function onClick(e) {
        onItemClick === null || onItemClick === void 0 || onItemClick(e, option);
        if (option.disabled) return;
        if (onChangeDebounce) {
          run(option[rowKey], option);
          return;
        }
        handleSelectItem(option[rowKey], option);
      },
      leftContent: iconPosition === 'left' && /*#__PURE__*/React.createElement(CheckBoxItem, {
        checked: active,
        className: "pisell-checkbox-group-item-checkbox"
      }),
      rightContent: iconPosition === 'right' && /*#__PURE__*/React.createElement(CheckBoxItem, {
        checked: active,
        className: "pisell-checkbox-group-item-checkbox"
      }),
      centerContent: renderOption === null || renderOption === void 0 ? void 0 : renderOption(option, active, index),
      centerContentFill: true
    });
  }));
};
export default PisellCheckboxGroup;