import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { resolveAddFlowCustomerId } from "../bookingEditService/buildBookingEditDrawerState.js";
import { expandMultiDayCacheItem } from "./expandMultiDayCacheItem.js";
import { buildBookingStateForAddFlow } from "./buildBookingStateForAddFlow.js";
import { isHolderRequiredOnAddCart, shouldPromptHolderOnAddCart } from "./shouldPromptHolderOnAddCart.js";
import { applyHolderToCacheItem } from "./applyHolderToCacheItem.js";
//#region src/plus/salesSdk/utils/enrichCacheItemBeforeAdd.ts
/**
* 加车前 Holder + 跨日展开（对齐 ticketBooking handleBooking4Service / processManualEditService）。
*/
function enrichCacheItemBeforeAdd(_x) {
	return _enrichCacheItemBeforeAdd.apply(this, arguments);
}
function _enrichCacheItemBeforeAdd() {
	_enrichCacheItemBeforeAdd = _asyncToGenerator(function* (input) {
		let cacheItem = input.cacheItem;
		const customerId = resolveAddFlowCustomerId(input.tempOrder, input.customerId);
		if (shouldPromptHolderOnAddCart({
			cacheItem,
			bookingConfig: input.bookingConfig,
			customerId
		})) {
			var _input$uiHosts;
			const openHolder = (_input$uiHosts = input.uiHosts) === null || _input$uiHosts === void 0 ? void 0 : _input$uiHosts.openHolderSelect;
			if (!openHolder) {
				if (isHolderRequiredOnAddCart(input.bookingConfig)) return {
					status: "missing_host",
					missing: "holder"
				};
			} else {
				var _input$bookingConfig, _cacheItem$_extend;
				const holderConfig = ((_input$bookingConfig = input.bookingConfig) === null || _input$bookingConfig === void 0 ? void 0 : _input$bookingConfig.holder) || {};
				const holderIds = yield openHolder({
					cacheItem,
					customerId,
					maxSelectedCount: (holderConfig === null || holderConfig === void 0 ? void 0 : holderConfig.maxSelectedCount) || (cacheItem === null || cacheItem === void 0 || (_cacheItem$_extend = cacheItem._extend) === null || _cacheItem$_extend === void 0 ? void 0 : _cacheItem$_extend.quantity) || 1
				});
				if (holderIds && holderIds.length > 0) cacheItem = applyHolderToCacheItem(cacheItem, holderIds);
				else if (isHolderRequiredOnAddCart(input.bookingConfig)) return { status: "cancelled" };
			}
		}
		const bookingState = buildBookingStateForAddFlow({
			cacheItem,
			bookingConfig: input.bookingConfig,
			tempOrder: input.tempOrder,
			customerId,
			date: input.date,
			locale: input.locale,
			resourcesOrigin: []
		});
		try {
			cacheItem = yield expandMultiDayCacheItem(cacheItem, bookingState, { concatBookings: input.mode === "drawer" });
		} catch (e) {
			console.warn("[SalesSdk] expandMultiDayCacheItem failed", e);
			return { status: "cancelled" };
		}
		return {
			status: "ok",
			cacheItem
		};
	});
	return _enrichCacheItemBeforeAdd.apply(this, arguments);
}
//#endregion
export { enrichCacheItemBeforeAdd };
