const require_runtime = require("../../../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_index = require("../../../../../../plus/selectHolder/components/HolderItem/index.js");
const require_index$1 = require("../../../../../../plus/selectHolder/components/HolderList/index.js");
const require_selectHolderModal = require("../../../../utils/selectHolderModal.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/components/booking/info2/service/editService/Holder/index.tsx
const LinkButton = antd.Button;
const normalizeValue = (val) => {
	if (Array.isArray(val)) return val.filter((item) => item !== void 0 && item !== null);
	if (val === void 0 || val === null || val === "") return [];
	return [val];
};
const Holder = ({ state, cacheItem, value, onChange, dispatch }) => {
	var _state$bookingConfig, _holderConfig$form, _state$bookingConfig2, _cacheItem$_extend, _state$pet4, _state$pet5, _state$bookingConfig4;
	const holderConfig = (state === null || state === void 0 || (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 ? void 0 : _state$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 isRequired = (state === null || state === void 0 || (_state$bookingConfig2 = state.bookingConfig) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.config) === null || _state$bookingConfig2 === void 0 || (_state$bookingConfig2 = _state$bookingConfig2.basic) === null || _state$bookingConfig2 === void 0 ? void 0 : _state$bookingConfig2.subject_require) === "require";
	const maxSelectedCount = (cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend = cacheItem._extend) === null || _cacheItem$_extend === void 0 ? void 0 : _cacheItem$_extend.quantity) || 1;
	const selectType = maxSelectedCount > 1 ? "multiple" : "single";
	const [selectedValues, setSelectedValues] = (0, react.useState)(() => normalizeValue(value));
	(0, react.useEffect)(() => {
		setSelectedValues(normalizeValue(value));
	}, [value]);
	const holderOptions = (0, react.useMemo)(() => {
		var _state$pet, _state$bookingConfig3, _state$pet2, _state$pet3;
		const holdersFromState = Array.isArray(state === null || state === void 0 || (_state$pet = state.pet) === null || _state$pet === void 0 ? void 0 : _state$pet.holders) ? state.pet.holders : [];
		const formId = (state === null || state === void 0 || (_state$bookingConfig3 = state.bookingConfig) === null || _state$bookingConfig3 === void 0 || (_state$bookingConfig3 = _state$bookingConfig3.config) === null || _state$bookingConfig3 === void 0 || (_state$bookingConfig3 = _state$bookingConfig3.basic) === null || _state$bookingConfig3 === void 0 || (_state$bookingConfig3 = _state$bookingConfig3.appointment_subject) === null || _state$bookingConfig3 === void 0 ? void 0 : _state$bookingConfig3.form_id) || 0;
		const holdersFromValue = Array.isArray(state === null || state === void 0 || (_state$pet2 = state.pet) === null || _state$pet2 === void 0 || (_state$pet2 = _state$pet2.value) === null || _state$pet2 === void 0 ? void 0 : _state$pet2[formId]) ? state === null || state === void 0 || (_state$pet3 = state.pet) === null || _state$pet3 === void 0 || (_state$pet3 = _state$pet3.value) === null || _state$pet3 === void 0 ? void 0 : _state$pet3[formId] : [];
		return (holdersFromState.length > 0 ? holdersFromState : holdersFromValue).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);
	}, [
		state === null || state === void 0 || (_state$pet4 = state.pet) === null || _state$pet4 === void 0 ? void 0 : _state$pet4.holders,
		state === null || state === void 0 || (_state$pet5 = state.pet) === null || _state$pet5 === void 0 ? void 0 : _state$pet5.value,
		state === null || state === void 0 || (_state$bookingConfig4 = state.bookingConfig) === null || _state$bookingConfig4 === void 0 ? void 0 : _state$bookingConfig4.config
	]);
	const displayConfig = (0, react.useMemo)(() => ({
		direction: "vertical",
		selectType,
		showAvatar: true,
		showTitle: true,
		showSubtitle: Boolean(holderOptions.some((option) => option.subtitle))
	}), [holderOptions, selectType]);
	const selectedSummary = (0, react.useMemo)(() => {
		if (!maxSelectedCount) return "";
		const label = _pisell_utils.locales.getText("pisell-select-holder-selected").replace(/[:：]\s*$/, "");
		return `${selectedValues.length}/${maxSelectedCount} ${label}`;
	}, [selectedValues.length, maxSelectedCount]);
	const handleApply = (0, react.useCallback)((nextValues) => {
		onChange === null || onChange === void 0 || onChange(nextValues);
	}, [onChange, selectType]);
	const handleEdit = (0, react.useCallback)(function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (event) {
			var _state$bookingConfig5, _cacheItem$_extend2;
			event === null || event === void 0 || event.preventDefault();
			const holderConfig = (state === null || state === void 0 || (_state$bookingConfig5 = state.bookingConfig) === null || _state$bookingConfig5 === void 0 ? void 0 : _state$bookingConfig5.holder) || {};
			const currentMaxSelectedCount = (holderConfig === null || holderConfig === void 0 ? void 0 : holderConfig.maxSelectedCount) || (cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend2 = cacheItem._extend) === null || _cacheItem$_extend2 === void 0 ? void 0 : _cacheItem$_extend2.quantity) || 1;
			const result = yield require_selectHolderModal.selectHolderModal({
				state,
				dispatch,
				initialValue: selectedValues.length > 0 ? selectedValues : void 0,
				maxSelectedCount: currentMaxSelectedCount
			});
			if (result && result.length > 0) {
				setSelectedValues(result);
				handleApply(result);
			}
		});
		return function(_x) {
			return _ref.apply(this, arguments);
		};
	}(), [
		selectedValues,
		state,
		dispatch,
		handleApply
	]);
	const handleClear = (0, react.useCallback)((event) => {
		event === null || event === void 0 || event.preventDefault();
		setSelectedValues([]);
		handleApply([]);
	}, [handleApply]);
	const clearDisabled = (0, react.useMemo)(() => {
		return (selectedValues === null || selectedValues === void 0 ? void 0 : selectedValues.length) === 0;
	}, [selectedValues]);
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(antd.Col, { span: 24 }, /* @__PURE__ */ react.default.createElement("div", { className: "booking-holder-field" }, /* @__PURE__ */ react.default.createElement("div", { className: "booking-holder-field__header" }, /* @__PURE__ */ react.default.createElement("div", { className: "booking-holder-field__title" }, isRequired && /* @__PURE__ */ react.default.createElement("span", { className: "booking-holder-field__required" }, "*"), /* @__PURE__ */ react.default.createElement("span", null, title), maxSelectedCount ? /* @__PURE__ */ react.default.createElement("span", { className: "booking-holder-field__count" }, selectedSummary) : null), /* @__PURE__ */ react.default.createElement("div", { className: "booking-holder-field__actions" }, /* @__PURE__ */ react.default.createElement(LinkButton, {
		type: "link",
		size: "small",
		className: "booking-holder-field__action-button",
		onClick: handleEdit
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, { type: "pisell2-edit-05" }), /* @__PURE__ */ react.default.createElement("span", null, _pisell_utils.locales.getText("pisell2.text.edit"))), /* @__PURE__ */ react.default.createElement(LinkButton, {
		type: "link",
		size: "small",
		disabled: clearDisabled,
		className: "booking-holder-field__action-button",
		onClick: handleClear
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, { type: "pisell2-delete_wght300_24px" }), /* @__PURE__ */ react.default.createElement("span", null, _pisell_utils.locales.getText("pisell2.booking.info.text.clear"))))), selectedValues.length === 0 ? /* @__PURE__ */ react.default.createElement(antd.Row, null, /* @__PURE__ */ react.default.createElement(antd.Col, {
		span: 8,
		xs: 24,
		sm: 12,
		md: 8,
		lg: 8,
		onClick: handleEdit
	}, /* @__PURE__ */ react.default.createElement(require_index.default, {
		item: {
			label: _pisell_utils.locales.getText("pisell2.booking.info.text.add-holder"),
			value: null
		},
		isAddItem: true
	}))) : /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		dataSource: selectedValues.map((item) => {
			const option = holderOptions.find((opt) => opt.value === item);
			return {
				label: (option === null || option === void 0 ? void 0 : option.label) || "",
				value: item
			};
		}),
		selectedValues,
		displayConfig,
		allowAdd: false,
		onClick: () => handleEdit()
	}))), /* @__PURE__ */ react.default.createElement(require_selectHolderModal.SelectHolderModal, null));
};
//#endregion
exports.default = Holder;
