const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_buildBookingEditDrawerState = require("../bookingEditService/buildBookingEditDrawerState.js");
const require_enrichCacheItemBeforeAdd = require("./enrichCacheItemBeforeAdd.js");
const require_patchCacheItemDateRange = require("./patchCacheItemDateRange.js");
const require_mergeCatalogDurationIntoCacheItem = require("./mergeCatalogDurationIntoCacheItem.js");
const require_ensureMultiDayRangeSelected = require("./ensureMultiDayRangeSelected.js");
const require_resolveConfirmDetailQuantity = require("./resolveConfirmDetailQuantity.js");
//#region src/plus/salesSdk/utils/runAddProductFlow.ts
function openBookingEditAfterEnrich(_x, _x2, _x3, _x4, _x5, _x6) {
	return _openBookingEditAfterEnrich.apply(this, arguments);
}
function _openBookingEditAfterEnrich() {
	_openBookingEditAfterEnrich = require_asyncToGenerator._asyncToGenerator(function* (cart, hosts, payload, flowOptions, options, multiDayRange) {
		var _payload$cacheItem, _flowOptions$date, _flowOptions$customer, _flowOptions$customer2;
		let cacheItem = require_mergeCatalogDurationIntoCacheItem.mergeCatalogDurationIntoCacheItem(payload.cacheItem, ((_payload$cacheItem = payload.cacheItem) === null || _payload$cacheItem === void 0 ? void 0 : _payload$cacheItem.product_id) ? payload.cacheItem : null);
		const multiDayGate = yield require_ensureMultiDayRangeSelected.ensureMultiDayRangeSelected({
			hosts,
			cacheItem,
			catalogItem: payload.cacheItem,
			defaultStartDate: (_flowOptions$date = flowOptions.date) !== null && _flowOptions$date !== void 0 ? _flowOptions$date : payload.date,
			existingRange: multiDayRange
		});
		if (multiDayGate.status === "missing_host") throw new Error("[SalesSdk] 跨日商品需配置 uiHosts.openMultiDaySelect（createOpenMultiDaySelectHost）");
		if (multiDayGate.status === "cancelled") return { status: "cancelled" };
		if (multiDayGate.status === "ok" && multiDayGate.cacheItem) cacheItem = multiDayGate.cacheItem;
		else {
			var _flowOptions$date2;
			cacheItem = require_patchCacheItemDateRange.applyDefaultMultiDayRangeIfNeeded(cacheItem, payload.cacheItem, (_flowOptions$date2 = flowOptions.date) !== null && _flowOptions$date2 !== void 0 ? _flowOptions$date2 : payload.date);
		}
		const enriched = yield require_enrichCacheItemBeforeAdd.enrichCacheItemBeforeAdd({
			cacheItem,
			uiHosts: hosts,
			bookingConfig: flowOptions.bookingConfig,
			tempOrder: flowOptions.tempOrder,
			customerId: (_flowOptions$customer = flowOptions.customerId) !== null && _flowOptions$customer !== void 0 ? _flowOptions$customer : payload.customerId,
			date: payload.date,
			locale: flowOptions.locale,
			mode: "drawer"
		});
		if (enriched.status === "cancelled" || enriched.status === "missing_host") return { status: "cancelled" };
		const openEdit = hosts === null || hosts === void 0 ? void 0 : hosts.openBookingEdit;
		if (!openEdit) return {
			status: "requiresBookingEdit",
			payload: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, payload), {}, { cacheItem: enriched.cacheItem })
		};
		const drawerCustomerId = require_buildBookingEditDrawerState.resolveAddFlowCustomerId(flowOptions.tempOrder, (_flowOptions$customer2 = flowOptions.customerId) !== null && _flowOptions$customer2 !== void 0 ? _flowOptions$customer2 : payload.customerId);
		const edited = yield openEdit(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, payload), {}, {
			cacheItem: enriched.cacheItem,
			customerId: drawerCustomerId
		}));
		if (!edited) return { status: "cancelled" };
		const { products } = yield cart.confirmBookingEdit({
			cacheItem: edited,
			options: {
				quantity: options === null || options === void 0 ? void 0 : options.quantity,
				note: options === null || options === void 0 ? void 0 : options.note
			}
		});
		return {
			status: "added",
			products
		};
	});
	return _openBookingEditAfterEnrich.apply(this, arguments);
}
/**
* 加车编排：addProduct → 规格 → 跨日日期 → confirmDetail（内含 Holder+展开）→ 资源抽屉 → 加车。
*/
function runAddProductFlow(_x7, _x8, _x9, _x10) {
	return _runAddProductFlow.apply(this, arguments);
}
function _runAddProductFlow() {
	_runAddProductFlow = require_asyncToGenerator._asyncToGenerator(function* (cart, hosts, product, options) {
		const flowOptions = options !== null && options !== void 0 ? options : {};
		let result = yield cart.addProduct(product, options);
		let multiDayRange = null;
		if (result.status === "requiresDetail") {
			var _result$payload$produ, _result$payload, _result$payload2;
			const openDetail = hosts === null || hosts === void 0 ? void 0 : hosts.openProductDetail;
			if (!openDetail) return result;
			const detailResult = yield openDetail(result.payload);
			if (!detailResult) return { status: "cancelled" };
			const catalog = (_result$payload$produ = (_result$payload = result.payload) === null || _result$payload === void 0 ? void 0 : _result$payload.productData) !== null && _result$payload$produ !== void 0 ? _result$payload$produ : product;
			const multiDayGate = yield require_ensureMultiDayRangeSelected.ensureMultiDayRangeSelected({
				hosts,
				detail: detailResult.detail,
				catalogItem: catalog,
				product,
				defaultStartDate: (_result$payload2 = result.payload) === null || _result$payload2 === void 0 ? void 0 : _result$payload2.date
			});
			if (multiDayGate.status === "missing_host") throw new Error("[SalesSdk] 跨日商品需配置 uiHosts.openMultiDaySelect（createOpenMultiDaySelectHost）");
			if (multiDayGate.status === "cancelled") return { status: "cancelled" };
			if (multiDayGate.status === "ok") multiDayRange = multiDayGate.range;
			const confirmInput = {
				item: catalog,
				detailResult,
				options: {
					quantity: require_resolveConfirmDetailQuantity.resolveConfirmDetailQuantity({
						detailResult,
						options: flowOptions
					}),
					multiDayRange: multiDayRange || void 0
				}
			};
			result = yield cart.confirmDetail(confirmInput);
			if (result.status === "requiresBookingEdit") return openBookingEditAfterEnrich(cart, hosts, result.payload, flowOptions, options, multiDayRange);
			return result;
		}
		if (result.status === "requiresBookingEdit") return openBookingEditAfterEnrich(cart, hosts, result.payload, flowOptions, options, multiDayRange);
		return result;
	});
	return _runAddProductFlow.apply(this, arguments);
}
//#endregion
exports.runAddProductFlow = runAddProductFlow;
