const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_utils = require("../../../components/booking/utils.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_buildBookingEditDrawerState = require("../bookingEditService/buildBookingEditDrawerState.js");
const require_useSalesSdk = require("../hooks/useSalesSdk.js");
const require_useBookingEditPetState = require("../bookingEditService/useBookingEditPetState.js");
const require_SalesSdkSelectHolderModal = require("../components/SalesSdkSelectHolderModal.js");
const require_holderHostController = require("../addProductFlow/holderHostController.js");
let react = require("react");
react = require_runtime.__toESM(react);
let ahooks = require("ahooks");
//#region src/plus/salesSdk/hosts/HolderHostRenderer.tsx
/**
* 承接 `openHolderSelect` Promise Host 的 Holder 弹窗渲染（无 miniRedux）。
*/
const HolderHostRenderer = () => {
	var _context$appHelper, _payload$customerId, _cacheItem$_extend, _bookingTicket$getOrd, _mergedState$bookingC, _cacheItem$_extend3, _mergedState$bookingC3, _mergedState$pet4;
	const pending = (0, react.useSyncExternalStore)(require_holderHostController.subscribeHolderHost, require_holderHostController.getHolderHostPending, () => null);
	const { bookingTicket, tempOrder } = require_useSalesSdk.useSalesSdk();
	const context = require_useEngineContext.default();
	const action = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.action;
	const open = !!pending;
	const payload = pending === null || pending === void 0 ? void 0 : pending.payload;
	const cacheItem = payload === null || payload === void 0 ? void 0 : payload.cacheItem;
	const customerId = require_buildBookingEditDrawerState.resolveBookingEditCustomerId(tempOrder, (_payload$customerId = payload === null || payload === void 0 ? void 0 : payload.customerId) !== null && _payload$customerId !== void 0 ? _payload$customerId : cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend = cacheItem._extend) === null || _cacheItem$_extend === void 0 ? void 0 : _cacheItem$_extend.customer_id, bookingTicket === null || bookingTicket === void 0 || (_bookingTicket$getOrd = bookingTicket.getOrderCustomerSnapshot) === null || _bookingTicket$getOrd === void 0 || (_bookingTicket$getOrd = _bookingTicket$getOrd.call(bookingTicket)) === null || _bookingTicket$getOrd === void 0 ? void 0 : _bookingTicket$getOrd.id);
	const bookingState = (0, react.useMemo)(() => {
		var _bookingTicket$getBoo, _bookingTicket$getBoo2, _ref, _bookingTicket$getBoo3, _bookingTicket$getBoo4, _cacheItem$_extend2, _cacheItem$_extend2$f;
		if (!cacheItem || !bookingTicket) return null;
		const bookingConfig = (_bookingTicket$getBoo = (_bookingTicket$getBoo2 = bookingTicket.getBookingConfig) === null || _bookingTicket$getBoo2 === void 0 ? void 0 : _bookingTicket$getBoo2.call(bookingTicket)) !== null && _bookingTicket$getBoo !== void 0 ? _bookingTicket$getBoo : null;
		const date = (_ref = (_bookingTicket$getBoo3 = (_bookingTicket$getBoo4 = bookingTicket.getBookingDate) === null || _bookingTicket$getBoo4 === void 0 ? void 0 : _bookingTicket$getBoo4.call(bookingTicket)) !== null && _bookingTicket$getBoo3 !== void 0 ? _bookingTicket$getBoo3 : cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend2 = cacheItem._extend) === null || _cacheItem$_extend2 === void 0 || (_cacheItem$_extend2 = _cacheItem$_extend2.start_date) === null || _cacheItem$_extend2 === void 0 || (_cacheItem$_extend2$f = _cacheItem$_extend2.format) === null || _cacheItem$_extend2$f === void 0 ? void 0 : _cacheItem$_extend2$f.call(_cacheItem$_extend2, "YYYY-MM-DD")) !== null && _ref !== void 0 ? _ref : void 0;
		return require_buildBookingEditDrawerState.buildBookingEditDrawerState({
			cacheItem,
			bookingConfig,
			date: typeof date === "string" ? date : void 0,
			customerId,
			locale: void 0,
			resourcesOrigin: []
		});
	}, [
		cacheItem,
		bookingTicket,
		customerId
	]);
	const { pet, dispatch } = require_useBookingEditPetState.useBookingEditPetState(open, bookingState !== null && bookingState !== void 0 ? bookingState : {});
	const mergedState = (0, react.useMemo)(() => {
		if (!bookingState) return null;
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, bookingState), {}, { pet: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, bookingState.pet), pet) });
	}, [bookingState, pet]);
	const holderConfig = (mergedState === null || mergedState === void 0 || (_mergedState$bookingC = mergedState.bookingConfig) === null || _mergedState$bookingC === void 0 ? void 0 : _mergedState$bookingC.holder) || {};
	const maxSelectedCount = (payload === null || payload === void 0 ? void 0 : payload.maxSelectedCount) || (holderConfig === null || holderConfig === void 0 ? void 0 : holderConfig.maxSelectedCount) || (cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend3 = cacheItem._extend) === null || _cacheItem$_extend3 === void 0 ? void 0 : _cacheItem$_extend3.quantity) || 1;
	const { formDataConfig, isMinorAndWaiver } = (0, react.useMemo)(() => {
		var _mergedState$bookingC2, _holder$form, _holder$form2;
		const holder = mergedState === null || mergedState === void 0 || (_mergedState$bookingC2 = mergedState.bookingConfig) === null || _mergedState$bookingC2 === void 0 ? void 0 : _mergedState$bookingC2.holder;
		if (!holder) return {
			formDataConfig: null,
			isMinorAndWaiver: false
		};
		const formDataConfig = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, holder), holder === null || holder === void 0 ? void 0 : holder.form), {}, {
			options: [],
			form_id: holder === null || holder === void 0 || (_holder$form = holder.form) === null || _holder$form === void 0 ? void 0 : _holder$form.id,
			field: holder === null || holder === void 0 || (_holder$form2 = holder.form) === null || _holder$form2 === void 0 ? void 0 : _holder$form2.form_field,
			relation_type: "appointment_booking",
			form_record: []
		});
		let isMinorAndWaiver = false;
		if ((formDataConfig === null || formDataConfig === void 0 ? void 0 : formDataConfig.form_type) === "minors") {
			var _formDataConfig$form_;
			const detail = formDataConfig === null || formDataConfig === void 0 || (_formDataConfig$form_ = formDataConfig.form_field) === null || _formDataConfig$form_ === void 0 ? void 0 : _formDataConfig$form_.find((item) => item.filed_type === "form_to_form" && item.status === "hide");
			if (detail) {
				var _detail$setting;
				isMinorAndWaiver = detail === null || detail === void 0 || (_detail$setting = detail.setting) === null || _detail$setting === void 0 ? void 0 : _detail$setting.linkedTable;
			}
		}
		return {
			formDataConfig,
			isMinorAndWaiver
		};
	}, [mergedState === null || mergedState === void 0 || (_mergedState$bookingC3 = mergedState.bookingConfig) === null || _mergedState$bookingC3 === void 0 ? void 0 : _mergedState$bookingC3.holder]);
	const handleAddHolder = (0, ahooks.useMemoizedFn)(() => {
		var _mergedState$pet, _mergedState$client, _mergedState$client2;
		if (!action || !mergedState) return;
		const waiverForm = (_mergedState$pet = mergedState.pet) === null || _mergedState$pet === void 0 ? void 0 : _mergedState$pet.waiverForm;
		action({
			type: "pisell1.goCustomFormAddData",
			data: {
				zIndex: 1101,
				customerId: (_mergedState$client = mergedState.client) === null || _mergedState$client === void 0 || (_mergedState$client = _mergedState$client.value) === null || _mergedState$client === void 0 ? void 0 : _mergedState$client.id,
				formDataConfig: isMinorAndWaiver ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, waiverForm), {}, {
					customer_id: (_mergedState$client2 = mergedState.client) === null || _mergedState$client2 === void 0 || (_mergedState$client2 = _mergedState$client2.value) === null || _mergedState$client2 === void 0 ? void 0 : _mergedState$client2.id,
					is_franchisor: formDataConfig === null || formDataConfig === void 0 ? void 0 : formDataConfig.is_franchisor
				}) : require_objectSpread2._objectSpread2({}, formDataConfig || {})
			},
			callback: (res) => {
				var _mergedState$client3, _mergedState$pet2, _mergedState$pet3;
				const obj = {};
				for (const item of res) obj[item.key] = item.value;
				dispatch({
					type: "setPet",
					payload: { holders: !require_utils.isWalkIn((_mergedState$client3 = mergedState.client) === null || _mergedState$client3 === void 0 || (_mergedState$client3 = _mergedState$client3.value) === null || _mergedState$client3 === void 0 ? void 0 : _mergedState$client3.id) ? [obj, ...((_mergedState$pet2 = mergedState.pet) === null || _mergedState$pet2 === void 0 ? void 0 : _mergedState$pet2.holders) || []] : ((_mergedState$pet3 = mergedState.pet) === null || _mergedState$pet3 === void 0 ? void 0 : _mergedState$pet3.holders) || [] }
				});
			}
		});
	});
	if (!open || !mergedState) return null;
	return /* @__PURE__ */ react.default.createElement(require_SalesSdkSelectHolderModal.SalesSdkSelectHolderModal, {
		open,
		holders: ((_mergedState$pet4 = mergedState.pet) === null || _mergedState$pet4 === void 0 ? void 0 : _mergedState$pet4.holders) || [],
		bookingConfig: mergedState.bookingConfig,
		customerId,
		maxSelectedCount,
		onAddHolder: action ? handleAddHolder : void 0,
		onCancel: () => require_holderHostController.completeHolderHost(null),
		onSkip: () => require_holderHostController.completeHolderHost(null),
		onConfirm: (ids) => require_holderHostController.completeHolderHost(ids)
	});
};
//#endregion
exports.HolderHostRenderer = HolderHostRenderer;
