const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_utils = require("../../../components/booking/utils.js");
const require_index = require("../../selectHolder/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
//#region src/plus/salesSdk/components/SalesSdkSelectHolderModal.tsx
function buildHolderOptions(holders) {
	return holders.map((item) => {
		const id = (item === null || item === void 0 ? void 0 : item.id) || (item === null || item === void 0 ? void 0 : item.form_record_id) || (item === null || item === void 0 ? void 0 : item.value);
		if (id === void 0 || id === null) return null;
		return {
			label: (item === null || item === void 0 ? void 0 : item.main_field) || (item === null || item === void 0 ? void 0 : item.label) || (item === null || item === void 0 ? void 0 : item.name) || _pisell_utils.locales.getText("pisell-select-holder-please-select"),
			value: id,
			avatar: item === null || item === void 0 ? void 0 : item.avatar,
			subtitle: (item === null || item === void 0 ? void 0 : item.subtitle) || (item === null || item === void 0 ? void 0 : item.second_field) || (item === null || item === void 0 ? void 0 : item.sub_field)
		};
	}).filter(Boolean);
}
function buildDisplayConfig(bookingConfig, holderOptions, maxSelectedCount) {
	var _bookingConfig$config;
	return {
		direction: "vertical",
		selectType: maxSelectedCount > 1 ? "multiple" : "single",
		showAvatar: true,
		showTitle: true,
		showSubtitle: Boolean(holderOptions.some((option) => option.subtitle)),
		isSkipable: (bookingConfig === null || bookingConfig === void 0 || (_bookingConfig$config = bookingConfig.config) === null || _bookingConfig$config === void 0 || (_bookingConfig$config = _bookingConfig$config.basic) === null || _bookingConfig$config === void 0 ? void 0 : _bookingConfig$config.subject_require) === "optional"
	};
}
/**
* SalesSdk 专用 Holder 弹窗（从 booking selectHolderModal 复制，props 驱动，无 miniRedux dispatch）。
*/
const SalesSdkSelectHolderModal = ({ open, holders, bookingConfig, customerId, initialValue, maxSelectedCount = 1, onAddHolder, onConfirm, onCancel, onSkip }) => {
	var _holderConfig$form;
	const [innerOpen, setInnerOpen] = (0, react.useState)(open);
	const holderOptions = (0, react.useMemo)(() => buildHolderOptions(holders), [holders]);
	const [selectedValues, setSelectedValues] = (0, react.useState)(() => {
		if (Array.isArray(initialValue)) return initialValue.filter((item) => item !== void 0 && item !== null);
		if (initialValue === void 0 || initialValue === null || initialValue === "") return [];
		return [initialValue];
	});
	(0, react.useEffect)(() => {
		setInnerOpen(open);
	}, [open]);
	const holderConfig = (bookingConfig === null || bookingConfig === void 0 ? void 0 : bookingConfig.holder) || {};
	const title = (holderConfig === null || holderConfig === void 0 || (_holderConfig$form = holderConfig.form) === null || _holderConfig$form === void 0 ? void 0 : _holderConfig$form.title) || (holderConfig === null || holderConfig === void 0 ? void 0 : holderConfig.title) || _pisell_utils.locales.getText("pisell2.text.holder");
	const displayConfig = (0, react.useMemo)(() => buildDisplayConfig(bookingConfig, holderOptions, maxSelectedCount), [
		bookingConfig,
		holderOptions,
		maxSelectedCount
	]);
	const closeWith = (fn) => {
		setInnerOpen(false);
		setTimeout(fn, 300);
	};
	const handleClose = () => closeWith(onCancel);
	const handleConfirm = (nextValues) => {
		closeWith(() => {
			if (nextValues === null || nextValues === void 0 ? void 0 : nextValues.length) onConfirm(nextValues.map((val) => Number(val)));
			else onCancel();
		});
	};
	const handleSkip = () => {
		if (onSkip) closeWith(onSkip);
		else handleClose();
	};
	const allowAdd = Boolean(onAddHolder) && !require_utils.isWalkIn(customerId);
	return /* @__PURE__ */ react.default.createElement(require_index.default, {
		open: innerOpen,
		modalConfig: {
			title: `${_pisell_utils.locales.getText("pisell-select-holder-please-select")} ${maxSelectedCount || 1} ${title}`,
			confirmText: _pisell_utils.locales.getText("pisell2.text.confirm")
		},
		dataSource: holderOptions,
		value: selectedValues,
		displayConfig,
		maxSelectedCount,
		allowAdd,
		onClose: handleClose,
		onConfirm: handleConfirm,
		onAdd: onAddHolder,
		onSkip: handleSkip
	});
};
//#endregion
exports.SalesSdkSelectHolderModal = SalesSdkSelectHolderModal;
