const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_buildProductLineUpdates = require("./buildProductLineUpdates.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
//#region src/plus/salesSdk/utils/editCartLineBooking.ts
function findOrderLine(item, tempOrder) {
	var _item$_identity$uniqu, _item$_identity, _item$metadata, _tempOrder$products, _tempOrder$products$f;
	const uid = (_item$_identity$uniqu = (_item$_identity = item._identity) === null || _item$_identity === void 0 ? void 0 : _item$_identity.unique_identification_number) !== null && _item$_identity$uniqu !== void 0 ? _item$_identity$uniqu : (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.unique_identification_number;
	if (!uid || !(tempOrder === null || tempOrder === void 0 || (_tempOrder$products = tempOrder.products) === null || _tempOrder$products === void 0 ? void 0 : _tempOrder$products.length)) return null;
	return (_tempOrder$products$f = tempOrder.products.find((line) => {
		var _line$metadata;
		return String((_line$metadata = line.metadata) === null || _line$metadata === void 0 ? void 0 : _line$metadata.unique_identification_number) === String(uid);
	})) !== null && _tempOrder$products$f !== void 0 ? _tempOrder$products$f : null;
}
function findBookingForLine(item, tempOrder) {
	var _item$_identity$uniqu2, _item$_identity2, _item$metadata2, _tempOrder$bookings, _item$_extend;
	const uid = (_item$_identity$uniqu2 = (_item$_identity2 = item._identity) === null || _item$_identity2 === void 0 ? void 0 : _item$_identity2.unique_identification_number) !== null && _item$_identity$uniqu2 !== void 0 ? _item$_identity$uniqu2 : (_item$metadata2 = item.metadata) === null || _item$metadata2 === void 0 ? void 0 : _item$metadata2.unique_identification_number;
	if (uid && (tempOrder === null || tempOrder === void 0 || (_tempOrder$bookings = tempOrder.bookings) === null || _tempOrder$bookings === void 0 ? void 0 : _tempOrder$bookings.length)) {
		const matched = tempOrder.bookings.filter((b) => String(b === null || b === void 0 ? void 0 : b.product_uid) === String(uid));
		const childBooking = matched.find((b) => b.parent_id !== 0 && b.parent_id !== null && b.parent_id !== void 0);
		if (childBooking) return childBooking;
		if (matched[0]) return matched[0];
	}
	if ((_item$_extend = item._extend) === null || _item$_extend === void 0 ? void 0 : _item$_extend.booking) return item._extend.booking;
	return null;
}
function findCatalogProduct(productId, catalog) {
	if (productId == null) return void 0;
	return catalog.find((p) => String(p.id) === String(productId));
}
/**
* 已加车预约行：反查 cacheItem → openBookingEdit → updateOrderProduct + booking。
*/
function editCartLineBooking(_x) {
	return _editCartLineBooking.apply(this, arguments);
}
function _editCartLineBooking() {
	_editCartLineBooking = require_asyncToGenerator._asyncToGenerator(function* (input) {
		var _ref, _bookingTicket$getTem, _bookingTicket$getTem2, _ref2, _input$productsCatalo, _bookingTicket$getPro, _tempOrder$customer_i;
		const { item, bookingTicket, uiHosts, updateItem } = input;
		const tempOrder = (_ref = (_bookingTicket$getTem = (_bookingTicket$getTem2 = bookingTicket.getTempOrder) === null || _bookingTicket$getTem2 === void 0 ? void 0 : _bookingTicket$getTem2.call(bookingTicket)) !== null && _bookingTicket$getTem !== void 0 ? _bookingTicket$getTem : input.tempOrder) !== null && _ref !== void 0 ? _ref : null;
		const booking = findBookingForLine(item, tempOrder);
		if (!booking) throw new Error("[SalesSdk] 该行无关联 booking，无法编辑资源");
		const orderLine = findOrderLine(item, tempOrder);
		if (!orderLine) throw new Error("[SalesSdk] 未在 tempOrder.products 找到对应商品行");
		if (typeof bookingTicket.buildCacheItemFromOrderLine !== "function") throw new Error("[SalesSdk] bookingTicket.buildCacheItemFromOrderLine 不可用，请升级 pisellos");
		const catalog = (_ref2 = (_input$productsCatalo = input.productsCatalog) !== null && _input$productsCatalo !== void 0 ? _input$productsCatalo : (_bookingTicket$getPro = bookingTicket.getProductCatalog) === null || _bookingTicket$getPro === void 0 || (_bookingTicket$getPro = _bookingTicket$getPro.call(bookingTicket)) === null || _bookingTicket$getPro === void 0 ? void 0 : _bookingTicket$getPro.products) !== null && _ref2 !== void 0 ? _ref2 : [];
		const sourceProduct = findCatalogProduct(orderLine.product_id, catalog);
		const cacheItem = bookingTicket.buildCacheItemFromOrderLine({
			product: orderLine,
			booking,
			sourceProduct: sourceProduct !== null && sourceProduct !== void 0 ? sourceProduct : null
		});
		const openEdit = uiHosts === null || uiHosts === void 0 ? void 0 : uiHosts.openBookingEdit;
		if (!openEdit) throw new Error("[SalesSdk] uiHosts.openBookingEdit 未配置");
		if (typeof bookingTicket.initBookingContext === "function") try {
			var _tempOrder$bookings2, _bookingTicket$isBook, _bookingTicket$isBook2;
			const bookingIds = tempOrder === null || tempOrder === void 0 || (_tempOrder$bookings2 = tempOrder.bookings) === null || _tempOrder$bookings2 === void 0 ? void 0 : _tempOrder$bookings2.map((b) => b === null || b === void 0 ? void 0 : b.id).filter((id) => typeof id === "number" && Number.isFinite(id));
			const initParams = {
				date: booking.start_date ? (0, dayjs.default)(String(booking.start_date)) : (0, dayjs.default)(),
				bookingIds: (bookingIds === null || bookingIds === void 0 ? void 0 : bookingIds.length) ? bookingIds : void 0
			};
			if (!((_bookingTicket$isBook = (_bookingTicket$isBook2 = bookingTicket.isBookingContextReady) === null || _bookingTicket$isBook2 === void 0 ? void 0 : _bookingTicket$isBook2.call(bookingTicket, initParams)) !== null && _bookingTicket$isBook !== void 0 ? _bookingTicket$isBook : false)) yield bookingTicket.initBookingContext(initParams);
		} catch (e) {
			console.warn("[SalesSdk] initBookingContext before edit 失败", e);
		}
		const edited = yield openEdit({
			cacheItem,
			date: booking.start_date != null ? String(booking.start_date) : null,
			forceOpen: true,
			customerId: (_tempOrder$customer_i = tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.customer_id) !== null && _tempOrder$customer_i !== void 0 ? _tempOrder$customer_i : void 0
		});
		if (!edited) return { status: "cancelled" };
		if (typeof bookingTicket.transformDetailToProductAndBooking !== "function") throw new Error("[SalesSdk] bookingTicket.transformDetailToProductAndBooking 不可用");
		const catalogItem = sourceProduct !== null && sourceProduct !== void 0 ? sourceProduct : { id: orderLine.product_id };
		const { product: productPatch, booking: bookingPatch } = bookingTicket.transformDetailToProductAndBooking({
			item: catalogItem,
			cacheItem: edited
		});
		const identity = item._identity;
		return {
			status: "updated",
			products: yield updateItem({
				product_id: identity.product_id,
				product_variant_id: identity.product_variant_id,
				unique_identification_number: identity.unique_identification_number,
				product_option_item: identity.product_option_item,
				product_bundle: identity.product_bundle,
				updates: require_buildProductLineUpdates.buildProductLineUpdates(orderLine, productPatch),
				booking: bookingPatch
			})
		};
	});
	return _editCartLineBooking.apply(this, arguments);
}
//#endregion
exports.editCartLineBooking = editCartLineBooking;
