const require_runtime = require("../../../../../../_virtual/_rolldown/runtime.js");
const require_index = require("../../../../../iconfont/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
require("./index.less");
//#region src/components/productCard/components/Time/components/SelectHolder/index.tsx
const defaultArr = [];
const SelectHolder = ({ lists = defaultArr, value, onChange, holderMode, isShowAddHolderButton, onAddHolder, addHolderButtonText, onClearHolder, isErrorHolder, allowClear = false, holderMaxCount }) => {
	const [selectedValue, setSelectedValue] = (0, react.useState)();
	const [open, setOpen] = (0, react.useState)(false);
	(0, react.useEffect)(() => {
		setSelectedValue(value);
	}, [value]);
	/**
	* @title: 点击添加holder事件
	* @description:
	* @param {Event} e
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-04-26 10:39
	*/
	const addItem = (e) => {
		e.preventDefault();
		e.stopPropagation();
		onAddHolder();
		setOpen(false);
	};
	const isError = (0, react.useMemo)(() => {
		if (!isErrorHolder) return false;
		if (!selectedValue) return true;
		if ((0, _pisell_utils.isArr)(selectedValue) && (selectedValue === null || selectedValue === void 0 ? void 0 : selectedValue.length) !== holderMaxCount) return true;
		return false;
	}, [selectedValue, isErrorHolder]);
	const handleChange = (val) => {
		let item;
		if ((0, _pisell_utils.isArr)(val)) {
			if (val.length > holderMaxCount) return;
			item = lists.filter((d) => val.includes(d.id));
		} else item = lists.find((d) => d.id === val);
		setSelectedValue(val);
		onChange(item);
	};
	return /* @__PURE__ */ react.default.createElement("div", { className: `pisell-lowcode-product-card-select-holder ${isError ? "pisell-lowcode-product-card-select-holder-error" : ""}` }, /* @__PURE__ */ react.default.createElement(antd.Select, {
		open,
		onDropdownVisibleChange: setOpen,
		onClick: (e) => e.stopPropagation(),
		mode: holderMode,
		size: "large",
		showSearch: true,
		optionFilterProp: "label",
		allowClear,
		onClear: () => onChange(""),
		value: selectedValue,
		placeholder: _pisell_utils.locales.getText("pisell2.product.card.add.holder.placeholder")(addHolderButtonText),
		onChange: handleChange,
		options: lists.map((item) => ({
			label: item === null || item === void 0 ? void 0 : item.label,
			value: item === null || item === void 0 ? void 0 : item.id
		})),
		dropdownRender: (menu) => /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, menu, isShowAddHolderButton ? /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Divider, { style: { margin: "8px 0" } }), /* @__PURE__ */ react.default.createElement("div", {
			onClick: addItem,
			className: "select-holder-add-btn-wrap",
			style: {
				display: "flex",
				alignContent: "center",
				padding: "4px 10px 10px 8px",
				gap: "8px",
				flex: "1 0 0",
				color: "var(--Primary-600, #7f56d9)",
				fontSize: "14px",
				fontWeight: 600,
				lineHeight: "20px",
				cursor: "pointer"
			}
		}, /* @__PURE__ */ react.default.createElement(require_index.default, {
			type: "pisell2-plus-circle",
			style: { fontSize: "20px" }
		}), /* @__PURE__ */ react.default.createElement("span", { style: {
			flex: 1,
			display: "inline-block",
			maxWidth: "100%",
			whiteSpace: "nowrap",
			overflow: "hidden",
			textOverflow: "ellipsis",
			verticalAlign: "middle"
		} }, _pisell_utils.locales.getText("pisell2.product.card.add.holder.button.text")(addHolderButtonText)))) : null)
	}), isError ? /* @__PURE__ */ react.default.createElement("div", { className: "select-holder-error" }, _pisell_utils.locales.getText("pisell2.product.card.add.holder.placeholder")(addHolderButtonText)) : null);
};
//#endregion
exports.default = SelectHolder;
