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); }
/**
 * PisellMultipleSelect - 多选下拉组件
 * 基于 Ant Design Select 封装，提供符合 Pisell 设计规范的多选功能
 */

import React, { forwardRef, useRef, useImperativeHandle, useCallback } from 'react';
import { message } from 'antd';
import { useMultipleSelectState } from '@pisell/utils';
import EditView from "./components/EditView";
import DisabledView from "./components/DisabledView";
import ReadOnlyView from "./components/ReadOnlyView";
import "./PisellMultipleSelect.less";
var PisellMultipleSelect = /*#__PURE__*/forwardRef(function (props, ref) {
  var value = props.value,
    defaultValue = props.defaultValue,
    onChange = props.onChange,
    options = props.options,
    disabled = props.disabled,
    readOnly = props.readOnly,
    maxCount = props.maxCount;

  // Select 内部 ref
  var selectRef = useRef(null);

  // 状态管理（从 @pisell/utils 导入）
  var state = useMultipleSelectState({
    value: value,
    defaultValue: defaultValue,
    onChange: onChange,
    mode: 'multiple'
  });

  // 处理 maxCount 限制
  var handleChange = useCallback(function (newValue) {
    if (maxCount && newValue.length > maxCount) {
      message.warning("\u6700\u591A\u53EA\u80FD\u9009\u62E9 ".concat(maxCount, " \u9879"));
      return;
    }
    var selectedOptions = newValue.map(function (v) {
      return options.find(function (opt) {
        return opt.value === v;
      });
    }).filter(Boolean);

    // 更新内部状态（非受控模式需要）
    state.setValue(newValue);

    // 触发外部回调
    onChange === null || onChange === void 0 || onChange(newValue, selectedOptions);
  }, [maxCount, onChange, options, state]);

  // 暴露 Ref 方法
  useImperativeHandle(ref, function () {
    return {
      focus: function focus() {
        var _selectRef$current;
        return (_selectRef$current = selectRef.current) === null || _selectRef$current === void 0 ? void 0 : _selectRef$current.focus();
      },
      blur: function blur() {
        var _selectRef$current2;
        return (_selectRef$current2 = selectRef.current) === null || _selectRef$current2 === void 0 ? void 0 : _selectRef$current2.blur();
      },
      getValue: function getValue() {
        return state.value;
      },
      setValue: function setValue(newValue) {
        return state.setValue(newValue);
      },
      clearValue: function clearValue() {
        return state.clearValue();
      },
      addValue: function addValue(val) {
        return state.addValue(val);
      },
      removeValue: function removeValue(val) {
        return state.removeValue(val);
      }
    };
  });

  // 根据不同状态渲染不同视图
  if (disabled) {
    return /*#__PURE__*/React.createElement(DisabledView, _extends({}, props, {
      ref: selectRef
    }));
  }
  if (readOnly) {
    return /*#__PURE__*/React.createElement(ReadOnlyView, props);
  }
  return /*#__PURE__*/React.createElement(EditView, _extends({}, props, {
    value: state.value,
    state: state,
    onChangeInternal: handleChange,
    ref: selectRef
  }));
});
PisellMultipleSelect.displayName = 'PisellMultipleSelect';
export default PisellMultipleSelect;