import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import PisellSingleSelect from "../../../pisellSingleSelect/PisellSingleSelect.js";
import "../../../pisellSingleSelect/index.js";
import PisellMultipleSelect from "../../../pisellMultipleSelect/PisellMultipleSelect.js";
import "../../../pisellMultipleSelect/index.js";
import React, { useEffect, useState } from "react";
import { Spin } from "antd";
//#region src/components/filter/components/FilterItem/AsyncOptionsSelectWrapper.tsx
/**
* 筛选项中 singleSelect/multipleSelect 的异步 options 包装器。
* 当列配置传入 getOptions（同步或异步）时，先解析再渲染 PisellSingleSelect/PisellMultipleSelect。
*/
const _excluded = [
	"getOptions",
	"fieldType",
	"value",
	"onChange"
];
function normalizeOptions(list) {
	if (!Array.isArray(list)) return [];
	return list.map((item) => item && typeof item === "object" && "label" in item && "value" in item ? {
		label: String(item.label),
		value: item.value
	} : {
		label: String(item),
		value: item
	});
}
const AsyncOptionsSelectWrapper = (props) => {
	const { getOptions, fieldType, value, onChange } = props, rest = _objectWithoutProperties(props, _excluded);
	const [options, setOptions] = useState([]);
	const [loading, setLoading] = useState(true);
	useEffect(() => {
		setLoading(true);
		const result = getOptions();
		Promise.resolve(result).then((list) => {
			setOptions(normalizeOptions(list));
		}).finally(() => {
			setLoading(false);
		});
	}, [getOptions]);
	const commonProps = _objectSpread2(_objectSpread2({}, rest), {}, {
		options,
		mode: "edit"
	});
	if (loading) return /* @__PURE__ */ React.createElement(Spin, {
		spinning: true,
		size: "small"
	}, /* @__PURE__ */ React.createElement("span", { style: {
		display: "inline-block",
		minWidth: 120,
		minHeight: 32
	} }));
	if (fieldType === "multipleSelect") return /* @__PURE__ */ React.createElement(PisellMultipleSelect, _objectSpread2(_objectSpread2({}, commonProps), {}, {
		value,
		onChange
	}));
	return /* @__PURE__ */ React.createElement(PisellSingleSelect, _objectSpread2(_objectSpread2({}, commonProps), {}, {
		value,
		onChange
	}));
};
AsyncOptionsSelectWrapper.displayName = "AsyncOptionsSelectWrapper";
//#endregion
export { AsyncOptionsSelectWrapper as default };
