import IconFont from "../../../../../iconfont/index.js";
import React, { useEffect, useMemo, useState } from "react";
import { isArr, locales } from "@pisell/utils";
import { Divider, Select } from "antd";
import "./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] = useState();
	const [open, setOpen] = useState(false);
	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 = useMemo(() => {
		if (!isErrorHolder) return false;
		if (!selectedValue) return true;
		if (isArr(selectedValue) && (selectedValue === null || selectedValue === void 0 ? void 0 : selectedValue.length) !== holderMaxCount) return true;
		return false;
	}, [selectedValue, isErrorHolder]);
	const handleChange = (val) => {
		let item;
		if (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.createElement("div", { className: `pisell-lowcode-product-card-select-holder ${isError ? "pisell-lowcode-product-card-select-holder-error" : ""}` }, /* @__PURE__ */ React.createElement(Select, {
		open,
		onDropdownVisibleChange: setOpen,
		onClick: (e) => e.stopPropagation(),
		mode: holderMode,
		size: "large",
		showSearch: true,
		optionFilterProp: "label",
		allowClear,
		onClear: () => onChange(""),
		value: selectedValue,
		placeholder: 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.createElement(React.Fragment, null, menu, isShowAddHolderButton ? /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Divider, { style: { margin: "8px 0" } }), /* @__PURE__ */ React.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.createElement(IconFont, {
			type: "pisell2-plus-circle",
			style: { fontSize: "20px" }
		}), /* @__PURE__ */ React.createElement("span", { style: {
			flex: 1,
			display: "inline-block",
			maxWidth: "100%",
			whiteSpace: "nowrap",
			overflow: "hidden",
			textOverflow: "ellipsis",
			verticalAlign: "middle"
		} }, locales.getText("pisell2.product.card.add.holder.button.text")(addHolderButtonText)))) : null)
	}), isError ? /* @__PURE__ */ React.createElement("div", { className: "select-holder-error" }, locales.getText("pisell2.product.card.add.holder.placeholder")(addHolderButtonText)) : null);
};
//#endregion
export { SelectHolder as default };
