import { useSales } from "../../../../businessModel/SalesModel/Sales/index.js";
import { useStep } from "../../../Sales/Step/index.js";
import { useCart } from "../../../Sales/Cart/index.js";
import { useHolder } from "../../../Sales/Holder/index.js";
import PisellSelectHolder from "../selectHolder/index.js";
import React, { useEffect, useState } from "react";
import { locales } from "@pisell/utils";
//#region src/components/eventBooking/components/Provider/SelectHolderModal.tsx
const SelectHolderModal = (props) => {
	var _holderData$holderLis;
	const { open } = props;
	const { data } = useSales();
	const { onHolderClose, onOpenCreateHolder, selectHolder, holderData, updateHolderList, defaultSelectedHolder } = useHolder();
	const { nextStep, prevStep, currentStepData } = useStep();
	const [selectedHolder, setSelectedHolder] = useState(defaultSelectedHolder);
	const { onCloseCart } = useCart();
	useEffect(() => {
		setSelectedHolder(defaultSelectedHolder);
	}, [defaultSelectedHolder]);
	/**
	* @title: 点击holder卡片
	* @description:
	* @param {any} e
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-16 22:29
	*/
	const handleHolder = (e) => {
		var _data$initData;
		if (!(e === null || e === void 0 ? void 0 : e.isAddCard)) {
			setSelectedHolder(e);
			return;
		}
		onOpenCreateHolder({
			form_id: (_data$initData = data.initData) === null || _data$initData === void 0 || (_data$initData = _data$initData.boardSetting) === null || _data$initData === void 0 || (_data$initData = _data$initData.config) === null || _data$initData === void 0 || (_data$initData = _data$initData.basic) === null || _data$initData === void 0 || (_data$initData = _data$initData.appointment_subject) === null || _data$initData === void 0 ? void 0 : _data$initData.form_id,
			form_title: locales.getText("pisell2.event.create.form"),
			callback: (res) => {
				updateHolderList(res === null || res === void 0 ? void 0 : res.values);
			}
		});
	};
	return /* @__PURE__ */ React.createElement(PisellSelectHolder, {
		open,
		options: (holderData === null || holderData === void 0 ? void 0 : holderData.holderList) || [],
		onCancel: () => onHolderClose(),
		value: selectedHolder === null || selectedHolder === void 0 ? void 0 : selectedHolder.id,
		title: locales.getText("pisell2.event.holder.modal.title"),
		okText: locales.getText("pisell2.event.confirm"),
		onClick: (e) => handleHolder(e),
		disabled: !(holderData === null || holderData === void 0 || (_holderData$holderLis = holderData.holderList) === null || _holderData$holderLis === void 0 ? void 0 : _holderData$holderLis.length) || !(selectedHolder === null || selectedHolder === void 0 ? void 0 : selectedHolder.id),
		errorTipText: locales.getText("pisell2.event.error.holder"),
		onOk: () => {
			onCloseCart();
			const isEvent = (currentStepData === null || currentStepData === void 0 ? void 0 : currentStepData.key) === "select-event";
			selectHolder(selectedHolder, isEvent ? "addToCart" : "createCartItem");
			if (isEvent) nextStep();
			else prevStep();
			onHolderClose();
		}
	});
};
//#endregion
export { SelectHolderModal as default };
