const require_runtime = require("../../../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
let _pisell_icon_es_AlertTriangle = require("@pisell/icon/es/AlertTriangle");
_pisell_icon_es_AlertTriangle = require_runtime.__toESM(_pisell_icon_es_AlertTriangle);
require("./index.less");
//#region src/components/productCard/components/Time/components/SelectResource/index.tsx
const _excluded = [
	"lists",
	"value",
	"onChange",
	"resourceMode",
	"allowClear",
	"onClearResource",
	"maxLength",
	"isErrorResource",
	"resourceErrorText"
];
const SelectResource = (_ref) => {
	let { lists, value, onChange, resourceMode, allowClear = false, onClearResource, maxLength, isErrorResource, resourceErrorText } = _ref, props = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
	const [selectedValue, setSelectedValue] = (0, react.useState)();
	(0, react.useEffect)(() => {
		setSelectedValue(value);
	}, [value]);
	const resourceMap = (0, react.useMemo)(() => {
		return (lists || []).reduce((acc, item) => {
			acc[item.id] = item;
			return acc;
		}, {});
	}, [lists]);
	/**
	* @title: 渲染下拉框
	* @description:
	* @param {*} useCallback
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-04-11 14:57
	*/
	const renderOptions = (0, react.useCallback)(() => {
		return (lists || []).map((d) => {
			return /* @__PURE__ */ react.default.createElement(antd.Select.Option, require_objectSpread2._objectSpread2({
				value: d.id,
				key: d.id
			}, d || {}), /* @__PURE__ */ react.default.createElement("div", { style: {
				display: "flex",
				alignItems: "center",
				gap: "8px"
			} }, /* @__PURE__ */ react.default.createElement("span", null, d["label"]), (d === null || d === void 0 ? void 0 : d.isError) ? /* @__PURE__ */ react.default.createElement(_pisell_icon_es_AlertTriangle.default, { style: {
				color: "#b42318",
				cursor: "pointer"
			} }) : null));
		});
	}, [lists]);
	const handleChange = (0, react.useCallback)((val) => {
		if (["multiple", "tags"].includes(resourceMode) && maxLength) {
			if (value && value.length >= maxLength && val.length > value.length) return;
		}
		setSelectedValue(val);
		if (props.isParallelResource) {
			const valueArray = (0, _pisell_utils.isArr)(val) ? val : [val];
			return onChange({
				value: valueArray,
				options: valueArray.map((v) => resourceMap[v]),
				resourceMode,
				props
			});
		}
		const item = resourceMap[val];
		onChange(item);
	}, [
		resourceMode,
		maxLength,
		value,
		onChange,
		props,
		resourceMap
	]);
	const isError = (0, react.useMemo)(() => {
		if (!selectedValue && isErrorResource) return true;
	}, [selectedValue, isErrorResource]);
	return /* @__PURE__ */ react.default.createElement("div", { className: `pisell-lowcode-product-card-select-resource ${isError ? "pisell-lowcode-product-card-select-resource-error" : ""}` }, /* @__PURE__ */ react.default.createElement(antd.Select, {
		disabled: props.disabled,
		mode: resourceMode,
		size: "large",
		showSearch: true,
		allowClear,
		onClear: () => onChange(""),
		onClick: (e) => e.stopPropagation(),
		style: {
			flex: 1,
			overflow: "hidden"
		},
		value: selectedValue,
		onChange: handleChange,
		placeholder: _pisell_utils.locales.getText("pisell2.product.card.add.holder.placeholder")("")
	}, renderOptions()), isError ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode-product-select-resource-error" }, _pisell_utils.locales.getText("pisell2.product.card.add.holder.placeholder")(resourceErrorText)) : null);
};
//#endregion
exports.default = SelectResource;
