import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { getPisellComponentsThemeConfig } from "../../theme/pisell-components.js";
import EditView from "./components/EditView.js";
import DisabledView from "./components/DisabledView.js";
import ReadOnlyView from "./components/ReadOnlyView.js";
import React, { forwardRef, useCallback, useImperativeHandle, useRef } from "react";
import { useMultipleSelectState } from "@pisell/utils";
import { ConfigProvider, message } from "antd";
import "./PisellMultipleSelect.less";
//#region src/components/pisellMultipleSelect/PisellMultipleSelect.tsx
/**
* PisellMultipleSelect - 多选下拉组件
* 基于 Ant Design Select 封装，提供符合 Pisell 设计规范的多选功能
*/
const PisellMultipleSelect = forwardRef((props, ref) => {
	const { value, defaultValue, onChange, options, disabled, readOnly, maxCount } = props;
	const selectRef = useRef(null);
	const state = useMultipleSelectState({
		value,
		defaultValue,
		onChange,
		mode: "multiple"
	});
	const handleChange = useCallback((newValue) => {
		if (maxCount && newValue.length > maxCount) {
			message.warning(`最多只能选择 ${maxCount} 项`);
			return;
		}
		const selectedOptions = newValue.map((v) => options.find((opt) => opt.value === v)).filter(Boolean);
		state.setValue(newValue);
		onChange === null || onChange === void 0 || onChange(newValue, selectedOptions);
	}, [
		maxCount,
		onChange,
		options,
		state
	]);
	useImperativeHandle(ref, () => ({
		focus: () => {
			var _selectRef$current;
			return (_selectRef$current = selectRef.current) === null || _selectRef$current === void 0 ? void 0 : _selectRef$current.focus();
		},
		blur: () => {
			var _selectRef$current2;
			return (_selectRef$current2 = selectRef.current) === null || _selectRef$current2 === void 0 ? void 0 : _selectRef$current2.blur();
		},
		getValue: () => state.value,
		setValue: (newValue) => state.setValue(newValue),
		clearValue: () => state.clearValue(),
		addValue: (val) => state.addValue(val),
		removeValue: (val) => state.removeValue(val)
	}));
	const theme = getPisellComponentsThemeConfig();
	if (disabled) return /* @__PURE__ */ React.createElement(ConfigProvider, { theme }, /* @__PURE__ */ React.createElement(DisabledView, _objectSpread2(_objectSpread2({}, props), {}, { ref: selectRef })));
	if (readOnly) return /* @__PURE__ */ React.createElement(ConfigProvider, { theme }, /* @__PURE__ */ React.createElement(ReadOnlyView, props));
	return /* @__PURE__ */ React.createElement(ConfigProvider, { theme }, /* @__PURE__ */ React.createElement(EditView, _objectSpread2(_objectSpread2({}, props), {}, {
		value: state.value,
		state,
		onChangeInternal: handleChange,
		ref: selectRef
	})));
});
PisellMultipleSelect.displayName = "PisellMultipleSelect";
//#endregion
export { PisellMultipleSelect as default };
