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 React, { useCallback, useEffect, useMemo, useState } from "react";
import { isArr, locales } from "@pisell/utils";
import { Select } from "antd";
import AlertTriangle from "@pisell/icon/es/AlertTriangle";
import "./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 = _objectWithoutProperties(_ref, _excluded);
	const [selectedValue, setSelectedValue] = useState();
	useEffect(() => {
		setSelectedValue(value);
	}, [value]);
	const resourceMap = 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 = useCallback(() => {
		return (lists || []).map((d) => {
			return /* @__PURE__ */ React.createElement(Select.Option, _objectSpread2({
				value: d.id,
				key: d.id
			}, d || {}), /* @__PURE__ */ React.createElement("div", { style: {
				display: "flex",
				alignItems: "center",
				gap: "8px"
			} }, /* @__PURE__ */ React.createElement("span", null, d["label"]), (d === null || d === void 0 ? void 0 : d.isError) ? /* @__PURE__ */ React.createElement(AlertTriangle, { style: {
				color: "#b42318",
				cursor: "pointer"
			} }) : null));
		});
	}, [lists]);
	const handleChange = 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 = 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 = useMemo(() => {
		if (!selectedValue && isErrorResource) return true;
	}, [selectedValue, isErrorResource]);
	return /* @__PURE__ */ React.createElement("div", { className: `pisell-lowcode-product-card-select-resource ${isError ? "pisell-lowcode-product-card-select-resource-error" : ""}` }, /* @__PURE__ */ React.createElement(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: locales.getText("pisell2.product.card.add.holder.placeholder")("")
	}, renderOptions()), isError ? /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode-product-select-resource-error" }, locales.getText("pisell2.product.card.add.holder.placeholder")(resourceErrorText)) : null);
};
//#endregion
export { SelectResource as default };
